fix: ignore html in mpa (#4582)

This commit is contained in:
Rhuzerv 2021-08-30 11:03:08 +08:00 committed by GitHub
parent f3f8d09e38
commit 04eb146b1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 13 deletions

View File

@ -7,4 +7,4 @@
"sassLoaderOptions": {
"prependData": ".test{color:red}"
}
}
}

View File

@ -0,0 +1,25 @@
import type { Plugin } from 'vite';
import * as path from 'path';
import * as glob from 'fast-glob';
import * as fs from 'fs';
export const ignoreHtmlPlugin = (rootDir: string): Plugin => {
let outDir = '';
return {
name: 'vite-plugin-ignore-html',
enforce: 'post',
config(cfg, { command }) {
if (command === 'build') {
outDir = cfg.build?.outDir ?? 'dist';
}
},
closeBundle() {
const distPath = path.resolve(rootDir, outDir);
const htmlFiles = glob.sync(path.resolve(distPath, '*.html'));
htmlFiles.forEach(file => {
fs.rmSync(file);
});
}
};
};

View File

@ -2,4 +2,5 @@ export * from './external';
export * from './history';
export * from './import';
export * from './polyfill';
export * from './html';
export * from './html';
export * from './ignoreHtml';

View File

@ -12,6 +12,7 @@ import {
polyfillPlugin,
serverHistoryPlugin,
htmlPlugin,
ignoreHtmlPlugin
} from '../plugins';
type Option = BuildOptions & InlineConfig;
@ -27,16 +28,18 @@ const getWebpackConfig = (context: Context) => {
return configArr[0];
};
const isBuild = (command: string) => command === 'build';
const getHtmlPlugin = (context: Context) => {
const { getValue, userConfig, rootDir } = context;
type Opt = {
inject: boolean
templateParameters: Record<string, string>
favicon: string
template: string
minify: boolean
filename: string
excludeChunks: string[]
inject?: boolean
templateParameters?: Record<string, string>
favicon?: string
minify?: boolean
excludeChunks?: string[]
}
const isMpa = userConfig.mpa as boolean;
@ -50,15 +53,29 @@ const getHtmlPlugin = (context: Context) => {
});
}
const pages = getValue('MPA_PAGES') as Record<string, Opt>;
const pages = {
index: {
template: path.resolve(rootDir, 'public', 'index.html'),
filename: 'index.html',
},
...getValue('MPA_PAGES'),
} as Record<string, Opt>;
const entries = userConfig.entry as Record<string, string[]>;
const mpaHtmlPlugins = Object.keys(pages).map(pageName => {
const singlePage = pages[pageName];
const mpaHtmlPlugins = Object.keys(entries).map(entryName => {
const singlePage = pages[entryName] ?? pages.index;
if (entryName === 'index') {
return htmlPlugin({
...singlePage,
entry: entries[entryName][0], // webpack entry
rootDir
});
}
return htmlPlugin({
...singlePage,
entry: entries[pageName][0], // webpack entry
entry: entries[entryName][0], // webpack entry
rootDir
});
});
@ -102,7 +119,8 @@ export const wp2vite = (context: Context): InlineConfig => {
hash: userConfig.hash as boolean,
outputAssetsPath: userConfig.outputAssetsPath as any,
rootDir,
})
}),
userConfig.ignoreHtmlTemplate ? ignoreHtmlPlugin(rootDir) : null
],
};
@ -139,7 +157,7 @@ export const wp2vite = (context: Context): InlineConfig => {
return entries;
};
if (command === 'start') {
if (!isBuild(command)) {
return all([
{
optimizeDeps: {