Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(asset): support /*@vite-ignore*/ for new URL(, import.meta.url) #16590

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)`,
)
}
}
Expand Down
6 changes: 5 additions & 1 deletion playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
page,
readFile,
readManifest,
serverLogs,
untilUpdated,
viteTestUrl,
watcher,
Expand Down Expand Up @@ -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',
Expand All @@ -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 () => {
Expand Down
7 changes: 5 additions & 2 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ <h2>new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)</h2>
<code class="dynamic-import-meta-url-2-ternary"></code>
</p>

<h2>new URL(`non-existent`, import.meta.url)</h2>
<h2>new URL(/* @vite-ignore */ 'non-existent', import.meta.url)</h2>
<p>
<code class="non-existent-import-meta-url"></code>
</p>
Expand Down Expand Up @@ -509,7 +509,10 @@ <h3>assets in noscript</h3>
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)

/**
Expand Down