From fcab1edbc59073c61399a348ec89f6756afc94e6 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Tue, 10 Sep 2024 02:09:10 +0800 Subject: [PATCH] refactor(bundler-vite): remove vite-root temp dir and clean up stale fix for vue plugin --- .../src/plugins/vuepressMainPlugin.ts | 47 ++++++++++-------- .../src/plugins/vuepressVuePlugin.ts | 48 +------------------ .../bundler-vite/src/resolveViteConfig.ts | 2 +- 3 files changed, 30 insertions(+), 67 deletions(-) diff --git a/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts b/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts index da2ce652fe..a7dd20a27c 100644 --- a/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts +++ b/packages/bundler-vite/src/plugins/vuepressMainPlugin.ts @@ -107,24 +107,6 @@ export const vuepressMainPlugin = ({ name: 'vuepress:main', config: async () => { - // create a temp index.html as dev entry point - if (!isBuild) { - await app.writeTemp( - 'vite-root/index.html', - fs - .readFileSync(app.options.templateDev) - .toString() - .replace( - /<\/body>/, - `\ - -`, - ), - ) - } - // vuepress related packages that include pure esm client code, // which should not be optimized in dev mode, and should not be // externalized in build ssr mode @@ -147,7 +129,7 @@ import 'vuepress/client-app' } return { - root: app.dir.temp('vite-root'), + root: app.dir.source(), base: app.options.base, mode: !isBuild || app.env.isDebug ? 'development' : 'production', define: await resolveDefine({ app, isBuild, isServer }), @@ -231,6 +213,33 @@ import 'vuepress/client-app' ], }) as Connect.NextHandleFunction, ) + + // serve the dev template as `/index.html` + server.middlewares.use((req, res, next) => { + if (!req.url?.endsWith('.html')) { + next() + return + } + + res.statusCode = 200 + res.setHeader('Content-Type', 'text/html') + const indexHtml = fs + .readFileSync(app.options.templateDev) + .toString() + .replace( + /<\/body>/, + `\ + +`, + ) + void server + .transformIndexHtml(req.url, indexHtml, req.originalUrl) + .then((result) => { + res.end(result) + }) + }) } }, }) diff --git a/packages/bundler-vite/src/plugins/vuepressVuePlugin.ts b/packages/bundler-vite/src/plugins/vuepressVuePlugin.ts index 96b11b514a..dd5185d548 100644 --- a/packages/bundler-vite/src/plugins/vuepressVuePlugin.ts +++ b/packages/bundler-vite/src/plugins/vuepressVuePlugin.ts @@ -1,57 +1,11 @@ import vuePlugin from '@vitejs/plugin-vue' import type { Plugin } from 'vite' -import type { AssetURLOptions, AssetURLTagConfig } from 'vue/compiler-sfc' import type { ViteBundlerOptions } from '../types.js' /** - * Determine if the given `transformAssetUrls` option is `AssetURLTagConfig` - */ -const isAssetURLTagConfig = ( - transformAssetUrls: AssetURLOptions | AssetURLTagConfig, -): transformAssetUrls is AssetURLTagConfig => - Object.values(transformAssetUrls).some((val) => Array.isArray(val)) - -/** - * Resolve `template.transformAssetUrls` option from user config - */ -const resolveTransformAssetUrls = ( - options: ViteBundlerOptions, -): AssetURLOptions => { - // default transformAssetUrls option - const defaultTransformAssetUrls = { includeAbsolute: true } - - // user provided transformAssetUrls option - const { transformAssetUrls: userTransformAssetUrls } = - options.vuePluginOptions?.template ?? {} - - // if user does not provide an object as transformAssetUrls - if (typeof userTransformAssetUrls !== 'object') { - return defaultTransformAssetUrls - } - - // AssetURLTagConfig - if (isAssetURLTagConfig(userTransformAssetUrls)) { - return { - ...defaultTransformAssetUrls, - tags: userTransformAssetUrls, - } - } - - // AssetURLOptions - return { - ...defaultTransformAssetUrls, - ...userTransformAssetUrls, - } -} - -/** - * Wrapper of official vue plugin + * Wrapper of the official vue plugin */ export const vuepressVuePlugin = (options: ViteBundlerOptions): Plugin => vuePlugin({ ...options.vuePluginOptions, - template: { - ...options.vuePluginOptions?.template, - transformAssetUrls: resolveTransformAssetUrls(options), - }, }) diff --git a/packages/bundler-vite/src/resolveViteConfig.ts b/packages/bundler-vite/src/resolveViteConfig.ts index 2c381386c7..ac4cadeed8 100644 --- a/packages/bundler-vite/src/resolveViteConfig.ts +++ b/packages/bundler-vite/src/resolveViteConfig.ts @@ -28,9 +28,9 @@ export const resolveViteConfig = ({ charset: 'utf8', }, plugins: [ - vuepressVuePlugin(options), vuepressMainPlugin({ app, isBuild, isServer }), vuepressUserConfigPlugin(options), + vuepressVuePlugin(options), ], }, // some vite options would not take effect inside a plugin, so we still need to merge them here in addition to userConfigPlugin