Skip to content

Commit e66cf69

Browse files
authored
fix: support multiline dynamic imports (#9314)
1 parent b82ee5d commit e66cf69

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
createFilter,
1111
normalizePath,
1212
parseRequest,
13+
removeComments,
1314
requestQuerySplitRE,
1415
transformStableResult
1516
} from '../utils'
@@ -176,11 +177,13 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
176177
s ||= new MagicString(source)
177178
let result
178179
try {
179-
result = await transformDynamicImport(
180-
source.slice(start, end),
181-
importer,
182-
resolve
183-
)
180+
// When import string is using backticks, es-module-lexer `end` captures
181+
// until the closing parenthesis, instead of the closing backtick.
182+
// There may be inline comments between the backtick and the closing
183+
// parenthesis, so we manually remove them for now.
184+
// See https://github.com/guybedford/es-module-lexer/issues/118
185+
const importSource = removeComments(source.slice(start, end)).trim()
186+
result = await transformDynamicImport(importSource, importer, resolve)
184187
} catch (error) {
185188
if (warnOnError) {
186189
this.warn(error)

packages/vite/src/node/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,10 @@ export function emptyCssComments(raw: string): string {
984984
return raw.replace(multilineCommentsRE, (s) => ' '.repeat(s.length))
985985
}
986986

987+
export function removeComments(raw: string): string {
988+
return raw.replace(multilineCommentsRE, '').replace(singlelineCommentsRE, '')
989+
}
990+
987991
function mergeConfigRecursively(
988992
defaults: Record<string, any>,
989993
overrides: Record<string, any>,

playground/dynamic-import/__tests__/dynamic-import.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ test('should load dynamic import with vars', async () => {
6868
)
6969
})
7070

71+
test('should load dynamic import with vars multiline', async () => {
72+
await untilUpdated(
73+
() => page.textContent('.dynamic-import-with-vars'),
74+
'hello',
75+
true
76+
)
77+
})
78+
7179
test('should load dynamic import with vars alias', async () => {
7280
await untilUpdated(
7381
() => page.textContent('.dynamic-import-with-vars-alias'),

playground/dynamic-import/nested/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ import(`../alias/${base}.js`).then((mod) => {
8484
text('.dynamic-import-with-vars', mod.hello())
8585
})
8686

87+
// prettier-ignore
88+
import(
89+
/* this messes with */
90+
`../alias/${base}.js`
91+
/* es-module-lexer */
92+
).then((mod) => {
93+
text('.dynamic-import-with-vars-multiline', mod.hello())
94+
})
95+
8796
import(`../alias/${base}.js?raw`).then((mod) => {
8897
text('.dynamic-import-with-vars-raw', JSON.stringify(mod))
8998
})

0 commit comments

Comments
 (0)