From 80a48b53f5d2367b9dca88ddcbb8c1ed001a6af3 Mon Sep 17 00:00:00 2001 From: ryomahan Date: Tue, 26 Dec 2023 17:57:18 +0800 Subject: [PATCH] fix: issue #134 (#137) --- packages/core/src/htmlPlugin.ts | 55 +++++++++++++++++++-------------- packages/core/src/typing.ts | 5 +++ 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/packages/core/src/htmlPlugin.ts b/packages/core/src/htmlPlugin.ts index dea54f0..1a1dff2 100644 --- a/packages/core/src/htmlPlugin.ts +++ b/packages/core/src/htmlPlugin.ts @@ -26,6 +26,27 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption { let viteConfig: ResolvedConfig let env: Record = {} + const transformIndexHtmlHandler = async (html, ctx) => { + const url = ctx.filename + const base = viteConfig.base + const excludeBaseUrl = url.replace(base, '/') + const htmlName = path.relative(process.cwd(), excludeBaseUrl) + + const page = getPage(userOptions, htmlName, viteConfig) + const { injectOptions = {} } = page + const _html = await renderHtml(html, { + injectOptions, + viteConfig, + env, + entry: page.entry || entry, + verbose, + }) + const { tags = [] } = injectOptions + return { + html: _html, + tags: tags, + } + } return { name: 'vite:html', @@ -93,30 +114,18 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption { ) }, - transformIndexHtml: { - enforce: 'pre', - async transform(html, ctx) { - const url = ctx.filename - const base = viteConfig.base - const excludeBaseUrl = url.replace(base, '/') - const htmlName = path.relative(process.cwd(), excludeBaseUrl) - - const page = getPage(userOptions, htmlName, viteConfig) - const { injectOptions = {} } = page - const _html = await renderHtml(html, { - injectOptions, - viteConfig, - env, - entry: page.entry || entry, - verbose, - }) - const { tags = [] } = injectOptions - return { - html: _html, - tags: tags, - } + transformIndexHtml: userOptions.viteNext + ? + { + // @ts-ignore + order: 'pre', + handler: transformIndexHtmlHandler, + } + : + { + enforce: 'pre', + transform: transformIndexHtmlHandler, }, - }, async closeBundle() { const outputDirs: string[] = [] diff --git a/packages/core/src/typing.ts b/packages/core/src/typing.ts index b50ddf6..9c2d17a 100644 --- a/packages/core/src/typing.ts +++ b/packages/core/src/typing.ts @@ -58,4 +58,9 @@ export interface UserOptions { * @default false */ verbose?: boolean + + /** + * fit vite2+ + */ + viteNext?: boolean }