Skip to content

Commit

Permalink
fix: dynamic-import-vars plugin normalize path issue (#16518)
Browse files Browse the repository at this point in the history
  • Loading branch information
godky authored Apr 25, 2024
1 parent 2d50be2 commit f71ba5b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ exports[`parse positives > ? in worker 1`] = `"__variableDynamicImportRuntimeHel
exports[`parse positives > alias path 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("./mods/*.js")), \`./mods/\${base}.js\`)"`;
exports[`parse positives > alias path with multi ../ 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("../../*.js")), \`../../\${base}.js\`)"`;
exports[`parse positives > basic 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("./mods/*.js")), \`./mods/\${base}.js\`)"`;
exports[`parse positives > with ../ and itself 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob("../dynamicImportVar/*.js")), \`./\${name}.js\`)"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ async function run(input: string) {
(await transformDynamicImport(
input,
normalizePath(resolve(__dirname, 'index.js')),
(id) => id.replace('@', resolve(__dirname, './mods/')),
(id) =>
id
.replace('@', resolve(__dirname, './mods/'))
.replace('#', resolve(__dirname, '../../')),
__dirname,
)) || {}
return `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`
Expand All @@ -27,6 +30,10 @@ describe('parse positives', () => {
expect(await run('`@/${base}.js`')).toMatchSnapshot()
})

it('alias path with multi ../', async () => {
expect(await run('`#/${base}.js`')).toMatchSnapshot()
})

it('with query', async () => {
expect(await run('`./mods/${base}.js?foo=bar`')).toMatchSnapshot()
})
Expand Down
13 changes: 7 additions & 6 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ export async function transformDynamicImport(
if (!resolvedFileName) {
return null
}
const relativeFileName = posix.relative(
posix.dirname(normalizePath(importer)),
normalizePath(resolvedFileName),
)
importSource = normalizePath(
'`' + (relativeFileName[0] === '.' ? '' : './') + relativeFileName + '`',
const relativeFileName = normalizePath(
posix.relative(
posix.dirname(normalizePath(importer)),
normalizePath(resolvedFileName),
),
)
importSource =
'`' + (relativeFileName[0] === '.' ? '' : './') + relativeFileName + '`'
}

const dynamicImportPattern = parseDynamicImportPattern(importSource)
Expand Down

0 comments on commit f71ba5b

Please sign in to comment.