Skip to content

Commit

Permalink
fix(dynamicImportVars): correct glob pattern for paths with parenthes…
Browse files Browse the repository at this point in the history
…es (#17940)

Co-authored-by: 翠 / green <green@sapphi.red>
  • Loading branch information
sunnylost and sapphi-red authored Sep 9, 2024
1 parent 2adc5aa commit 2a391a7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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}`
Expand Down
3 changes: 3 additions & 0 deletions playground/dynamic-import/(app)/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hello() {
return 'dynamic-import-with-vars-contains-parenthesis'
}
6 changes: 6 additions & 0 deletions playground/dynamic-import/(app)/nest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const base = 'main'
import(`../${base}.js`).then((mod) => {
document.querySelector(
'.dynamic-import-with-vars-contains-parenthesis',
).textContent = mod.hello()
})
8 changes: 8 additions & 0 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 4 additions & 0 deletions playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<p>dynamic-import-with-vars-worker</p>
<div class="dynamic-import-with-vars-worker">todo</div>

<p>dynamic-import-with-vars-contains-parenthesis</p>
<div class="dynamic-import-with-vars-contains-parenthesis">todo</div>

<div class="view"></div>

<div class="dynamic-import-self"></div>
Expand All @@ -43,6 +46,7 @@
<div class="dynamic-import-nested-self"></div>

<script type="module" src="./nested/index.js"></script>
<script type="module" src="./(app)/nest/index.js"></script>
<style>
p {
color: #0088ff;
Expand Down

0 comments on commit 2a391a7

Please sign in to comment.