Skip to content

Commit b68fa67

Browse files
feat(schematics): export reducer directly when Ivy is enabled (#2440)
1 parent fdee3d8 commit b68fa67

File tree

30 files changed

+773
-65
lines changed

30 files changed

+773
-65
lines changed

modules/data/schematics-core/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
pluralize,
1111
} from './utility/strings';
1212

13+
export { isIvyEnabled } from './utility/angular-utils';
14+
1315
export {
1416
findNodes,
1517
getSourceNodes,
@@ -47,6 +49,8 @@ export {
4749
ModuleOptions,
4850
} from './utility/find-module';
4951

52+
export { findPropertyInAstObject } from './utility/json-utilts';
53+
5054
export {
5155
addReducerToState,
5256
addReducerToStateInterface,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { JsonAstNode, JsonAstObject } from '@angular-devkit/core';
2+
3+
// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/json-utils.ts
4+
export function findPropertyInAstObject(
5+
node: JsonAstObject,
6+
propertyName: string
7+
): JsonAstNode | null {
8+
let maybeNode: JsonAstNode | null = null;
9+
for (const property of node.properties) {
10+
if (property.key.value == propertyName) {
11+
maybeNode = property.value;
12+
}
13+
}
14+
15+
return maybeNode;
16+
}

modules/effects/schematics-core/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
pluralize,
1111
} from './utility/strings';
1212

13+
export { isIvyEnabled } from './utility/angular-utils';
14+
1315
export {
1416
findNodes,
1517
getSourceNodes,
@@ -47,6 +49,8 @@ export {
4749
ModuleOptions,
4850
} from './utility/find-module';
4951

52+
export { findPropertyInAstObject } from './utility/json-utilts';
53+
5054
export {
5155
addReducerToState,
5256
addReducerToStateInterface,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { JsonAstNode, JsonAstObject } from '@angular-devkit/core';
2+
3+
// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/json-utils.ts
4+
export function findPropertyInAstObject(
5+
node: JsonAstObject,
6+
propertyName: string
7+
): JsonAstNode | null {
8+
let maybeNode: JsonAstNode | null = null;
9+
for (const property of node.properties) {
10+
if (property.key.value == propertyName) {
11+
maybeNode = property.value;
12+
}
13+
}
14+
15+
return maybeNode;
16+
}

modules/entity/schematics-core/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
pluralize,
1111
} from './utility/strings';
1212

13+
export { isIvyEnabled } from './utility/angular-utils';
14+
1315
export {
1416
findNodes,
1517
getSourceNodes,
@@ -47,6 +49,8 @@ export {
4749
ModuleOptions,
4850
} from './utility/find-module';
4951

52+
export { findPropertyInAstObject } from './utility/json-utilts';
53+
5054
export {
5155
addReducerToState,
5256
addReducerToStateInterface,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { JsonAstNode, JsonAstObject } from '@angular-devkit/core';
2+
3+
// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/json-utils.ts
4+
export function findPropertyInAstObject(
5+
node: JsonAstObject,
6+
propertyName: string
7+
): JsonAstNode | null {
8+
let maybeNode: JsonAstNode | null = null;
9+
for (const property of node.properties) {
10+
if (property.key.value == propertyName) {
11+
maybeNode = property.value;
12+
}
13+
}
14+
15+
return maybeNode;
16+
}

modules/router-store/schematics-core/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
pluralize,
1111
} from './utility/strings';
1212

13+
export { isIvyEnabled } from './utility/angular-utils';
14+
1315
export {
1416
findNodes,
1517
getSourceNodes,
@@ -47,6 +49,8 @@ export {
4749
ModuleOptions,
4850
} from './utility/find-module';
4951

52+
export { findPropertyInAstObject } from './utility/json-utilts';
53+
5054
export {
5155
addReducerToState,
5256
addReducerToStateInterface,

0 commit comments

Comments
 (0)