diff --git a/packages/compiler-cli/ngcc/src/packages/build_marker.ts b/packages/compiler-cli/ngcc/src/packages/build_marker.ts index 3703183d63..d9b99ff651 100644 --- a/packages/compiler-cli/ngcc/src/packages/build_marker.ts +++ b/packages/compiler-cli/ngcc/src/packages/build_marker.ts @@ -59,16 +59,20 @@ export function markAsProcessed( } const scripts = packageJson.scripts || (packageJson.scripts = {}); - scripts.prepublishOnly__ivy_ngcc_bak = - scripts.prepublishOnly__ivy_ngcc_bak || scripts.prepublishOnly; - - scripts.prepublishOnly = 'node --eval \"console.error(\'' + + const oldPrepublishOnly = scripts.prepublishOnly; + const newPrepublishOnly = 'node --eval \"console.error(\'' + 'ERROR: Trying to publish a package that has been compiled by NGCC. This is not allowed.\\n' + 'Please delete and rebuild the package, without compiling with NGCC, before attempting to publish.\\n' + 'Note that NGCC may have been run by importing this package into another project that is being built with Ivy enabled.\\n' + '\')\" ' + '&& exit 1'; + if (oldPrepublishOnly && (oldPrepublishOnly !== newPrepublishOnly)) { + scripts.prepublishOnly__ivy_ngcc_bak = oldPrepublishOnly; + } + + scripts.prepublishOnly = newPrepublishOnly; + // Just in case this package.json was synthesized due to a custom configuration // we will ensure that the path to the containing folder exists before we write the file. fs.ensureDir(dirname(packageJsonPath)); diff --git a/packages/compiler-cli/ngcc/test/packages/build_marker_spec.ts b/packages/compiler-cli/ngcc/test/packages/build_marker_spec.ts index 5ce34281aa..1eeb377d1b 100644 --- a/packages/compiler-cli/ngcc/test/packages/build_marker_spec.ts +++ b/packages/compiler-cli/ngcc/test/packages/build_marker_spec.ts @@ -147,6 +147,26 @@ runInEachFileSystem(() => { expect(pkg.scripts.prepublishOnly).toContain('This is not allowed'); expect(pkg.scripts.prepublishOnly__ivy_ngcc_bak).toBe(prepublishOnly); }); + + it(`should not keep backup of overwritten 'prepublishOnly' script`, () => { + const COMMON_PACKAGE_PATH = _('/node_modules/@angular/common/package.json'); + const fs = getFileSystem(); + let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)); + + markAsProcessed(fs, pkg, COMMON_PACKAGE_PATH, ['fesm2015']); + + pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)); + expect(pkg.scripts.prepublishOnly).toContain('This is not allowed'); + expect(pkg.scripts.prepublishOnly__ivy_ngcc_bak).toBeUndefined(); + + // Running again, now that there is `prepublishOnly` script (created by `ngcc`), it should + // still not back it up as `prepublishOnly__ivy_ngcc_bak`. + markAsProcessed(fs, pkg, COMMON_PACKAGE_PATH, ['fesm2015']); + + pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)); + expect(pkg.scripts.prepublishOnly).toContain('This is not allowed'); + expect(pkg.scripts.prepublishOnly__ivy_ngcc_bak).toBeUndefined(); + }); }); describe('hasBeenProcessed', () => {