diff --git a/packages/vite/src/plugins/dynamic-base.ts b/packages/vite/src/plugins/dynamic-base.ts index 1d6008ad02..6ef33dc073 100644 --- a/packages/vite/src/plugins/dynamic-base.ts +++ b/packages/vite/src/plugins/dynamic-base.ts @@ -33,6 +33,12 @@ export const RelativeAssetPlugin = function (): Plugin { .replace(assetRE, r => r.replace(/\/__NUXT_BASE__/g, assetBase)) .replace(/\/__NUXT_BASE__/g, publicBase) } + if (asset.type === 'chunk' && typeof asset.code === 'string') { + asset.code = asset.code + .replace(/`\$\{(_?_?publicAssetsURL|buildAssetsURL|)\(\)\}([^`]*)`/g, '$1(`$2`)') + .replace(/"\/__NUXT_BASE__\/([^"]*)"\.replace\("\/__NUXT_BASE__", ""\)/g, '"$1"') + .replace(/'\/__NUXT_BASE__\/([^']*)'\.replace\("\/__NUXT_BASE__", ""\)/g, '"$1"') + } } } } diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index e606c74bfd..5ec95c56f6 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -13,6 +13,7 @@ import { prepareDevServerEntry } from './vite-node' import { isCSS, isDirectory, readDirRecursively } from './utils' import { bundleRequest } from './dev-bundler' import { writeManifest } from './manifest' +import { RelativeAssetPlugin } from './plugins/dynamic-base' export async function buildServer (ctx: ViteBuildContext) { const _resolve = id => resolveModule(id, { paths: ctx.nuxt.options.modulesDir }) @@ -74,6 +75,7 @@ export async function buildServer (ctx: ViteBuildContext) { }, plugins: [ cacheDirPlugin(ctx.nuxt.options.rootDir, 'server'), + RelativeAssetPlugin(), vuePlugin(ctx.config.vue), viteJsxPlugin() ]