From 78761c236b8befce935322c1ee2d7b9f22a3ea90 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 4 Mar 2022 11:45:14 +0000 Subject: [PATCH] fix(vite): handle base in `vite/preload-helper` (#3499) --- packages/vite/src/plugins/dynamic-base.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/plugins/dynamic-base.ts b/packages/vite/src/plugins/dynamic-base.ts index 1380eb13b1..51dee93196 100644 --- a/packages/vite/src/plugins/dynamic-base.ts +++ b/packages/vite/src/plugins/dynamic-base.ts @@ -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(`(?