From 3ea39e074542688924c0132a097f70df2590ef65 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 6 Apr 2022 15:15:36 +0100 Subject: [PATCH] fix(vite): clean up dynamic paths (#4130) --- packages/vite/src/plugins/dynamic-base.ts | 6 ++++++ packages/vite/src/server.ts | 2 ++ 2 files changed, 8 insertions(+) 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() ]