From 42fee9ee87211dee5cf33f9e18d0abaa331e7936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 2 Nov 2023 22:29:48 +0900 Subject: [PATCH] fix: don't add `?import` for `@vite-ignore`ed import (#14851) --- .../vite/src/node/plugins/importAnalysis.ts | 15 +++++++------ .../__tests__/dynamic-import.spec.ts | 22 ++++++++++++++----- playground/dynamic-import/nested/index.js | 2 -- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 0428159f7d1429..aced11d305d9c5 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -624,12 +624,12 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { server.warmupRequest(url, { ssr }) } } else if (!importer.startsWith(withTrailingSlash(clientDir))) { + // check @vite-ignore which suppresses dynamic import warning + const hasViteIgnore = hasViteIgnoreRE.test( + // complete expression inside parens + source.slice(dynamicIndex + 1, end), + ) if (!isInNodeModules(importer)) { - // check @vite-ignore which suppresses dynamic import warning - const hasViteIgnore = hasViteIgnoreRE.test( - // complete expression inside parens - source.slice(dynamicIndex + 1, end), - ) if (!hasViteIgnore) { this.warn( `\n` + @@ -652,8 +652,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { if (!ssr) { const url = rawUrl.replace(cleanUpRawUrlRE, '').trim() if ( - !urlIsStringRE.test(url) || - isExplicitImportRequired(url.slice(1, -1)) + !hasViteIgnore && + (!urlIsStringRE.test(url) || + isExplicitImportRequired(url.slice(1, -1))) ) { needQueryInjectHelper = true str().overwrite( diff --git a/playground/dynamic-import/__tests__/dynamic-import.spec.ts b/playground/dynamic-import/__tests__/dynamic-import.spec.ts index d81317678bc6f3..55f24cd71c1f53 100644 --- a/playground/dynamic-import/__tests__/dynamic-import.spec.ts +++ b/playground/dynamic-import/__tests__/dynamic-import.spec.ts @@ -1,5 +1,12 @@ import { expect, test } from 'vitest' -import { getColor, isBuild, page, serverLogs, untilUpdated } from '~utils' +import { + getColor, + isBuild, + isServe, + page, + serverLogs, + untilUpdated, +} from '~utils' test('should load literal dynamic import', async () => { await page.click('.baz') @@ -30,11 +37,14 @@ test('should have same reference on static and dynamic js import, .mxd', async ( await untilUpdated(() => page.textContent('.view'), 'true', true) }) -// in this case, it is not possible to detect the correct module -test('should have same reference on static and dynamic js import, .mxd2', async () => { - await page.click('.mxd2') - await untilUpdated(() => page.textContent('.view'), 'false', true) -}) +// in this case, it is not possible to detect the correct module in build +test.runIf(isServe)( + 'should have same reference on static and dynamic js import, .mxd2', + async () => { + await page.click('.mxd2') + await untilUpdated(() => page.textContent('.view'), 'true') + }, +) test('should have same reference on static and dynamic js import, .mxdjson', async () => { await page.click('.mxdjson') diff --git a/playground/dynamic-import/nested/index.js b/playground/dynamic-import/nested/index.js index 25a6426e3f7ee1..edad15d51f11e8 100644 --- a/playground/dynamic-import/nested/index.js +++ b/playground/dynamic-import/nested/index.js @@ -33,8 +33,6 @@ document.querySelector('.mxd').addEventListener('click', async () => { document.querySelector('.mxd2').addEventListener('click', async () => { const test = { jss: '../files/mxd.js' } - const ttest = test - const view = 'mxd' const { default: mxdDynamic } = await import(/*@vite-ignore*/ test.jss) text('.view', mxdStatic === mxdDynamic) })