|
| 1 | +import path from 'node:path' |
1 | 2 | import { parseAst } from 'rollup/parseAst' |
2 | 3 | import { describe, expect, test } from 'vitest' |
3 | 4 | import { PartialEnvironment } from '../../baseEnvironment' |
4 | 5 | import { resolveConfig } from '../../config' |
5 | 6 | import { assetImportMetaUrlPlugin } from '../../plugins/assetImportMetaUrl' |
| 7 | +import { normalizePath } from '../../utils' |
6 | 8 |
|
7 | | -async function createAssetImportMetaurlPluginTransform() { |
| 9 | +async function createAssetImportMetaurlPluginTransform( |
| 10 | + addWatchFile?: (file: string) => void, |
| 11 | +) { |
8 | 12 | const config = await resolveConfig({ configFile: false }, 'serve') |
9 | 13 | const instance = assetImportMetaUrlPlugin(config) |
10 | 14 | const environment = new PartialEnvironment('client', config) |
11 | 15 |
|
12 | | - return async (code: string) => { |
| 16 | + return async (code: string, id = 'foo.ts') => { |
13 | 17 | // @ts-expect-error transform.handler should exist |
14 | 18 | const result = await instance.transform.handler.call( |
15 | | - { environment, parse: parseAst }, |
| 19 | + { environment, parse: parseAst, addWatchFile }, |
16 | 20 | code, |
17 | | - 'foo.ts', |
| 21 | + id, |
18 | 22 | ) |
19 | 23 | return result?.code || result |
20 | 24 | } |
@@ -63,3 +67,29 @@ describe('assetImportMetaUrlPlugin', async () => { |
63 | 67 | ).toMatchInlineSnapshot(`"new URL(\`\${file}.js\`, import.meta.url)"`) |
64 | 68 | }) |
65 | 69 | }) |
| 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 | +}) |
0 commit comments