fix(vite): handle base in `vite/preload-helper` (#3499)

This commit is contained in:
Daniel Roe 2022-03-04 11:45:14 +00:00 committed by GitHub
parent 9f506ded60
commit 78761c236b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 10 deletions

View File

@ -49,7 +49,7 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl
if (options.globalPublicPath && id.includes('entry.ts')) {
injectUtils = true
s.prepend(`${options.globalPublicPath} = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);`)
s.prepend(`${options.globalPublicPath} = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);\n`)
}
const assetId = code.match(VITE_ASSET_RE)
@ -59,24 +59,21 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl
}
if (injectUtils || (code.includes('NUXT_BASE') && !code.includes('const NUXT_BASE ='))) {
s.prepend('const NUXT_BASE = NUXT_CONFIG.app.cdnURL || NUXT_CONFIG.app.baseURL;')
s.prepend('const NUXT_BASE = NUXT_CONFIG.app.cdnURL || NUXT_CONFIG.app.baseURL;\n')
if (options.env === 'dev') {
s.prepend(`const NUXT_CONFIG = { app: ${JSON.stringify(options.devAppConfig)} };`)
s.prepend(`const NUXT_CONFIG = { app: ${JSON.stringify(options.devAppConfig)} };\n`)
} else if (options.env === 'server') {
s.prepend('import NUXT_CONFIG from "#config";')
s.prepend('import NUXT_CONFIG from "#config";\n')
} else {
s.prepend('const NUXT_CONFIG = __NUXT__.config;')
s.prepend('const NUXT_CONFIG = __NUXT__.config;\n')
}
}
if (id === 'vite/preload-helper') {
injectUtils = true
// Define vite base path as buildAssetsUrl (i.e. including _nuxt/)
code.replace(
/const base = ['"]\/__NUXT_BASE__\/['"]/,
'const base = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);'
)
s.replace(/const base = ['"]\/__NUXT_BASE__\/['"]/, 'const base = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir)')
}
// Sanitize imports
@ -84,7 +81,7 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl
// Dynamically compute string URLs featuring baseURL
for (const delimiter of ['`', '"', "'"]) {
const delimiterRE = new RegExp(`${delimiter}([^${delimiter}]*)\\/__NUXT_BASE__\\/([^${delimiter}]*)${delimiter}`, 'g')
const delimiterRE = new RegExp(`(?<!const base = )${delimiter}([^${delimiter}]*)\\/__NUXT_BASE__\\/([^${delimiter}]*)${delimiter}`, 'g')
/* eslint-disable-next-line no-template-curly-in-string */
s.replace(delimiterRE, '`$1${NUXT_BASE}$2`')
}