Skip to content

Commit ec7ee22

Browse files
Dunqingsapphi-red
andauthored
fix(analysis): warnings for dynamic imports that use static template literals (#14458)
Co-authored-by: 翠 / green <green@sapphi.red>
1 parent 6f9d39d commit ec7ee22

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//
7979
const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
8080
const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/
8181

82+
const templateLiteralRE = /^\s*`(.*)`\s*$/
83+
8284
interface UrlPosition {
8385
url: string
8486
start: number
@@ -423,12 +425,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
423425
ss: expStart,
424426
se: expEnd,
425427
d: dynamicIndex,
426-
// #2083 User may use escape path,
427-
// so use imports[index].n to get the unescaped string
428-
n: specifier,
429428
a: assertIndex,
430429
} = importSpecifier
431430

431+
// #2083 User may use escape path,
432+
// so use imports[index].n to get the unescaped string
433+
let specifier = importSpecifier.n
434+
432435
const rawUrl = source.slice(start, end)
433436

434437
// check import.meta usage
@@ -466,6 +469,14 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
466469
hasEnv = true
467470
}
468471
return
472+
} else if (templateLiteralRE.test(rawUrl)) {
473+
// If the import has backticks but isn't transformed as a glob import
474+
// (as there's nothing to glob), check if it's simply a plain string.
475+
// If so, we can replace the specifier as a plain string to prevent
476+
// an incorrect "cannot be analyzed" warning.
477+
if (!(rawUrl.includes('${') && rawUrl.includes('}'))) {
478+
specifier = rawUrl.replace(templateLiteralRE, '$1')
479+
}
469480
}
470481

471482
const isDynamicImport = dynamicIndex > -1

playground/dynamic-import/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
<div class="dynamic-import-self"></div>
3737

38+
<div class="dynamic-import-static"></div>
39+
3840
<div class="dynamic-import-nested-self"></div>
3941

4042
<script type="module" src="./nested/index.js"></script>

playground/dynamic-import/nested/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,8 @@ import(`../nested/nested/${base}.js`).then((mod) => {
131131
text('.dynamic-import-nested-self', mod.self)
132132
})
133133

134+
import(`../nested/static.js`).then((mod) => {
135+
text('.dynamic-import-static', mod.self)
136+
})
137+
134138
console.log('index.js')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const self = 'dynamic-import-static'

0 commit comments

Comments
 (0)