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: support multiline dynamic imports #9314

Merged
merged 5 commits into from
Jul 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createFilter,
normalizePath,
parseRequest,
removeComments,
requestQuerySplitRE,
transformStableResult
} from '../utils'
Expand Down Expand Up @@ -176,11 +177,13 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
s ||= new MagicString(source)
let result
try {
result = await transformDynamicImport(
source.slice(start, end),
importer,
resolve
)
// When import string is using backticks, es-module-lexer `end` captures
// until the closing parenthesis, instead of the closing backtick.
// There may be inline comments between the backtick and the closing
// parenthesis, so we manually remove them for now.
// See https://github.com/guybedford/es-module-lexer/issues/118
const importSource = removeComments(source.slice(start, end)).trim()
result = await transformDynamicImport(importSource, importer, resolve)
} catch (error) {
if (warnOnError) {
this.warn(error)
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,10 @@ export function emptyCssComments(raw: string): string {
return raw.replace(multilineCommentsRE, (s) => ' '.repeat(s.length))
}

export function removeComments(raw: string): string {
return raw.replace(multilineCommentsRE, '').replace(singlelineCommentsRE, '')
}

function mergeConfigRecursively(
defaults: Record<string, any>,
overrides: Record<string, any>,
Expand Down
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 @@ -68,6 +68,14 @@ test('should load dynamic import with vars', async () => {
)
})

test('should load dynamic import with vars multiline', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-with-vars'),
'hello',
true
)
})

test('should load dynamic import with vars alias', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-with-vars-alias'),
Expand Down
9 changes: 9 additions & 0 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ import(`../alias/${base}.js`).then((mod) => {
text('.dynamic-import-with-vars', mod.hello())
})

// prettier-ignore
import(
/* this messes with */
`../alias/${base}.js`
/* es-module-lexer */
).then((mod) => {
text('.dynamic-import-with-vars-multiline', mod.hello())
})

import(`../alias/${base}.js?raw`).then((mod) => {
text('.dynamic-import-with-vars-raw', JSON.stringify(mod))
})
Expand Down