Skip to content

Commit

Permalink
fix: issue vbenjs#134 (vbenjs#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryomahan authored and gao.boyu committed Feb 19, 2024
1 parent bdc69ec commit 80a48b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
55 changes: 32 additions & 23 deletions packages/core/src/htmlPlugin.ts
Expand Up @@ -26,6 +26,27 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {

let viteConfig: ResolvedConfig
let env: Record<string, any> = {}
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',
Expand Down Expand Up @@ -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[] = []

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/typing.ts
Expand Up @@ -58,4 +58,9 @@ export interface UserOptions {
* @default false
*/
verbose?: boolean

/**
* fit vite2+
*/
viteNext?: boolean
}

0 comments on commit 80a48b5

Please sign in to comment.