|
8 | 8 |
|
9 | 9 | import ts from 'typescript'; |
10 | 10 | import type { AngularHostOptions } from '../angular-host'; |
| 11 | +import { transformCompilerOptions } from './compiler-options'; |
| 12 | +import { TypeScriptCompilation } from './typescript-compilation'; |
11 | 13 | import { |
12 | 14 | AngularCompilation, |
13 | 15 | AngularCompilationResult, |
14 | 16 | DiagnosticModes, |
15 | 17 | NoopCompilation, |
16 | | - TypeScriptCompilation, |
17 | 18 | createAngularCompilation, |
18 | 19 | } from './index'; |
19 | 20 |
|
@@ -62,13 +63,24 @@ describe('AngularCompilation', () => { |
62 | 63 | it('initializes with empty referencedFiles and compiler options', async () => { |
63 | 64 | const compilation = new NoopCompilation(); |
64 | 65 | const mockHostOptions = {} as AngularHostOptions; |
65 | | - const result = await compilation.initialize('tsconfig.json', mockHostOptions, (opts) => ({ |
66 | | - ...opts, |
67 | | - customOption: true, |
68 | | - })); |
| 66 | + const result = await compilation.initialize('tsconfig.json', mockHostOptions); |
69 | 67 |
|
70 | 68 | expect(result.referencedFiles).toEqual([]); |
71 | | - expect(result.compilerOptions['customOption']).toBe(true); |
| 69 | + expect(result.compilerOptions).toBeDefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('initializes with CompilerOptionOverrides object', async () => { |
| 73 | + const compilation = new NoopCompilation(); |
| 74 | + const mockHostOptions = {} as AngularHostOptions; |
| 75 | + const result = await compilation.initialize('tsconfig.json', mockHostOptions, { |
| 76 | + sourcemap: true, |
| 77 | + enableHmr: true, |
| 78 | + }); |
| 79 | + |
| 80 | + expect(result.referencedFiles).toEqual([]); |
| 81 | + expect(result.compilerOptions.inlineSources).toBe(true); |
| 82 | + expect(result.compilerOptions.inlineSourceMap).toBe(true); |
| 83 | + expect(result.compilerOptions['_enableHmr']).toBe(true); |
72 | 84 | }); |
73 | 85 |
|
74 | 86 | it('throws when calling emitAffectedFiles', () => { |
@@ -184,4 +196,160 @@ describe('AngularCompilation', () => { |
184 | 196 | ); |
185 | 197 | }); |
186 | 198 | }); |
| 199 | + |
| 200 | + describe('transformCompilerOptions', () => { |
| 201 | + it('does not mutate the input compiler options object', () => { |
| 202 | + const originalOptions: ts.CompilerOptions = { |
| 203 | + target: ts.ScriptTarget.ES2020, |
| 204 | + module: ts.ModuleKind.CommonJS, |
| 205 | + }; |
| 206 | + const originalCopy = { ...originalOptions }; |
| 207 | + |
| 208 | + transformCompilerOptions(ts, originalOptions); |
| 209 | + |
| 210 | + expect(originalOptions).toEqual(originalCopy); |
| 211 | + }); |
| 212 | + |
| 213 | + it('sets target to ES2022 and useDefineForClassFields to false when target is undefined', () => { |
| 214 | + const { compilerOptions, warnings } = transformCompilerOptions( |
| 215 | + ts, |
| 216 | + { module: ts.ModuleKind.ES2022 }, |
| 217 | + undefined, |
| 218 | + 'tsconfig.json', |
| 219 | + ); |
| 220 | + |
| 221 | + expect(compilerOptions.target).toBe(ts.ScriptTarget.ES2022); |
| 222 | + expect(compilerOptions.useDefineForClassFields).toBe(false); |
| 223 | + expect(warnings.length).toBe(1); |
| 224 | + expect(warnings[0].text).toContain( |
| 225 | + "TypeScript compiler options 'target' and 'useDefineForClassFields'", |
| 226 | + ); |
| 227 | + expect(warnings[0].location?.file).toBe('tsconfig.json'); |
| 228 | + }); |
| 229 | + |
| 230 | + it('preserves existing useDefineForClassFields if target < ES2022', () => { |
| 231 | + const { compilerOptions, warnings } = transformCompilerOptions( |
| 232 | + ts, |
| 233 | + { |
| 234 | + target: ts.ScriptTarget.ES2020, |
| 235 | + useDefineForClassFields: true, |
| 236 | + module: ts.ModuleKind.ES2022, |
| 237 | + }, |
| 238 | + undefined, |
| 239 | + 'tsconfig.json', |
| 240 | + ); |
| 241 | + |
| 242 | + expect(compilerOptions.target).toBe(ts.ScriptTarget.ES2022); |
| 243 | + expect(compilerOptions.useDefineForClassFields).toBe(true); |
| 244 | + expect(warnings.length).toBe(1); |
| 245 | + }); |
| 246 | + |
| 247 | + it('sets compilationMode to full and warns when compilationMode is partial', () => { |
| 248 | + const { compilerOptions, warnings } = transformCompilerOptions(ts, { |
| 249 | + target: ts.ScriptTarget.ES2022, |
| 250 | + module: ts.ModuleKind.ES2022, |
| 251 | + compilationMode: 'partial', |
| 252 | + }); |
| 253 | + |
| 254 | + expect(compilerOptions.compilationMode).toBe('full'); |
| 255 | + expect(warnings.length).toBe(1); |
| 256 | + expect(warnings[0].text).toContain('Angular partial compilation mode is not supported'); |
| 257 | + }); |
| 258 | + |
| 259 | + it('configures incremental and tsBuildInfoFile when cachePath is provided', () => { |
| 260 | + const { compilerOptions } = transformCompilerOptions( |
| 261 | + ts, |
| 262 | + { target: ts.ScriptTarget.ES2022 }, |
| 263 | + { cachePath: '/tmp/cache' }, |
| 264 | + ); |
| 265 | + |
| 266 | + expect(compilerOptions.incremental).toBe(true); |
| 267 | + expect(compilerOptions.tsBuildInfoFile).toContain('.tsbuildinfo'); |
| 268 | + }); |
| 269 | + |
| 270 | + it('sets incremental to false when cachePath is not provided or incremental is false', () => { |
| 271 | + const { compilerOptions: opt1 } = transformCompilerOptions( |
| 272 | + ts, |
| 273 | + { target: ts.ScriptTarget.ES2022 }, |
| 274 | + undefined, |
| 275 | + ); |
| 276 | + expect(opt1.incremental).toBe(false); |
| 277 | + |
| 278 | + const { compilerOptions: opt2 } = transformCompilerOptions( |
| 279 | + ts, |
| 280 | + { target: ts.ScriptTarget.ES2022, incremental: false }, |
| 281 | + { cachePath: '/tmp/cache' }, |
| 282 | + ); |
| 283 | + expect(opt2.incremental).toBe(false); |
| 284 | + }); |
| 285 | + |
| 286 | + it('sets module to ES2022 and warns when module < ES2015', () => { |
| 287 | + const { compilerOptions, warnings } = transformCompilerOptions(ts, { |
| 288 | + target: ts.ScriptTarget.ES2022, |
| 289 | + module: ts.ModuleKind.CommonJS, |
| 290 | + }); |
| 291 | + |
| 292 | + expect(compilerOptions.module).toBe(ts.ModuleKind.ES2022); |
| 293 | + expect(warnings.length).toBe(1); |
| 294 | + expect(warnings[0].text).toContain( |
| 295 | + "TypeScript compiler options 'module' values 'CommonJS', 'UMD'", |
| 296 | + ); |
| 297 | + }); |
| 298 | + |
| 299 | + it('warns when isolatedModules is enabled with emitDecoratorMetadata', () => { |
| 300 | + const { warnings } = transformCompilerOptions(ts, { |
| 301 | + target: ts.ScriptTarget.ES2022, |
| 302 | + module: ts.ModuleKind.ES2022, |
| 303 | + isolatedModules: true, |
| 304 | + emitDecoratorMetadata: true, |
| 305 | + }); |
| 306 | + |
| 307 | + expect(warnings.length).toBe(1); |
| 308 | + expect(warnings[0].text).toContain( |
| 309 | + "TypeScript compiler option 'isolatedModules' may prevent", |
| 310 | + ); |
| 311 | + }); |
| 312 | + |
| 313 | + it('synchronizes customConditions when moduleResolution is Bundler or module is Preserve', () => { |
| 314 | + const { compilerOptions: bundlerOptions } = transformCompilerOptions( |
| 315 | + ts, |
| 316 | + { target: ts.ScriptTarget.ES2022, moduleResolution: ts.ModuleResolutionKind.Bundler }, |
| 317 | + { customConditions: ['development'] }, |
| 318 | + ); |
| 319 | + expect(bundlerOptions.customConditions).toEqual(['development']); |
| 320 | + |
| 321 | + const { compilerOptions: preserveOptions } = transformCompilerOptions( |
| 322 | + ts, |
| 323 | + { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.Preserve }, |
| 324 | + { customConditions: ['development'] }, |
| 325 | + ); |
| 326 | + expect(preserveOptions.customConditions).toEqual(['development']); |
| 327 | + }); |
| 328 | + |
| 329 | + it('applies override options correctly', () => { |
| 330 | + const { compilerOptions } = transformCompilerOptions( |
| 331 | + ts, |
| 332 | + { target: ts.ScriptTarget.ES2022, isolatedModules: true }, |
| 333 | + { |
| 334 | + sourcemap: true, |
| 335 | + preserveSymlinks: true, |
| 336 | + externalRuntimeStyles: true, |
| 337 | + enableHmr: true, |
| 338 | + instrumentForCoverage: true, |
| 339 | + includeTestMetadata: true, |
| 340 | + }, |
| 341 | + ); |
| 342 | + |
| 343 | + expect(compilerOptions.inlineSources).toBe(true); |
| 344 | + expect(compilerOptions.inlineSourceMap).toBe(true); |
| 345 | + expect(compilerOptions.preserveSymlinks).toBe(true); |
| 346 | + expect(compilerOptions.externalRuntimeStyles).toBe(true); |
| 347 | + expect(compilerOptions['_enableHmr']).toBe(true); |
| 348 | + expect(compilerOptions['_useTypeScriptTranspilation']).toBe(true); |
| 349 | + expect(compilerOptions.supportTestBed).toBe(true); |
| 350 | + expect(compilerOptions.supportJitMode).toBe(true); |
| 351 | + expect(compilerOptions.noEmitOnError).toBe(false); |
| 352 | + expect(compilerOptions.composite).toBe(false); |
| 353 | + }); |
| 354 | + }); |
187 | 355 | }); |
0 commit comments