Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dynamic-import-vars plugin normalize path issue #16518

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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