@@ -82,6 +82,8 @@ export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//
82
82
const cleanUpRawUrlRE = / \/ \* [ \s \S ] * ?\* \/ | ( [ ^ \\ : ] | ^ ) \/ \/ .* $ / gm
83
83
const urlIsStringRE = / ^ (?: ' .* ' | " .* " | ` .* ` ) $ /
84
84
85
+ const templateLiteralRE = / ^ \s * ` ( .* ) ` \s * $ /
86
+
85
87
interface UrlPosition {
86
88
url : string
87
89
start : number
@@ -426,12 +428,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
426
428
ss : expStart ,
427
429
se : expEnd ,
428
430
d : dynamicIndex ,
429
- // #2083 User may use escape path,
430
- // so use imports[index].n to get the unescaped string
431
- n : specifier ,
432
431
a : assertIndex ,
433
432
} = importSpecifier
434
433
434
+ // #2083 User may use escape path,
435
+ // so use imports[index].n to get the unescaped string
436
+ let specifier = importSpecifier . n
437
+
435
438
const rawUrl = source . slice ( start , end )
436
439
437
440
// check import.meta usage
@@ -469,6 +472,14 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
469
472
hasEnv = true
470
473
}
471
474
return
475
+ } else if ( templateLiteralRE . test ( rawUrl ) ) {
476
+ // If the import has backticks but isn't transformed as a glob import
477
+ // (as there's nothing to glob), check if it's simply a plain string.
478
+ // If so, we can replace the specifier as a plain string to prevent
479
+ // an incorrect "cannot be analyzed" warning.
480
+ if ( ! ( rawUrl . includes ( '${' ) && rawUrl . includes ( '}' ) ) ) {
481
+ specifier = rawUrl . replace ( templateLiteralRE , '$1' )
482
+ }
472
483
}
473
484
474
485
const isDynamicImport = dynamicIndex > - 1
0 commit comments