Skip to content

Commit 0bb07f1

Browse files
committed
fix(nuxt): resolve real paths imported into virtual files
1 parent 91568c5 commit 0bb07f1

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

packages/nuxt/src/core/plugins/virtual.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface VirtualFSPluginOptions {
1111
}
1212

1313
const RELATIVE_ID_RE = /^\.{1,2}[\\/]/
14-
export const VirtualFSPlugin = (nuxt: Nuxt, options: VirtualFSPluginOptions) => createUnplugin(() => {
14+
export const VirtualFSPlugin = (nuxt: Nuxt, options: VirtualFSPluginOptions) => createUnplugin((_, meta) => {
1515
const extensions = ['', ...nuxt.options.extensions]
1616
const alias = { ...nuxt.options.alias, ...options.alias }
1717

@@ -26,31 +26,46 @@ export const VirtualFSPlugin = (nuxt: Nuxt, options: VirtualFSPluginOptions) =>
2626
}
2727
}
2828

29-
return {
30-
name: 'nuxt:virtual',
29+
function resolveId (id: string, importer?: string) {
30+
id = resolveAlias(id, alias)
3131

32-
resolveId: {
33-
order: 'pre',
34-
handler (id, importer) {
35-
id = resolveAlias(id, alias)
32+
if (process.platform === 'win32' && isAbsolute(id)) {
33+
// Add back C: prefix on Windows
34+
id = resolve(id)
35+
}
3636

37-
if (process.platform === 'win32' && isAbsolute(id)) {
38-
// Add back C: prefix on Windows
39-
id = resolve(id)
40-
}
37+
const resolvedId = resolveWithExt(id)
38+
if (resolvedId) {
39+
return PREFIX + encodeURIComponent(resolvedId)
40+
}
4141

42-
const resolvedId = resolveWithExt(id)
43-
if (resolvedId) {
44-
return PREFIX + encodeURIComponent(resolvedId)
45-
}
42+
if (importer && RELATIVE_ID_RE.test(id)) {
43+
const path = resolve(dirname(withoutPrefix(decodeURIComponent(importer))), id)
44+
// resolve relative paths to virtual files
45+
const resolved = resolveWithExt(path)
46+
if (resolved) {
47+
return PREFIX + encodeURIComponent(resolved)
48+
}
49+
}
50+
}
51+
52+
return {
53+
name: 'nuxt:virtual',
4654

47-
if (importer && RELATIVE_ID_RE.test(id)) {
48-
const path = resolve(dirname(withoutPrefix(decodeURIComponent(importer))), id)
49-
const resolved = resolveWithExt(path)
50-
if (resolved) {
51-
return PREFIX + encodeURIComponent(resolved)
55+
resolveId: meta.framework === 'vite' ? undefined : { order: 'pre', handler: resolveId },
56+
57+
vite: {
58+
resolveId: {
59+
order: 'pre',
60+
handler (id, importer) {
61+
const res = resolveId(id, importer)
62+
if (res) {
63+
return res
5264
}
53-
}
65+
if (importer && importer.startsWith(PREFIX) && RELATIVE_ID_RE.test(id)) {
66+
return this.resolve?.(id, withoutPrefix(decodeURIComponent(importer)), { skipSelf: true })
67+
}
68+
},
5469
},
5570
},
5671

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy