Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import path from 'node:path'
import { parseAst } from 'rollup/parseAst'
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
}
Expand Down Expand Up @@ -63,3 +67,29 @@ describe('assetImportMetaUrlPlugin', async () => {
).toMatchInlineSnapshot(`"new URL(\`\${file}.js\`, import.meta.url)"`)
})
})

describe('assetImportMetaUrlPlugin watch files', () => {
const file = normalizePath(
path.join(import.meta.dirname, 'assetImportMetaUrl.spec.ts'),
)

const cases = [
'./assetImportMetaUrl.spec.ts',
'./assetImportMetaUrl.spec.ts?inline',
'./assetImportMetaUrl.spec.ts#hash',
]

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])
})
}
})
7 changes: 4 additions & 3 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
Loading