Skip to content

Commit

Permalink
fix(angular): adjust ngcc postinstall command (#4641)
Browse files Browse the repository at this point in the history
  • Loading branch information
juristr authored Mar 5, 2021
1 parent d50beb0 commit 1716c0b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/angular/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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')

This comment has been minimized.

Copy link
@matheo

matheo Mar 10, 2021

Contributor

@juristr cannot use optional-chain here?

) {
// 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()]);
}
2 changes: 1 addition & 1 deletion packages/angular/src/schematics/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});

Expand Down
3 changes: 1 addition & 2 deletions packages/angular/src/schematics/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down

0 comments on commit 1716c0b

Please sign in to comment.