Skip to content

Commit aa4430c

Browse files
committed
fix(vite-plugin-angular): preserve import.meta during Vitest downleveling
1 parent cd2104e commit aa4430c

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
import { angularVitestPlugin } from './angular-vitest-plugin';
3+
4+
// Exercise the Vite 6/7 fallback while the workspace itself uses Vite 8.
5+
vi.mock(import('./utils/rolldown.js'), () => ({
6+
getJsTransformConfigKey: () => 'esbuild',
7+
isRolldown: () => false,
8+
}));
9+
10+
const transform = (code: string, id: string) => {
11+
const hook = angularVitestPlugin().transform as {
12+
handler: (code: string, id: string) => Promise<{ code: string }>;
13+
};
14+
return hook.handler(code, id);
15+
};
16+
17+
describe('angularVitestPlugin esbuild downleveling', () => {
18+
it('preserves HMR access in generated CDK CSS modules', async () => {
19+
const { code } = await transform(
20+
'export const id = "/node_modules/@angular/cdk/overlay-prebuilt.css"; import.meta.hot.accept();',
21+
'/node_modules/@angular/cdk/overlay-prebuilt.css',
22+
);
23+
24+
expect(code).toContain('import.meta.hot.accept()');
25+
});
26+
27+
it('preserves module metadata while downleveling async functions', async () => {
28+
const { code } = await transform(
29+
'export const url = import.meta.url; export async function example() { return await Promise.resolve(42); }',
30+
'/node_modules/@angular/core/fesm2022/core.mjs',
31+
);
32+
const moduleUrl = `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`;
33+
const result = await import(moduleUrl);
34+
35+
expect(result.url).toBe(moduleUrl);
36+
expect(await result.example()).toBe(42);
37+
expect(code).not.toMatch(/\basync\s+function\b/);
38+
});
39+
});

‎packages/vite-plugin-angular/src/lib/angular-vitest-plugin.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export function angularVitestPlugin(): Plugin {
7979
loader: 'js',
8080
format: 'esm',
8181
target: 'es2016',
82+
supported: { 'import-meta': true },
8283
sourcemap: true,
8384
sourcefile: id,
8485
});

0 commit comments

Comments
 (0)