Skip to content

Commit 8880bc5

Browse files
authored
feat(asset): support /*@vite-ignore*/ for new URL(, import.meta.url) (#16590)
1 parent 5f13bf8 commit 8880bc5

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

packages/vite/src/node/plugins/assetImportMetaUrl.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { fileToUrl } from './asset'
1111
import { preloadHelperId } from './importAnalysisBuild'
1212
import type { InternalResolveOptions } from './resolve'
1313
import { tryFsResolve } from './resolve'
14+
import { hasViteIgnoreRE } from './importAnalysis'
1415

1516
/**
1617
* Convert `new URL('./foo.png', import.meta.url)` to its resolved built URL
@@ -54,6 +55,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
5455
let match: RegExpExecArray | null
5556
while ((match = assetImportMetaUrlRE.exec(cleanString))) {
5657
const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices!
58+
if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue
59+
5760
const rawUrl = code.slice(urlStart, urlEnd)
5861

5962
if (!s) s = new MagicString(code)
@@ -134,7 +137,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
134137
if (!builtUrl) {
135138
const rawExp = code.slice(startIndex, endIndex)
136139
config.logger.warnOnce(
137-
`\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime`,
140+
`\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime. ` +
141+
`If this is intended, you can use the /* @vite-ignore */ comment to suppress this warning.`,
138142
)
139143
builtUrl = url
140144
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
185185
s.update(
186186
expStart,
187187
expEnd,
188-
// add `'' +` to skip vite:asset-import-meta-url plugin
189-
`new URL('' + ${JSON.stringify(builtUrl)}, import.meta.url)`,
188+
`new URL(/* @vite-ignore */ ${JSON.stringify(builtUrl)}, import.meta.url)`,
190189
)
191190
}
192191
}

playground/assets/__tests__/assets.spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
page,
1515
readFile,
1616
readManifest,
17+
serverLogs,
1718
untilUpdated,
1819
viteTestUrl,
1920
watcher,
@@ -464,7 +465,7 @@ test('new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)', async ()
464465
)
465466
})
466467

467-
test('new URL(`non-existent`, import.meta.url)', async () => {
468+
test("new URL(/* @vite-ignore */ 'non-existent', import.meta.url)", async () => {
468469
// the inlined script tag is extracted in a separate file
469470
const importMetaUrl = new URL(
470471
isBuild ? '/foo/bar/assets/index.js' : '/foo/bar/index.html',
@@ -473,6 +474,9 @@ test('new URL(`non-existent`, import.meta.url)', async () => {
473474
expect(await page.textContent('.non-existent-import-meta-url')).toMatch(
474475
new URL('non-existent', importMetaUrl).pathname,
475476
)
477+
expect(serverLogs).not.toContainEqual(
478+
expect.stringContaining("doesn't exist at build time"),
479+
)
476480
})
477481

478482
test.runIf(isBuild)('manifest', async () => {

playground/assets/index.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ <h2>new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)</h2>
304304
<code class="dynamic-import-meta-url-2-ternary"></code>
305305
</p>
306306

307-
<h2>new URL(`non-existent`, import.meta.url)</h2>
307+
<h2>new URL(/* @vite-ignore */ 'non-existent', import.meta.url)</h2>
308308
<p>
309309
<code class="non-existent-import-meta-url"></code>
310310
</p>
@@ -509,7 +509,10 @@ <h3>assets in noscript</h3>
509509
import someString from './static/foo.txt?raw'
510510
document.querySelector('.raw-query').textContent = someString
511511

512-
const metaUrlNonExistent = new URL('non-existent', import.meta.url).pathname
512+
const metaUrlNonExistent = new URL(
513+
/* @vite-ignore */ 'non-existent',
514+
import.meta.url,
515+
).pathname
513516
text('.non-existent-import-meta-url', metaUrlNonExistent)
514517

515518
/**

0 commit comments

Comments
 (0)