From 1716c0b7f78cbacc677bd2361b5235918afcd754 Mon Sep 17 00:00:00 2001 From: Juri Strumpflohner Date: Fri, 5 Mar 2021 17:40:28 +0100 Subject: [PATCH] fix(angular): adjust ngcc postinstall command (#4641) --- packages/angular/migrations.json | 5 ++ .../update-ngcc-postinstall.spec.ts | 57 +++++++++++++++++++ .../update-12-0-0/update-ngcc-postinstall.ts | 24 ++++++++ .../angular/src/schematics/init/init.spec.ts | 2 +- packages/angular/src/schematics/init/init.ts | 3 +- 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.spec.ts create mode 100644 packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.ts diff --git a/packages/angular/migrations.json b/packages/angular/migrations.json index c6ae1ffe7f148..60dcf4a71259b 100644 --- a/packages/angular/migrations.json +++ b/packages/angular/migrations.json @@ -55,6 +55,11 @@ "version": "11.0.0-beta.13", "description": "Update builder configurations and dependencies", "factory": "./src/migrations/update-11-0-0/update-builders-config" + }, + "update-ngcc-postinstall": { + "version": "12.0.0-beta.0", + "description": "adjusts the ngcc postinstall command to just leave 'ngcc' in there. This fixes Ivy in Jest tests and Storybooks", + "factory": "./src/migrations/update-12-0-0/update-ngcc-postinstall" } }, "packageJsonUpdates": { diff --git a/packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.spec.ts b/packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.spec.ts new file mode 100644 index 0000000000000..6f9dc791aceeb --- /dev/null +++ b/packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.spec.ts @@ -0,0 +1,57 @@ +import { Tree } from '@angular-devkit/schematics'; +import { createEmptyWorkspace } from '@nrwl/workspace/testing'; +import { runMigration } from '../../utils/testing'; + +function createAngularCLIPoweredWorkspace() { + const tree = createEmptyWorkspace(Tree.empty()); + tree.delete('workspace.json'); + tree.create('angular.json', '{}'); + return tree; +} + +describe('Remove ngcc flags from postinstall script', () => { + [ + { + test: + 'node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points', + expected: + 'node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main', + }, + { + test: + 'node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points && echo "hi"', + expected: + 'node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main && echo "hi"', + }, + { + test: + 'ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points && node ./decorate-angular-cli.js && echo "hi"', + expected: + 'ngcc --properties es2015 browser module main && node ./decorate-angular-cli.js && echo "hi"', + }, + { + test: + 'ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points && node ./decorate-angular-cli.js', + expected: + 'ngcc --properties es2015 browser module main && node ./decorate-angular-cli.js', + }, + ].forEach((testEntry) => { + it(`should adjust ngcc for: "${testEntry.test}"`, async () => { + const tree = createAngularCLIPoweredWorkspace(); + + tree.overwrite( + '/package.json', + JSON.stringify({ + scripts: { + postinstall: testEntry.test, + }, + }) + ); + + const result = await runMigration('update-ngcc-postinstall', {}, tree); + + const packageJson = JSON.parse(result.read('/package.json').toString()); + expect(packageJson.scripts.postinstall).toEqual(testEntry.expected); + }); + }); +}); diff --git a/packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.ts b/packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.ts new file mode 100644 index 0000000000000..db12abf3fbf12 --- /dev/null +++ b/packages/angular/src/migrations/update-12-0-0/update-ngcc-postinstall.ts @@ -0,0 +1,24 @@ +import { chain, SchematicContext, Tree } from '@angular-devkit/schematics'; +import { updateJsonInTree } from '@nrwl/workspace'; +import { formatFiles } from '@nrwl/workspace'; + +const udpateNgccPostinstallScript = (host: Tree, context: SchematicContext) => { + updateJsonInTree('package.json', (json) => { + if ( + json.scripts && + json.scripts.postinstall && + json.scripts.postinstall.includes('ngcc') + ) { + // if exists, add execution of this script + json.scripts.postinstall = json.scripts.postinstall.replace( + /(.*)(ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points)(.*)/, + '$1ngcc --properties es2015 browser module main$3' + ); + } + return json; + })(host, context); +}; + +export default function () { + return chain([udpateNgccPostinstallScript, formatFiles()]); +} diff --git a/packages/angular/src/schematics/init/init.spec.ts b/packages/angular/src/schematics/init/init.spec.ts index b56d3ccb45ab9..ee08511b38f4c 100644 --- a/packages/angular/src/schematics/init/init.spec.ts +++ b/packages/angular/src/schematics/init/init.spec.ts @@ -45,7 +45,7 @@ describe('init', () => { const packageJson = readJsonInTree(tree, 'package.json'); expect(packageJson.scripts.postinstall).toEqual( - 'ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points' + 'ngcc --properties es2015 browser module main' ); }); diff --git a/packages/angular/src/schematics/init/init.ts b/packages/angular/src/schematics/init/init.ts index 5639d729aac79..572d6c1f4116b 100755 --- a/packages/angular/src/schematics/init/init.ts +++ b/packages/angular/src/schematics/init/init.ts @@ -148,8 +148,7 @@ export function setDefaults(options: Schema): Rule { function addPostinstall(): Rule { return updateJsonInTree('package.json', (json, context) => { json.scripts = json.scripts || {}; - const command = - 'ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points'; + const command = 'ngcc --properties es2015 browser module main'; if (!json.scripts.postinstall) { json.scripts.postinstall = command; } else if (!json.scripts.postinstall.includes('ngcc')) {