Skip to content

Commit 0e370a2

Browse files
author
Loïc Mangeonjean
committed
fix: make the new URL(..., import.meta.url) syntax always point to the untransformed version of the file
1 parent 689a066 commit 0e370a2

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

packages/vite/src/node/plugins/asset.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,11 @@ export async function fileToUrl(
321321
pluginContext: PluginContext,
322322
id: string,
323323
asFileUrl = false,
324+
raw: boolean = false,
324325
): Promise<string> {
325326
const { environment } = pluginContext
326327
if (!environment.config.isBundled) {
327-
return fileToDevUrl(environment, id, asFileUrl)
328+
return fileToDevUrl(environment, id, asFileUrl, raw)
328329
} else {
329330
return fileToBuiltUrl(pluginContext, id)
330331
}
@@ -334,6 +335,7 @@ export async function fileToDevUrl(
334335
environment: Environment,
335336
id: string,
336337
asFileUrl = false,
338+
raw: boolean,
337339
): Promise<string> {
338340
const config = environment.getTopLevelConfig()
339341
const publicFile = checkPublicFile(id, config)
@@ -364,11 +366,11 @@ export async function fileToDevUrl(
364366
if (publicFile) {
365367
// in public dir during dev, keep the url as-is
366368
rtn = id
367-
} else if (id.startsWith(withTrailingSlash(config.root))) {
368-
// in project root, infer short public path
369+
} else if (id.startsWith(withTrailingSlash(config.root)) && !raw) {
370+
// in project root and not raw, infer short public path
369371
rtn = '/' + path.posix.relative(config.root, id)
370372
} else {
371-
// outside of project root, use absolute fs path
373+
// outside of project root or raw, use absolute fs path
372374
// (this is special handled by the serve static middleware
373375
rtn = path.posix.join(FS_PREFIX, id)
374376
}

packages/vite/src/node/plugins/assetImportMetaUrl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
144144
try {
145145
if (publicDir && isParentDirectory(publicDir, file)) {
146146
const publicPath = '/' + path.posix.relative(publicDir, file)
147-
builtUrl = await fileToUrl(this, publicPath)
147+
builtUrl = await fileToUrl(this, publicPath, undefined, true)
148148
} else {
149-
builtUrl = await fileToUrl(this, file)
149+
builtUrl = await fileToUrl(this, file, true)
150150
// during dev, builtUrl may point to a directory or a non-existing file
151151
if (tryStatSync(file)?.isFile()) {
152152
this.addWatchFile(file)

0 commit comments

Comments
 (0)