diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index e6dcb756bc035a..145f6130a55708 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -19,7 +19,6 @@ import { } from '../utils' import type { Environment } from '../environment' import { usePerEnvironmentState } from '../environment' -import { toAbsoluteGlob } from './importMetaGlob' import { hasViteIgnoreRE } from './importAnalysis' import { workerOrSharedWorkerRE } from './worker' @@ -144,11 +143,13 @@ export async function transformDynamicImport( } const { globParams, rawPattern, userPattern } = dynamicImportPattern const params = globParams ? `, ${JSON.stringify(globParams)}` : '' + const dir = importer ? posix.dirname(importer) : root + const normalized = + rawPattern[0] === '/' + ? posix.join(root, rawPattern.slice(1)) + : posix.join(dir, rawPattern) - let newRawPattern = posix.relative( - posix.dirname(importer), - await toAbsoluteGlob(rawPattern, root, importer, resolve), - ) + let newRawPattern = posix.relative(posix.dirname(importer), normalized) if (!relativePathRE.test(newRawPattern)) { newRawPattern = `./${newRawPattern}` diff --git a/playground/dynamic-import/(app)/main.js b/playground/dynamic-import/(app)/main.js new file mode 100644 index 00000000000000..48f83a961a109a --- /dev/null +++ b/playground/dynamic-import/(app)/main.js @@ -0,0 +1,3 @@ +export function hello() { + return 'dynamic-import-with-vars-contains-parenthesis' +} diff --git a/playground/dynamic-import/(app)/nest/index.js b/playground/dynamic-import/(app)/nest/index.js new file mode 100644 index 00000000000000..f2e2ca17f05336 --- /dev/null +++ b/playground/dynamic-import/(app)/nest/index.js @@ -0,0 +1,6 @@ +const base = 'main' +import(`../${base}.js`).then((mod) => { + document.querySelector( + '.dynamic-import-with-vars-contains-parenthesis', + ).textContent = mod.hello() +}) diff --git a/playground/dynamic-import/__tests__/dynamic-import.spec.ts b/playground/dynamic-import/__tests__/dynamic-import.spec.ts index b94b712de37c3e..a1a843a0b04f29 100644 --- a/playground/dynamic-import/__tests__/dynamic-import.spec.ts +++ b/playground/dynamic-import/__tests__/dynamic-import.spec.ts @@ -160,6 +160,14 @@ test('should work with load ../ and contain itself directory', async () => { ) }) +test('should work a load path that contains parentheses.', async () => { + await untilUpdated( + () => page.textContent('.dynamic-import-with-vars-contains-parenthesis'), + 'dynamic-import-with-vars-contains-parenthesis', + true, + ) +}) + test.runIf(isBuild)( 'should rollup warn when static and dynamic import a module in same chunk', async () => { diff --git a/playground/dynamic-import/index.html b/playground/dynamic-import/index.html index 81f2d8f8ed0fea..1289efc3e1ac4e 100644 --- a/playground/dynamic-import/index.html +++ b/playground/dynamic-import/index.html @@ -34,6 +34,9 @@
dynamic-import-with-vars-worker
dynamic-import-with-vars-contains-parenthesis
+