From 04eb146b1ed64424e8e4052fd479be979f05c166 Mon Sep 17 00:00:00 2001 From: Rhuzerv <1339184537@qq.com> Date: Mon, 30 Aug 2021 11:03:08 +0800 Subject: [PATCH] fix: ignore html in mpa (#4582) --- examples/basic-vite/build.json | 2 +- .../vite-service/src/plugins/ignoreHtml.ts | 25 ++++++++++++ packages/vite-service/src/plugins/index.ts | 3 +- packages/vite-service/src/wp2vite/index.ts | 40 ++++++++++++++----- 4 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 packages/vite-service/src/plugins/ignoreHtml.ts diff --git a/examples/basic-vite/build.json b/examples/basic-vite/build.json index e302d032d..2959f2614 100644 --- a/examples/basic-vite/build.json +++ b/examples/basic-vite/build.json @@ -7,4 +7,4 @@ "sassLoaderOptions": { "prependData": ".test{color:red}" } -} +} \ No newline at end of file diff --git a/packages/vite-service/src/plugins/ignoreHtml.ts b/packages/vite-service/src/plugins/ignoreHtml.ts new file mode 100644 index 000000000..d2bcad825 --- /dev/null +++ b/packages/vite-service/src/plugins/ignoreHtml.ts @@ -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); + }); + } + }; +}; diff --git a/packages/vite-service/src/plugins/index.ts b/packages/vite-service/src/plugins/index.ts index 14cb0e40c..afe967b42 100644 --- a/packages/vite-service/src/plugins/index.ts +++ b/packages/vite-service/src/plugins/index.ts @@ -2,4 +2,5 @@ export * from './external'; export * from './history'; export * from './import'; export * from './polyfill'; -export * from './html'; \ No newline at end of file +export * from './html'; +export * from './ignoreHtml'; diff --git a/packages/vite-service/src/wp2vite/index.ts b/packages/vite-service/src/wp2vite/index.ts index d234eb59e..2389c8c17 100644 --- a/packages/vite-service/src/wp2vite/index.ts +++ b/packages/vite-service/src/wp2vite/index.ts @@ -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 - favicon: string template: string - minify: boolean filename: string - excludeChunks: string[] + inject?: boolean + templateParameters?: Record + 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; + const pages = { + index: { + template: path.resolve(rootDir, 'public', 'index.html'), + filename: 'index.html', + }, + ...getValue('MPA_PAGES'), + } as Record; const entries = userConfig.entry as Record; - 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: {