From 4479431312540683d488544766ba0d8334a995f4 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 7 Aug 2023 09:27:05 +0200 Subject: [PATCH] fix: dynamic import vars ignored warning (#14006) --- .../vite/src/node/plugins/dynamicImportVars.ts | 5 +++++ packages/vite/src/node/plugins/importAnalysis.ts | 2 +- .../vite/src/node/plugins/workerImportMetaUrl.ts | 5 ++--- .../__tests__/dynamic-import.spec.ts | 14 ++++++++++++++ playground/dynamic-import/index.html | 3 +++ playground/dynamic-import/nested/index.js | 5 +++++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index 45a27c6f466431..f4956046c219ff 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -17,6 +17,7 @@ import { transformStableResult, } from '../utils' import { toAbsoluteGlob } from './importMetaGlob' +import { hasViteIgnoreRE } from './importAnalysis' export const dynamicImportHelperId = '\0vite/dynamic-import-helper' @@ -206,6 +207,10 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { continue } + if (hasViteIgnoreRE.test(source.slice(expStart, expEnd))) { + continue + } + s ||= new MagicString(source) let result try { diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 12f44c8afe547f..c33cd519fb4da7 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -76,7 +76,7 @@ const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/ const hasImportInQueryParamsRE = /[?&]import=?\b/ -const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\// +export const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\// const cleanUpRawUrlRE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/ diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index 5e186af64f8648..385566c1108ec3 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -19,8 +19,7 @@ import { WORKER_FILE_ID, workerFileToUrl } from './worker' import { fileToUrl } from './asset' import type { InternalResolveOptions } from './resolve' import { tryFsResolve } from './resolve' - -const ignoreFlagRE = /\/\*\s*@vite-ignore\s*\*\// +import { hasViteIgnoreRE } from './importAnalysis' interface WorkerOptions { type?: WorkerType @@ -78,7 +77,7 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType { .substring(commaIndex + 1, endIndex) .replace(/\}[\s\S]*,/g, '}') // strip trailing comma for parsing - const hasViteIgnore = ignoreFlagRE.test(workerOptString) + const hasViteIgnore = hasViteIgnoreRE.test(workerOptString) if (hasViteIgnore) { return 'ignore' } diff --git a/playground/dynamic-import/__tests__/dynamic-import.spec.ts b/playground/dynamic-import/__tests__/dynamic-import.spec.ts index daa378da6ef1af..d81317678bc6f3 100644 --- a/playground/dynamic-import/__tests__/dynamic-import.spec.ts +++ b/playground/dynamic-import/__tests__/dynamic-import.spec.ts @@ -69,6 +69,20 @@ test('should load dynamic import with vars', async () => { ) }) +test('should load dynamic import with vars ignored', async () => { + await untilUpdated( + () => page.textContent('.dynamic-import-with-vars-ignored'), + 'hello', + true, + ) + // No warning should be logged as we are using @vite-ignore + expect( + serverLogs.some((log) => + log.includes('"https" has been externalized for browser compatibility'), + ), + ).toBe(false) +}) + test('should load dynamic import with vars multiline', async () => { await untilUpdated( () => page.textContent('.dynamic-import-with-vars-multiline'), diff --git a/playground/dynamic-import/index.html b/playground/dynamic-import/index.html index 0787955f9ca309..31cf128ddd12d1 100644 --- a/playground/dynamic-import/index.html +++ b/playground/dynamic-import/index.html @@ -13,6 +13,9 @@

dynamic-import-with-vars

todo
+

dynamic-import-with-vars-ignored

+
todo
+

dynamic-import-with-vars-multiline

todo
diff --git a/playground/dynamic-import/nested/index.js b/playground/dynamic-import/nested/index.js index 054801f451a85f..180dd6b5ad3b6b 100644 --- a/playground/dynamic-import/nested/index.js +++ b/playground/dynamic-import/nested/index.js @@ -84,6 +84,11 @@ import(`../alias/${base}.js`).then((mod) => { text('.dynamic-import-with-vars', mod.hello()) }) +import(/*@vite-ignore*/ `https://localhost`).catch((mod) => { + console.log(mod) + text('.dynamic-import-with-vars-ignored', 'hello') +}) + // prettier-ignore import( /* this messes with */