diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 62248025127253..bd8ee293e04500 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -196,12 +196,25 @@ export async function urlToBuiltUrl( config: ResolvedConfig, pluginContext: PluginContext ): Promise { + url = (await pluginContext.resolve(url, importer))?.id || url + + // The resolving plugin wants to force its own url. + if (url.startsWith('!')) { + return url.slice(1) + } + + if (url.startsWith(config.root + '/')) { + url = url.split(config.root)[1] + } + if (checkPublicFile(url, config.root)) { return config.build.base + url.slice(1) } + const file = url.startsWith('/') ? path.join(config.root, url) : path.join(path.dirname(importer), url) + return fileToBuiltUrl( file, config,