@@ -82,6 +82,8 @@ export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//
8282const cleanUpRawUrlRE = / \/ \* [ \s \S ] * ?\* \/ | ( [ ^ \\ : ] | ^ ) \/ \/ .* $ / gm
8383const urlIsStringRE = / ^ (?: ' .* ' | " .* " | ` .* ` ) $ /
8484
85+ const templateLiteralRE = / ^ \s * ` ( .* ) ` \s * $ /
86+
8587interface UrlPosition {
8688 url : string
8789 start : number
@@ -426,12 +428,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
426428 ss : expStart ,
427429 se : expEnd ,
428430 d : dynamicIndex ,
429- // #2083 User may use escape path,
430- // so use imports[index].n to get the unescaped string
431- n : specifier ,
432431 a : assertIndex ,
433432 } = importSpecifier
434433
434+ // #2083 User may use escape path,
435+ // so use imports[index].n to get the unescaped string
436+ let specifier = importSpecifier . n
437+
435438 const rawUrl = source . slice ( start , end )
436439
437440 // check import.meta usage
@@ -469,6 +472,14 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
469472 hasEnv = true
470473 }
471474 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+ }
472483 }
473484
474485 const isDynamicImport = dynamicIndex > - 1
0 commit comments