|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | + |
| 5 | +import { normalizePath } from 'vite' |
| 6 | +import { afterEach, describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +import { angular, type PluginOptions } from '../vite-plugin/index.js' |
| 9 | +import { |
| 10 | + installedAngularVersion, |
| 11 | + parseAngularVersion, |
| 12 | +} from '../vite-plugin/utils/angular-version.js' |
| 13 | + |
| 14 | +const dirs: string[] = [] |
| 15 | + |
| 16 | +/** A project directory with `@angular/core` at `version` installed, if any. */ |
| 17 | +function project(version?: string): string { |
| 18 | + const dir = mkdtempSync(join(tmpdir(), 'angular-version-')) |
| 19 | + dirs.push(dir) |
| 20 | + if (version) install(dir, version) |
| 21 | + return dir |
| 22 | +} |
| 23 | + |
| 24 | +/** `@angular/core` at `version` in `dir/node_modules`. */ |
| 25 | +function install(dir: string, version: string): void { |
| 26 | + const core = join(dir, 'node_modules/@angular/core') |
| 27 | + mkdirSync(core, { recursive: true }) |
| 28 | + writeFileSync(join(core, 'package.json'), JSON.stringify({ name: '@angular/core', version })) |
| 29 | +} |
| 30 | + |
| 31 | +afterEach(() => { |
| 32 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }) |
| 33 | +}) |
| 34 | + |
| 35 | +describe('parseAngularVersion', () => { |
| 36 | + it('reads stable and prerelease versions', () => { |
| 37 | + expect(parseAngularVersion('22.1.3')).toEqual({ major: 22, minor: 1, patch: 3 }) |
| 38 | + expect(parseAngularVersion('22.2.0-next.3')).toEqual({ |
| 39 | + major: 22, |
| 40 | + minor: 2, |
| 41 | + patch: 0, |
| 42 | + prerelease: 'next.3', |
| 43 | + }) |
| 44 | + expect(parseAngularVersion('21.0.0+sha-abc')).toEqual({ major: 21, minor: 0, patch: 0 }) |
| 45 | + }) |
| 46 | + |
| 47 | + it("leaves out a build of Angular's own repository and anything else", () => { |
| 48 | + expect(parseAngularVersion('0.0.0-PLACEHOLDER')).toBeUndefined() |
| 49 | + expect(parseAngularVersion('22')).toBeUndefined() |
| 50 | + expect(parseAngularVersion(undefined)).toBeUndefined() |
| 51 | + }) |
| 52 | +}) |
| 53 | + |
| 54 | +describe('installedAngularVersion', () => { |
| 55 | + it('finds @angular/core from a nested directory', () => { |
| 56 | + const root = project('21.2.1') |
| 57 | + const nested = join(root, 'apps/app') |
| 58 | + mkdirSync(nested, { recursive: true }) |
| 59 | + expect(installedAngularVersion(nested)).toEqual({ major: 21, minor: 2, patch: 1 }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('resolves a relative root first', () => { |
| 63 | + // `.` in an app whose packages are hoisted to the workspace above it. |
| 64 | + const root = project('21.2.1') |
| 65 | + const nested = join(root, 'apps/app') |
| 66 | + mkdirSync(nested, { recursive: true }) |
| 67 | + const cwd = process.cwd() |
| 68 | + process.chdir(nested) |
| 69 | + try { |
| 70 | + expect(installedAngularVersion('.')).toEqual({ major: 21, minor: 2, patch: 1 }) |
| 71 | + } finally { |
| 72 | + process.chdir(cwd) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + it('gives undefined without a readable package', () => { |
| 77 | + const root = project() |
| 78 | + mkdirSync(join(root, 'node_modules/@angular/core'), { recursive: true }) |
| 79 | + writeFileSync(join(root, 'node_modules/@angular/core/package.json'), '{') |
| 80 | + expect(installedAngularVersion(root)).toBeUndefined() |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +const SOURCE = `import { Component } from '@angular/core'; |
| 85 | +import { Cmp } from './cmp'; |
| 86 | +
|
| 87 | +@Component({ |
| 88 | + selector: 'app-root', |
| 89 | + template: '@defer { <cmp-a /> }', |
| 90 | + deferredImports: [Cmp], |
| 91 | +}) |
| 92 | +export class AppComponent {} |
| 93 | +` |
| 94 | + |
| 95 | +/** The plugin's output for `SOURCE` in `root`, or the error it reports. */ |
| 96 | +async function transform(options: PluginOptions, root: string): Promise<string> { |
| 97 | + const plugin = angular(options).find((candidate) => candidate.name === '@oxc-angular/vite') |
| 98 | + if (!plugin?.transform || typeof plugin.transform === 'function') { |
| 99 | + throw new Error('Expected plugin transform handler') |
| 100 | + } |
| 101 | + const configResolved = plugin.configResolved as unknown as |
| 102 | + | { handler: (config: unknown) => void } |
| 103 | + | ((config: unknown) => void) |
| 104 | + const config = { root, build: {}, isProduction: false } |
| 105 | + if (typeof configResolved === 'function') configResolved(config) |
| 106 | + else configResolved?.handler(config) |
| 107 | + try { |
| 108 | + const result = await plugin.transform.handler.call( |
| 109 | + { |
| 110 | + error(message: string) { |
| 111 | + throw new Error(message) |
| 112 | + }, |
| 113 | + warn() {}, |
| 114 | + } as any, |
| 115 | + SOURCE, |
| 116 | + normalizePath(join(root, 'src/app.component.ts')), |
| 117 | + ) |
| 118 | + return (result as { code: string }).code |
| 119 | + } catch (error) { |
| 120 | + return `ERROR ${(error as Error).message}` |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +describe('angularVersion default', () => { |
| 125 | + it('compiles the array deferredImports of an Angular 22.1 project', async () => { |
| 126 | + const root = project('22.1.0') |
| 127 | + const code = await transform({ workspaceRoot: root }, root) |
| 128 | + expect(code).not.toContain('ERROR') |
| 129 | + expect(code).toContain('import("./cmp")') |
| 130 | + }) |
| 131 | + |
| 132 | + it('reports the array deferredImports of an Angular 22.2 project', async () => { |
| 133 | + const root = project('22.2.0') |
| 134 | + const code = await transform({ workspaceRoot: root }, root) |
| 135 | + expect(code).toContain("'deferredImports' must be an object mapping") |
| 136 | + }) |
| 137 | + |
| 138 | + it('prefers the angularVersion option over the installed version', async () => { |
| 139 | + const root = project('22.2.0') |
| 140 | + const code = await transform( |
| 141 | + { workspaceRoot: root, angularVersion: { major: 22, minor: 1, patch: 0 } }, |
| 142 | + root, |
| 143 | + ) |
| 144 | + expect(code).not.toContain('ERROR') |
| 145 | + }) |
| 146 | + |
| 147 | + it("reads the version installed for Vite's root, not the workspace root", async () => { |
| 148 | + // An app with its own `@angular/core` in a workspace on a newer one. |
| 149 | + const workspace = project('22.2.0') |
| 150 | + const app = join(workspace, 'apps/old') |
| 151 | + install(app, '22.1.0') |
| 152 | + const code = await transform({ workspaceRoot: workspace }, app) |
| 153 | + expect(code).not.toContain('ERROR') |
| 154 | + }) |
| 155 | + |
| 156 | + it('links dependencies for the same version', async () => { |
| 157 | + const workspace = project('22.2.0') |
| 158 | + const app = join(workspace, 'apps/old') |
| 159 | + install(app, '22.1.0') |
| 160 | + const linker = angular({ workspaceRoot: workspace }).find( |
| 161 | + (candidate) => candidate.name === '@oxc-angular/vite-linker', |
| 162 | + ) as any |
| 163 | + const config = linker.config({ root: app }, { command: 'build', mode: 'production' }) |
| 164 | + expect(config.optimizeDeps.rolldownOptions.plugins[0].name).toBe('angular-linker@22.1.0') |
| 165 | + }) |
| 166 | +}) |
0 commit comments