Skip to content

Commit 4479431

Browse files
authoredAug 7, 2023
fix: dynamic import vars ignored warning (#14006)
1 parent a1b519e commit 4479431

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed
 

‎packages/vite/src/node/plugins/dynamicImportVars.ts

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
transformStableResult,
1818
} from '../utils'
1919
import { toAbsoluteGlob } from './importMetaGlob'
20+
import { hasViteIgnoreRE } from './importAnalysis'
2021

2122
export const dynamicImportHelperId = '\0vite/dynamic-import-helper'
2223

@@ -206,6 +207,10 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
206207
continue
207208
}
208209

210+
if (hasViteIgnoreRE.test(source.slice(expStart, expEnd))) {
211+
continue
212+
}
213+
209214
s ||= new MagicString(source)
210215
let result
211216
try {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/
7676

7777
const hasImportInQueryParamsRE = /[?&]import=?\b/
7878

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

8181
const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm
8282
const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/

‎packages/vite/src/node/plugins/workerImportMetaUrl.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import { WORKER_FILE_ID, workerFileToUrl } from './worker'
1919
import { fileToUrl } from './asset'
2020
import type { InternalResolveOptions } from './resolve'
2121
import { tryFsResolve } from './resolve'
22-
23-
const ignoreFlagRE = /\/\*\s*@vite-ignore\s*\*\//
22+
import { hasViteIgnoreRE } from './importAnalysis'
2423

2524
interface WorkerOptions {
2625
type?: WorkerType
@@ -78,7 +77,7 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType {
7877
.substring(commaIndex + 1, endIndex)
7978
.replace(/\}[\s\S]*,/g, '}') // strip trailing comma for parsing
8079

81-
const hasViteIgnore = ignoreFlagRE.test(workerOptString)
80+
const hasViteIgnore = hasViteIgnoreRE.test(workerOptString)
8281
if (hasViteIgnore) {
8382
return 'ignore'
8483
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ test('should load dynamic import with vars', async () => {
6969
)
7070
})
7171

72+
test('should load dynamic import with vars ignored', async () => {
73+
await untilUpdated(
74+
() => page.textContent('.dynamic-import-with-vars-ignored'),
75+
'hello',
76+
true,
77+
)
78+
// No warning should be logged as we are using @vite-ignore
79+
expect(
80+
serverLogs.some((log) =>
81+
log.includes('"https" has been externalized for browser compatibility'),
82+
),
83+
).toBe(false)
84+
})
85+
7286
test('should load dynamic import with vars multiline', async () => {
7387
await untilUpdated(
7488
() => page.textContent('.dynamic-import-with-vars-multiline'),

‎playground/dynamic-import/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<p>dynamic-import-with-vars</p>
1414
<div class="dynamic-import-with-vars">todo</div>
1515

16+
<p>dynamic-import-with-vars-ignored</p>
17+
<div class="dynamic-import-with-vars-ignored">todo</div>
18+
1619
<p>dynamic-import-with-vars-multiline</p>
1720
<div class="dynamic-import-with-vars-multiline">todo</div>
1821

‎playground/dynamic-import/nested/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ import(`../alias/${base}.js`).then((mod) => {
8484
text('.dynamic-import-with-vars', mod.hello())
8585
})
8686

87+
import(/*@vite-ignore*/ `https://localhost`).catch((mod) => {
88+
console.log(mod)
89+
text('.dynamic-import-with-vars-ignored', 'hello')
90+
})
91+
8792
// prettier-ignore
8893
import(
8994
/* this messes with */

0 commit comments

Comments
 (0)
Please sign in to comment.