|
| 1 | +import { |
| 2 | + JsonParseMode, |
| 3 | + dirname, |
| 4 | + normalize, |
| 5 | + parseJsonAst, |
| 6 | + resolve, |
| 7 | +} from '@angular-devkit/core'; |
| 8 | +import { Tree } from '@angular-devkit/schematics'; |
| 9 | +import { findPropertyInAstObject } from './json-utilts'; |
| 10 | + |
| 11 | +// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/migrations/update-9/utils.ts |
| 12 | +export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean { |
| 13 | + // In version 9, Ivy is turned on by default |
| 14 | + // Ivy is opted out only when 'enableIvy' is set to false. |
| 15 | + |
| 16 | + const buffer = tree.read(tsConfigPath); |
| 17 | + if (!buffer) { |
| 18 | + return true; |
| 19 | + } |
| 20 | + |
| 21 | + const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose); |
| 22 | + |
| 23 | + if (tsCfgAst.kind !== 'object') { |
| 24 | + return true; |
| 25 | + } |
| 26 | + |
| 27 | + const ngCompilerOptions = findPropertyInAstObject( |
| 28 | + tsCfgAst, |
| 29 | + 'angularCompilerOptions' |
| 30 | + ); |
| 31 | + if (ngCompilerOptions && ngCompilerOptions.kind === 'object') { |
| 32 | + const enableIvy = findPropertyInAstObject(ngCompilerOptions, 'enableIvy'); |
| 33 | + |
| 34 | + if (enableIvy) { |
| 35 | + return !!enableIvy.value; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const configExtends = findPropertyInAstObject(tsCfgAst, 'extends'); |
| 40 | + if (configExtends && configExtends.kind === 'string') { |
| 41 | + const extendedTsConfigPath = resolve( |
| 42 | + dirname(normalize(tsConfigPath)), |
| 43 | + normalize(configExtends.value) |
| 44 | + ); |
| 45 | + |
| 46 | + return isIvyEnabled(tree, extendedTsConfigPath); |
| 47 | + } |
| 48 | + |
| 49 | + return true; |
| 50 | +} |
0 commit comments