Skip to content

Commit

Permalink
feat(schematics): export reducer directly when Ivy is enabled (#2440)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Mar 19, 2020
1 parent fdee3d8 commit b68fa67
Show file tree
Hide file tree
Showing 30 changed files with 773 additions and 65 deletions.
4 changes: 4 additions & 0 deletions modules/data/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
pluralize,
} from './utility/strings';

export { isIvyEnabled } from './utility/angular-utils';

export {
findNodes,
getSourceNodes,
Expand Down Expand Up @@ -47,6 +49,8 @@ export {
ModuleOptions,
} from './utility/find-module';

export { findPropertyInAstObject } from './utility/json-utilts';

export {
addReducerToState,
addReducerToStateInterface,
Expand Down
50 changes: 50 additions & 0 deletions modules/data/schematics-core/utility/angular-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
JsonParseMode,
dirname,
normalize,
parseJsonAst,
resolve,
} from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { findPropertyInAstObject } from './json-utilts';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/migrations/update-9/utils.ts
export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean {
// In version 9, Ivy is turned on by default
// Ivy is opted out only when 'enableIvy' is set to false.

const buffer = tree.read(tsConfigPath);
if (!buffer) {
return true;
}

const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);

if (tsCfgAst.kind !== 'object') {
return true;
}

const ngCompilerOptions = findPropertyInAstObject(
tsCfgAst,
'angularCompilerOptions'
);
if (ngCompilerOptions && ngCompilerOptions.kind === 'object') {
const enableIvy = findPropertyInAstObject(ngCompilerOptions, 'enableIvy');

if (enableIvy) {
return !!enableIvy.value;
}
}

const configExtends = findPropertyInAstObject(tsCfgAst, 'extends');
if (configExtends && configExtends.kind === 'string') {
const extendedTsConfigPath = resolve(
dirname(normalize(tsConfigPath)),
normalize(configExtends.value)
);

return isIvyEnabled(tree, extendedTsConfigPath);
}

return true;
}
16 changes: 16 additions & 0 deletions modules/data/schematics-core/utility/json-utilts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JsonAstNode, JsonAstObject } from '@angular-devkit/core';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/json-utils.ts
export function findPropertyInAstObject(
node: JsonAstObject,
propertyName: string
): JsonAstNode | null {
let maybeNode: JsonAstNode | null = null;
for (const property of node.properties) {
if (property.key.value == propertyName) {
maybeNode = property.value;
}
}

return maybeNode;
}
4 changes: 4 additions & 0 deletions modules/effects/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
pluralize,
} from './utility/strings';

export { isIvyEnabled } from './utility/angular-utils';

export {
findNodes,
getSourceNodes,
Expand Down Expand Up @@ -47,6 +49,8 @@ export {
ModuleOptions,
} from './utility/find-module';

export { findPropertyInAstObject } from './utility/json-utilts';

export {
addReducerToState,
addReducerToStateInterface,
Expand Down
50 changes: 50 additions & 0 deletions modules/effects/schematics-core/utility/angular-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
JsonParseMode,
dirname,
normalize,
parseJsonAst,
resolve,
} from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { findPropertyInAstObject } from './json-utilts';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/migrations/update-9/utils.ts
export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean {
// In version 9, Ivy is turned on by default
// Ivy is opted out only when 'enableIvy' is set to false.

const buffer = tree.read(tsConfigPath);
if (!buffer) {
return true;
}

const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);

if (tsCfgAst.kind !== 'object') {
return true;
}

const ngCompilerOptions = findPropertyInAstObject(
tsCfgAst,
'angularCompilerOptions'
);
if (ngCompilerOptions && ngCompilerOptions.kind === 'object') {
const enableIvy = findPropertyInAstObject(ngCompilerOptions, 'enableIvy');

if (enableIvy) {
return !!enableIvy.value;
}
}

const configExtends = findPropertyInAstObject(tsCfgAst, 'extends');
if (configExtends && configExtends.kind === 'string') {
const extendedTsConfigPath = resolve(
dirname(normalize(tsConfigPath)),
normalize(configExtends.value)
);

return isIvyEnabled(tree, extendedTsConfigPath);
}

return true;
}
16 changes: 16 additions & 0 deletions modules/effects/schematics-core/utility/json-utilts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JsonAstNode, JsonAstObject } from '@angular-devkit/core';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/json-utils.ts
export function findPropertyInAstObject(
node: JsonAstObject,
propertyName: string
): JsonAstNode | null {
let maybeNode: JsonAstNode | null = null;
for (const property of node.properties) {
if (property.key.value == propertyName) {
maybeNode = property.value;
}
}

return maybeNode;
}
4 changes: 4 additions & 0 deletions modules/entity/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
pluralize,
} from './utility/strings';

export { isIvyEnabled } from './utility/angular-utils';

export {
findNodes,
getSourceNodes,
Expand Down Expand Up @@ -47,6 +49,8 @@ export {
ModuleOptions,
} from './utility/find-module';

export { findPropertyInAstObject } from './utility/json-utilts';

export {
addReducerToState,
addReducerToStateInterface,
Expand Down
50 changes: 50 additions & 0 deletions modules/entity/schematics-core/utility/angular-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
JsonParseMode,
dirname,
normalize,
parseJsonAst,
resolve,
} from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { findPropertyInAstObject } from './json-utilts';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/migrations/update-9/utils.ts
export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean {
// In version 9, Ivy is turned on by default
// Ivy is opted out only when 'enableIvy' is set to false.

const buffer = tree.read(tsConfigPath);
if (!buffer) {
return true;
}

const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);

if (tsCfgAst.kind !== 'object') {
return true;
}

const ngCompilerOptions = findPropertyInAstObject(
tsCfgAst,
'angularCompilerOptions'
);
if (ngCompilerOptions && ngCompilerOptions.kind === 'object') {
const enableIvy = findPropertyInAstObject(ngCompilerOptions, 'enableIvy');

if (enableIvy) {
return !!enableIvy.value;
}
}

const configExtends = findPropertyInAstObject(tsCfgAst, 'extends');
if (configExtends && configExtends.kind === 'string') {
const extendedTsConfigPath = resolve(
dirname(normalize(tsConfigPath)),
normalize(configExtends.value)
);

return isIvyEnabled(tree, extendedTsConfigPath);
}

return true;
}
16 changes: 16 additions & 0 deletions modules/entity/schematics-core/utility/json-utilts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JsonAstNode, JsonAstObject } from '@angular-devkit/core';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/json-utils.ts
export function findPropertyInAstObject(
node: JsonAstObject,
propertyName: string
): JsonAstNode | null {
let maybeNode: JsonAstNode | null = null;
for (const property of node.properties) {
if (property.key.value == propertyName) {
maybeNode = property.value;
}
}

return maybeNode;
}
4 changes: 4 additions & 0 deletions modules/router-store/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
pluralize,
} from './utility/strings';

export { isIvyEnabled } from './utility/angular-utils';

export {
findNodes,
getSourceNodes,
Expand Down Expand Up @@ -47,6 +49,8 @@ export {
ModuleOptions,
} from './utility/find-module';

export { findPropertyInAstObject } from './utility/json-utilts';

export {
addReducerToState,
addReducerToStateInterface,
Expand Down
50 changes: 50 additions & 0 deletions modules/router-store/schematics-core/utility/angular-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
JsonParseMode,
dirname,
normalize,
parseJsonAst,
resolve,
} from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { findPropertyInAstObject } from './json-utilts';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/migrations/update-9/utils.ts
export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean {
// In version 9, Ivy is turned on by default
// Ivy is opted out only when 'enableIvy' is set to false.

const buffer = tree.read(tsConfigPath);
if (!buffer) {
return true;
}

const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);

if (tsCfgAst.kind !== 'object') {
return true;
}

const ngCompilerOptions = findPropertyInAstObject(
tsCfgAst,
'angularCompilerOptions'
);
if (ngCompilerOptions && ngCompilerOptions.kind === 'object') {
const enableIvy = findPropertyInAstObject(ngCompilerOptions, 'enableIvy');

if (enableIvy) {
return !!enableIvy.value;
}
}

const configExtends = findPropertyInAstObject(tsCfgAst, 'extends');
if (configExtends && configExtends.kind === 'string') {
const extendedTsConfigPath = resolve(
dirname(normalize(tsConfigPath)),
normalize(configExtends.value)
);

return isIvyEnabled(tree, extendedTsConfigPath);
}

return true;
}
16 changes: 16 additions & 0 deletions modules/router-store/schematics-core/utility/json-utilts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JsonAstNode, JsonAstObject } from '@angular-devkit/core';

// https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/json-utils.ts
export function findPropertyInAstObject(
node: JsonAstObject,
propertyName: string
): JsonAstNode | null {
let maybeNode: JsonAstNode | null = null;
for (const property of node.properties) {
if (property.key.value == propertyName) {
maybeNode = property.value;
}
}

return maybeNode;
}
4 changes: 4 additions & 0 deletions modules/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
pluralize,
} from './utility/strings';

export { isIvyEnabled } from './utility/angular-utils';

export {
findNodes,
getSourceNodes,
Expand Down Expand Up @@ -47,6 +49,8 @@ export {
ModuleOptions,
} from './utility/find-module';

export { findPropertyInAstObject } from './utility/json-utilts';

export {
addReducerToState,
addReducerToStateInterface,
Expand Down
Loading

0 comments on commit b68fa67

Please sign in to comment.