From 48f2cb1f1c12fa1243299f8032557de39b739efd Mon Sep 17 00:00:00 2001 From: lprnmns Date: Thu, 15 Oct 2026 18:52:02 +0300 Subject: [PATCH 1/2] fix(assetImportMetaUrl): watch referenced files that have a query or hash Co-authored-by: jackfishwick <9215166+jackfishwick@users.noreply.github.com> --- .../plugins/assetImportMetaUrl.spec.ts | 39 +++++++++++++++++++ .../src/node/plugins/assetImportMetaUrl.ts | 7 ++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts b/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts index aacc1b1c50ee90..2da9f28a7042ae 100644 --- a/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts @@ -1,3 +1,4 @@ +import path from 'node:path' import { parseAst } from 'rollup/parseAst' import { describe, expect, test } from 'vitest' import { PartialEnvironment } from '../../baseEnvironment' @@ -63,3 +64,41 @@ describe('assetImportMetaUrlPlugin', async () => { ).toMatchInlineSnapshot(`"new URL(\`\${file}.js\`, import.meta.url)"`) }) }) + +describe('assetImportMetaUrlPlugin watch files', async () => { + const config = await resolveConfig({ configFile: false }, 'serve') + const instance = assetImportMetaUrlPlugin(config) + const environment = new PartialEnvironment('client', config) + const file = path.posix.join( + import.meta.dirname.replaceAll('\\', '/'), + 'assetImportMetaUrl.spec.ts', + ) + + const getWatchFiles = async (url: string) => { + const watchFiles: string[] = [] + // @ts-expect-error transform.handler should exist + await instance.transform.handler.call( + { + environment, + parse: parseAst, + addWatchFile: (file: string) => watchFiles.push(file), + }, + `new URL('${url}', import.meta.url)`, + path.join(import.meta.dirname, 'entry.js'), + ) + return watchFiles + } + + test('watches the referenced file', async () => { + expect(await getWatchFiles('./assetImportMetaUrl.spec.ts')).toEqual([file]) + }) + + test('watches the referenced file when the url has a query or hash', async () => { + expect(await getWatchFiles('./assetImportMetaUrl.spec.ts?inline')).toEqual([ + file, + ]) + expect(await getWatchFiles('./assetImportMetaUrl.spec.ts#hash')).toEqual([ + file, + ]) + }) +}) diff --git a/packages/vite/src/node/plugins/assetImportMetaUrl.ts b/packages/vite/src/node/plugins/assetImportMetaUrl.ts index 77093a365c434c..c52f8d996cbacb 100644 --- a/packages/vite/src/node/plugins/assetImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/assetImportMetaUrl.ts @@ -2,7 +2,7 @@ import path from 'node:path' import MagicString from 'magic-string' import { exactRegex } from 'rolldown/filter' import { stripLiteral } from 'strip-literal' -import { slash } from '../../shared/utils' +import { cleanUrl, slash } from '../../shared/utils' import type { ResolvedConfig } from '../config' import { CLIENT_ENTRY } from '../constants' import { createBackCompatIdResolver } from '../idResolver' @@ -148,8 +148,9 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { } else { builtUrlExpr = await fileToUrl(this, file, 'js') // during dev, builtUrl may point to a directory or a non-existing file - if (tryStatSync(file)?.isFile()) { - this.addWatchFile(file) + const watchFile = cleanUrl(file) + if (tryStatSync(watchFile)?.isFile()) { + this.addWatchFile(watchFile) } } } catch { From bc14cfd0d5a78644ac6425982a0c97d99e18a97b Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Mon, 26 Oct 2026 11:32:47 +0900 Subject: [PATCH 2/2] test: simplify --- .../plugins/assetImportMetaUrl.spec.ts | 65 ++++++++----------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts b/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts index 2da9f28a7042ae..ae5864db3639d2 100644 --- a/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts @@ -4,18 +4,21 @@ import { describe, expect, test } from 'vitest' import { PartialEnvironment } from '../../baseEnvironment' import { resolveConfig } from '../../config' import { assetImportMetaUrlPlugin } from '../../plugins/assetImportMetaUrl' +import { normalizePath } from '../../utils' -async function createAssetImportMetaurlPluginTransform() { +async function createAssetImportMetaurlPluginTransform( + addWatchFile?: (file: string) => void, +) { const config = await resolveConfig({ configFile: false }, 'serve') const instance = assetImportMetaUrlPlugin(config) const environment = new PartialEnvironment('client', config) - return async (code: string) => { + return async (code: string, id = 'foo.ts') => { // @ts-expect-error transform.handler should exist const result = await instance.transform.handler.call( - { environment, parse: parseAst }, + { environment, parse: parseAst, addWatchFile }, code, - 'foo.ts', + id, ) return result?.code || result } @@ -65,40 +68,28 @@ describe('assetImportMetaUrlPlugin', async () => { }) }) -describe('assetImportMetaUrlPlugin watch files', async () => { - const config = await resolveConfig({ configFile: false }, 'serve') - const instance = assetImportMetaUrlPlugin(config) - const environment = new PartialEnvironment('client', config) - const file = path.posix.join( - import.meta.dirname.replaceAll('\\', '/'), - 'assetImportMetaUrl.spec.ts', +describe('assetImportMetaUrlPlugin watch files', () => { + const file = normalizePath( + path.join(import.meta.dirname, 'assetImportMetaUrl.spec.ts'), ) - const getWatchFiles = async (url: string) => { - const watchFiles: string[] = [] - // @ts-expect-error transform.handler should exist - await instance.transform.handler.call( - { - environment, - parse: parseAst, - addWatchFile: (file: string) => watchFiles.push(file), - }, - `new URL('${url}', import.meta.url)`, - path.join(import.meta.dirname, 'entry.js'), - ) - return watchFiles - } + const cases = [ + './assetImportMetaUrl.spec.ts', + './assetImportMetaUrl.spec.ts?inline', + './assetImportMetaUrl.spec.ts#hash', + ] - test('watches the referenced file', async () => { - expect(await getWatchFiles('./assetImportMetaUrl.spec.ts')).toEqual([file]) - }) - - test('watches the referenced file when the url has a query or hash', async () => { - expect(await getWatchFiles('./assetImportMetaUrl.spec.ts?inline')).toEqual([ - file, - ]) - expect(await getWatchFiles('./assetImportMetaUrl.spec.ts#hash')).toEqual([ - file, - ]) - }) + for (const url of cases) { + test(`watches the referenced file for ${url}`, async () => { + const watchFiles: string[] = [] + const transform = await createAssetImportMetaurlPluginTransform((file) => + watchFiles.push(file), + ) + await transform( + `new URL('${url}', import.meta.url)`, + path.join(import.meta.dirname, 'entry.js'), + ) + expect(watchFiles).toStrictEqual([file]) + }) + } })