Skip to content

Commit e954a9c

Browse files
fix: watch files that have a query or hash referenced with new URL(..., import.meta.url) (#23780)
Co-authored-by: jackfishwick <[email protected]>
1 parent 2ed1031 commit e954a9c

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1+
import path from 'node:path'
12
import { parseAst } from 'rollup/parseAst'
23
import { describe, expect, test } from 'vitest'
34
import { PartialEnvironment } from '../../baseEnvironment'
45
import { resolveConfig } from '../../config'
56
import { assetImportMetaUrlPlugin } from '../../plugins/assetImportMetaUrl'
7+
import { normalizePath } from '../../utils'
68

7-
async function createAssetImportMetaurlPluginTransform() {
9+
async function createAssetImportMetaurlPluginTransform(
10+
addWatchFile?: (file: string) => void,
11+
) {
812
const config = await resolveConfig({ configFile: false }, 'serve')
913
const instance = assetImportMetaUrlPlugin(config)
1014
const environment = new PartialEnvironment('client', config)
1115

12-
return async (code: string) => {
16+
return async (code: string, id = 'foo.ts') => {
1317
// @ts-expect-error transform.handler should exist
1418
const result = await instance.transform.handler.call(
15-
{ environment, parse: parseAst },
19+
{ environment, parse: parseAst, addWatchFile },
1620
code,
17-
'foo.ts',
21+
id,
1822
)
1923
return result?.code || result
2024
}
@@ -63,3 +67,29 @@ describe('assetImportMetaUrlPlugin', async () => {
6367
).toMatchInlineSnapshot(`"new URL(\`\${file}.js\`, import.meta.url)"`)
6468
})
6569
})
70+
71+
describe('assetImportMetaUrlPlugin watch files', () => {
72+
const file = normalizePath(
73+
path.join(import.meta.dirname, 'assetImportMetaUrl.spec.ts'),
74+
)
75+
76+
const cases = [
77+
'./assetImportMetaUrl.spec.ts',
78+
'./assetImportMetaUrl.spec.ts?inline',
79+
'./assetImportMetaUrl.spec.ts#hash',
80+
]
81+
82+
for (const url of cases) {
83+
test(`watches the referenced file for ${url}`, async () => {
84+
const watchFiles: string[] = []
85+
const transform = await createAssetImportMetaurlPluginTransform((file) =>
86+
watchFiles.push(file),
87+
)
88+
await transform(
89+
`new URL('${url}', import.meta.url)`,
90+
path.join(import.meta.dirname, 'entry.js'),
91+
)
92+
expect(watchFiles).toStrictEqual([file])
93+
})
94+
}
95+
})

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'node:path'
22
import MagicString from 'magic-string'
33
import { exactRegex } from 'rolldown/filter'
44
import { stripLiteral } from 'strip-literal'
5-
import { slash } from '../../shared/utils'
5+
import { cleanUrl, slash } from '../../shared/utils'
66
import type { ResolvedConfig } from '../config'
77
import { CLIENT_ENTRY } from '../constants'
88
import { createBackCompatIdResolver } from '../idResolver'
@@ -148,8 +148,9 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
148148
} else {
149149
builtUrlExpr = await fileToUrl(this, file, 'js')
150150
// during dev, builtUrl may point to a directory or a non-existing file
151-
if (tryStatSync(file)?.isFile()) {
152-
this.addWatchFile(file)
151+
const watchFile = cleanUrl(file)
152+
if (tryStatSync(watchFile)?.isFile()) {
153+
this.addWatchFile(watchFile)
153154
}
154155
}
155156
} catch {

0 commit comments

Comments
 (0)