From 8880bc5a9a09fcd1a2e0590048c0c61e47a43b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 29 May 2024 23:17:53 +0900 Subject: [PATCH] feat(asset): support `/*@vite-ignore*/` for `new URL(, import.meta.url)` (#16590) --- packages/vite/src/node/plugins/assetImportMetaUrl.ts | 6 +++++- packages/vite/src/node/plugins/workerImportMetaUrl.ts | 3 +-- playground/assets/__tests__/assets.spec.ts | 6 +++++- playground/assets/index.html | 7 +++++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/plugins/assetImportMetaUrl.ts b/packages/vite/src/node/plugins/assetImportMetaUrl.ts index fb0b45e9d2a937..588f6e08b07c3d 100644 --- a/packages/vite/src/node/plugins/assetImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/assetImportMetaUrl.ts @@ -11,6 +11,7 @@ import { fileToUrl } from './asset' import { preloadHelperId } from './importAnalysisBuild' import type { InternalResolveOptions } from './resolve' import { tryFsResolve } from './resolve' +import { hasViteIgnoreRE } from './importAnalysis' /** * Convert `new URL('./foo.png', import.meta.url)` to its resolved built URL @@ -54,6 +55,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { let match: RegExpExecArray | null while ((match = assetImportMetaUrlRE.exec(cleanString))) { const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices! + if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue + const rawUrl = code.slice(urlStart, urlEnd) if (!s) s = new MagicString(code) @@ -134,7 +137,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { if (!builtUrl) { const rawExp = code.slice(startIndex, endIndex) config.logger.warnOnce( - `\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime`, + `\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime. ` + + `If this is intended, you can use the /* @vite-ignore */ comment to suppress this warning.`, ) builtUrl = url } diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index 0a7b34d4ff3dc8..22a5de2ec67b06 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -185,8 +185,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin { s.update( expStart, expEnd, - // add `'' +` to skip vite:asset-import-meta-url plugin - `new URL('' + ${JSON.stringify(builtUrl)}, import.meta.url)`, + `new URL(/* @vite-ignore */ ${JSON.stringify(builtUrl)}, import.meta.url)`, ) } } diff --git a/playground/assets/__tests__/assets.spec.ts b/playground/assets/__tests__/assets.spec.ts index 5cf7e7c9dd62fc..6f193cc43cd9ca 100644 --- a/playground/assets/__tests__/assets.spec.ts +++ b/playground/assets/__tests__/assets.spec.ts @@ -14,6 +14,7 @@ import { page, readFile, readManifest, + serverLogs, untilUpdated, viteTestUrl, watcher, @@ -464,7 +465,7 @@ test('new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)', async () ) }) -test('new URL(`non-existent`, import.meta.url)', async () => { +test("new URL(/* @vite-ignore */ 'non-existent', import.meta.url)", async () => { // the inlined script tag is extracted in a separate file const importMetaUrl = new URL( isBuild ? '/foo/bar/assets/index.js' : '/foo/bar/index.html', @@ -473,6 +474,9 @@ test('new URL(`non-existent`, import.meta.url)', async () => { expect(await page.textContent('.non-existent-import-meta-url')).toMatch( new URL('non-existent', importMetaUrl).pathname, ) + expect(serverLogs).not.toContainEqual( + expect.stringContaining("doesn't exist at build time"), + ) }) test.runIf(isBuild)('manifest', async () => { diff --git a/playground/assets/index.html b/playground/assets/index.html index 1da86cc482976a..2fd63f46f63960 100644 --- a/playground/assets/index.html +++ b/playground/assets/index.html @@ -304,7 +304,7 @@

new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)

-

new URL(`non-existent`, import.meta.url)

+

new URL(/* @vite-ignore */ 'non-existent', import.meta.url)

@@ -509,7 +509,10 @@

assets in noscript

import someString from './static/foo.txt?raw' document.querySelector('.raw-query').textContent = someString - const metaUrlNonExistent = new URL('non-existent', import.meta.url).pathname + const metaUrlNonExistent = new URL( + /* @vite-ignore */ 'non-existent', + import.meta.url, + ).pathname text('.non-existent-import-meta-url', metaUrlNonExistent) /**