Skip to content

Commit 4333bc8

Browse files
authored
fix: issue #134 (#137)
1 parent ac54a6e commit 4333bc8

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

packages/core/src/htmlPlugin.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
2626

2727
let viteConfig: ResolvedConfig
2828
let env: Record<string, any> = {}
29+
const transformIndexHtmlHandler = async (html, ctx) => {
30+
const url = ctx.filename
31+
const base = viteConfig.base
32+
const excludeBaseUrl = url.replace(base, '/')
33+
const htmlName = path.relative(process.cwd(), excludeBaseUrl)
34+
35+
const page = getPage(userOptions, htmlName, viteConfig)
36+
const { injectOptions = {} } = page
37+
const _html = await renderHtml(html, {
38+
injectOptions,
39+
viteConfig,
40+
env,
41+
entry: page.entry || entry,
42+
verbose,
43+
})
44+
const { tags = [] } = injectOptions
45+
return {
46+
html: _html,
47+
tags: tags,
48+
}
49+
}
2950

3051
return {
3152
name: 'vite:html',
@@ -93,30 +114,18 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
93114
)
94115
},
95116

96-
transformIndexHtml: {
97-
enforce: 'pre',
98-
async transform(html, ctx) {
99-
const url = ctx.filename
100-
const base = viteConfig.base
101-
const excludeBaseUrl = url.replace(base, '/')
102-
const htmlName = path.relative(process.cwd(), excludeBaseUrl)
103-
104-
const page = getPage(userOptions, htmlName, viteConfig)
105-
const { injectOptions = {} } = page
106-
const _html = await renderHtml(html, {
107-
injectOptions,
108-
viteConfig,
109-
env,
110-
entry: page.entry || entry,
111-
verbose,
112-
})
113-
const { tags = [] } = injectOptions
114-
return {
115-
html: _html,
116-
tags: tags,
117-
}
117+
transformIndexHtml: userOptions.viteNext
118+
?
119+
{
120+
// @ts-ignore
121+
order: 'pre',
122+
handler: transformIndexHtmlHandler,
123+
}
124+
:
125+
{
126+
enforce: 'pre',
127+
transform: transformIndexHtmlHandler,
118128
},
119-
},
120129
async closeBundle() {
121130
const outputDirs: string[] = []
122131

packages/core/src/typing.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ export interface UserOptions {
5858
* @default false
5959
*/
6060
verbose?: boolean
61+
62+
/**
63+
* fit vite2+
64+
*/
65+
viteNext?: boolean
6166
}

0 commit comments

Comments
 (0)