Skip to content

Commit

Permalink
fix: injectQuery check with double slash in the url (#14910)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Nov 9, 2023
1 parent 7c240a0 commit 84c5ff6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import MagicString from 'magic-string'
import type { ExportSpecifier, ImportSpecifier } from 'es-module-lexer'
import { init, parse as parseImports } from 'es-module-lexer'
import { parse as parseJS } from 'acorn'
import { stripLiteral } from 'strip-literal'
import type { Node } from 'estree'
import { findStaticImports, parseStaticImport } from 'mlly'
import { makeLegalIdentifier } from '@rollup/pluginutils'
Expand Down Expand Up @@ -74,7 +75,13 @@ const hasImportInQueryParamsRE = /[?&]import=?\b/

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

const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
const trimWhitespaceRE = /^(\s*)(\S|\S[\s\S]*\S)\s*$/
const trimWhitespaceAndComments = (code: string) => {
const cleanedCode = stripLiteral(code)
const match = trimWhitespaceRE.exec(cleanedCode)
return match ? code.slice(match[1].length, match[2].length) : code
}

const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/

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

if (!ssr) {
const url = rawUrl.replace(cleanUpRawUrlRE, '').trim()
const url = trimWhitespaceAndComments(rawUrl)
if (
!urlIsStringRE.test(url) ||
isExplicitImportRequired(url.slice(1, -1))
) {
needQueryInjectHelper = true
// Use rawUrl to avoid removing comments like @vite-ignore
str().overwrite(
start,
end,
`__vite__injectQuery(${url}, 'import')`,
`__vite__injectQuery(${rawUrl}, 'import')`,
{ contentOnly: true },
)
}
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 @@ -83,6 +83,14 @@ test('should load dynamic import with vars ignored', async () => {
).toBe(false)
})

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

test('should load dynamic import with vars multiline', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-with-vars-multiline'),
Expand Down
3 changes: 3 additions & 0 deletions playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<p>dynamic-import-with-vars-ignored</p>
<div class="dynamic-import-with-vars-ignored">todo</div>

<p>dynamic-import-with-double-slash-ignored</p>
<div class="dynamic-import-with-double-slash-ignored">todo</div>

<p>dynamic-import-with-vars-multiline</p>
<div class="dynamic-import-with-vars-multiline">todo</div>

Expand Down
5 changes: 5 additions & 0 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ import(/*@vite-ignore*/ `https://localhost`).catch((mod) => {
text('.dynamic-import-with-vars-ignored', 'hello')
})

import(/*@vite-ignore*/ `https://localhost//${'test'}`).catch((mod) => {
console.log(mod)
text('.dynamic-import-with-double-slash-ignored', 'hello')
})

// prettier-ignore
import(
/* this messes with */
Expand Down

0 comments on commit 84c5ff6

Please sign in to comment.