Skip to content

Commit 84c5ff6

Browse files
authored
fix: injectQuery check with double slash in the url (#14910)
1 parent 7c240a0 commit 84c5ff6

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import MagicString from 'magic-string'
66
import type { ExportSpecifier, ImportSpecifier } from 'es-module-lexer'
77
import { init, parse as parseImports } from 'es-module-lexer'
88
import { parse as parseJS } from 'acorn'
9+
import { stripLiteral } from 'strip-literal'
910
import type { Node } from 'estree'
1011
import { findStaticImports, parseStaticImport } from 'mlly'
1112
import { makeLegalIdentifier } from '@rollup/pluginutils'
@@ -74,7 +75,13 @@ const hasImportInQueryParamsRE = /[?&]import=?\b/
7475

7576
export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//
7677

77-
const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
78+
const trimWhitespaceRE = /^(\s*)(\S|\S[\s\S]*\S)\s*$/
79+
const trimWhitespaceAndComments = (code: string) => {
80+
const cleanedCode = stripLiteral(code)
81+
const match = trimWhitespaceRE.exec(cleanedCode)
82+
return match ? code.slice(match[1].length, match[2].length) : code
83+
}
84+
7885
const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/
7986

8087
const templateLiteralRE = /^\s*`(.*)`\s*$/
@@ -650,16 +657,17 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
650657
}
651658

652659
if (!ssr) {
653-
const url = rawUrl.replace(cleanUpRawUrlRE, '').trim()
660+
const url = trimWhitespaceAndComments(rawUrl)
654661
if (
655662
!urlIsStringRE.test(url) ||
656663
isExplicitImportRequired(url.slice(1, -1))
657664
) {
658665
needQueryInjectHelper = true
666+
// Use rawUrl to avoid removing comments like @vite-ignore
659667
str().overwrite(
660668
start,
661669
end,
662-
`__vite__injectQuery(${url}, 'import')`,
670+
`__vite__injectQuery(${rawUrl}, 'import')`,
663671
{ contentOnly: true },
664672
)
665673
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ test('should load dynamic import with vars ignored', async () => {
8383
).toBe(false)
8484
})
8585

86+
test('should load dynamic import with double slash ignored', async () => {
87+
await untilUpdated(
88+
() => page.textContent('.dynamic-import-with-double-slash-ignored'),
89+
'hello',
90+
true,
91+
)
92+
})
93+
8694
test('should load dynamic import with vars multiline', async () => {
8795
await untilUpdated(
8896
() => page.textContent('.dynamic-import-with-vars-multiline'),

playground/dynamic-import/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<p>dynamic-import-with-vars-ignored</p>
1717
<div class="dynamic-import-with-vars-ignored">todo</div>
1818

19+
<p>dynamic-import-with-double-slash-ignored</p>
20+
<div class="dynamic-import-with-double-slash-ignored">todo</div>
21+
1922
<p>dynamic-import-with-vars-multiline</p>
2023
<div class="dynamic-import-with-vars-multiline">todo</div>
2124

playground/dynamic-import/nested/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ import(/*@vite-ignore*/ `https://localhost`).catch((mod) => {
8989
text('.dynamic-import-with-vars-ignored', 'hello')
9090
})
9191

92+
import(/*@vite-ignore*/ `https://localhost//${'test'}`).catch((mod) => {
93+
console.log(mod)
94+
text('.dynamic-import-with-double-slash-ignored', 'hello')
95+
})
96+
9297
// prettier-ignore
9398
import(
9499
/* this messes with */

0 commit comments

Comments
 (0)