Skip to content

Commit f71ba5b

Browse files
authored
fix: dynamic-import-vars plugin normalize path issue (#16518)
1 parent 2d50be2 commit f71ba5b

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

packages/vite/src/node/__tests__/plugins/dynamicImportVar/__snapshots__/parse.spec.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ exports[`parse positives > ? in worker 1`] = `"__variableDynamicImportRuntimeHel
88
99
exports[`parse positives > alias path 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("./mods/*.js")), \`./mods/\${base}.js\`)"`;
1010
11+
exports[`parse positives > alias path with multi ../ 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("../../*.js")), \`../../\${base}.js\`)"`;
12+
1113
exports[`parse positives > basic 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("./mods/*.js")), \`./mods/\${base}.js\`)"`;
1214
1315
exports[`parse positives > with ../ and itself 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("../dynamicImportVar/*.js")), \`./\${name}.js\`)"`;

packages/vite/src/node/__tests__/plugins/dynamicImportVar/parse.spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ async function run(input: string) {
1212
(await transformDynamicImport(
1313
input,
1414
normalizePath(resolve(__dirname, 'index.js')),
15-
(id) => id.replace('@', resolve(__dirname, './mods/')),
15+
(id) =>
16+
id
17+
.replace('@', resolve(__dirname, './mods/'))
18+
.replace('#', resolve(__dirname, '../../')),
1619
__dirname,
1720
)) || {}
1821
return `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`
@@ -27,6 +30,10 @@ describe('parse positives', () => {
2730
expect(await run('`@/${base}.js`')).toMatchSnapshot()
2831
})
2932

33+
it('alias path with multi ../', async () => {
34+
expect(await run('`#/${base}.js`')).toMatchSnapshot()
35+
})
36+
3037
it('with query', async () => {
3138
expect(await run('`./mods/${base}.js?foo=bar`')).toMatchSnapshot()
3239
})

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@ export async function transformDynamicImport(
117117
if (!resolvedFileName) {
118118
return null
119119
}
120-
const relativeFileName = posix.relative(
121-
posix.dirname(normalizePath(importer)),
122-
normalizePath(resolvedFileName),
123-
)
124-
importSource = normalizePath(
125-
'`' + (relativeFileName[0] === '.' ? '' : './') + relativeFileName + '`',
120+
const relativeFileName = normalizePath(
121+
posix.relative(
122+
posix.dirname(normalizePath(importer)),
123+
normalizePath(resolvedFileName),
124+
),
126125
)
126+
importSource =
127+
'`' + (relativeFileName[0] === '.' ? '' : './') + relativeFileName + '`'
127128
}
128129

129130
const dynamicImportPattern = parseDynamicImportPattern(importSource)

0 commit comments

Comments
 (0)