diff --git a/packages/compiler-cli/ngcc/src/packages/build_marker.ts b/packages/compiler-cli/ngcc/src/packages/build_marker.ts index 53288f1bcd..8b23e064d6 100644 --- a/packages/compiler-cli/ngcc/src/packages/build_marker.ts +++ b/packages/compiler-cli/ngcc/src/packages/build_marker.ts @@ -22,18 +22,24 @@ export const NGCC_VERSION = '0.0.0-PLACEHOLDER'; * @param packageJson The parsed contents of the package.json file for the entry-point. * @param format The entry-point format property in the package.json to check. * @returns true if the entry-point and format have already been processed with this ngcc version. - * @throws Error if the entry-point has already been processed with a different ngcc - * version. + * @throws Error if the `packageJson` property is not an object. + * @throws Error if the entry-point has already been processed with a different ngcc version. */ export function hasBeenProcessed(packageJson: any, format: string): boolean { - const compiledVersion = - packageJson && packageJson.__modified_by_ngcc__ && packageJson.__modified_by_ngcc__[format]; - if (compiledVersion && compiledVersion !== NGCC_VERSION) { + if (typeof packageJson !== 'object') { + throw new Error('`packageJson` parameter is invalid. It parameter must be an object.'); + } + if (!packageJson.__modified_by_ngcc__) { + return false; + } + if (Object.keys(packageJson.__modified_by_ngcc__) + .some(property => packageJson.__modified_by_ngcc__[property] !== NGCC_VERSION)) { throw new Error( 'The ngcc compiler has changed since the last ngcc build.\n' + 'Please completely remove `node_modules` and try again.'); } - return !!compiledVersion; + + return packageJson.__modified_by_ngcc__[format] === NGCC_VERSION; } /** 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 3977b9e1e7..6b13e5e709 100644 --- a/packages/compiler-cli/ngcc/test/packages/build_marker_spec.ts +++ b/packages/compiler-cli/ngcc/test/packages/build_marker_spec.ts @@ -10,7 +10,7 @@ import {readFileSync, writeFileSync} from 'fs'; import * as mockFs from 'mock-fs'; import {AbsoluteFsPath} from '../../../src/ngtsc/path'; -import {checkMarker, writeMarker} from '../../src/packages/build_marker'; +import {checkMarker, hasBeenProcessed, writeMarker} from '../../src/packages/build_marker'; import {EntryPoint} from '../../src/packages/entry_point'; function createMockFileSystem() { @@ -150,4 +150,52 @@ describe('Marker files', () => { 'Please completely remove `node_modules` and try again.'); }); }); + + describe('hasBeenProcessed', () => { + it('should return true if the marker exists for the given format property', () => { + expect( + hasBeenProcessed({__modified_by_ngcc__: {'fesm2015': '0.0.0-PLACEHOLDER'}}, 'fesm2015')) + .toBe(true); + }); + it('should return false if the marker does not exist for the given format property', () => { + expect(hasBeenProcessed({__modified_by_ngcc__: {'fesm2015': '0.0.0-PLACEHOLDER'}}, 'module')) + .toBe(false); + }); + it('should return false if the no markers exist', + () => { expect(hasBeenProcessed({}, 'module')).toBe(false); }); + it('should throw an Error if the packageJson is not an object', () => { + expect(() => hasBeenProcessed(undefined, 'fesm2015')) + .toThrowError('`packageJson` parameter is invalid. It parameter must be an object.'); + expect( + () => hasBeenProcessed( + '{"__modified_by_ngcc__": {"fesm2015": "0.0.0-PLACEHOLDER"}}', 'fesm2015')) + .toThrowError('`packageJson` parameter is invalid. It parameter must be an object.'); + }); + it('should throw an Error if the format has been compiled with a different version.', () => { + expect(() => hasBeenProcessed({__modified_by_ngcc__: {'fesm2015': '8.0.0'}}, 'fesm2015')) + .toThrowError( + 'The ngcc compiler has changed since the last ngcc build.\n' + + 'Please completely remove `node_modules` and try again.'); + }); + it('should throw an Error if any format has been compiled with a different version.', () => { + expect(() => hasBeenProcessed({__modified_by_ngcc__: {'fesm2015': '8.0.0'}}, 'module')) + .toThrowError( + 'The ngcc compiler has changed since the last ngcc build.\n' + + 'Please completely remove `node_modules` and try again.'); + expect( + () => hasBeenProcessed( + {__modified_by_ngcc__: {'module': '0.0.0-PLACEHOLDER', 'fesm2015': '8.0.0'}}, + 'module')) + .toThrowError( + 'The ngcc compiler has changed since the last ngcc build.\n' + + 'Please completely remove `node_modules` and try again.'); + expect( + () => hasBeenProcessed( + {__modified_by_ngcc__: {'module': '0.0.0-PLACEHOLDER', 'fesm2015': '8.0.0'}}, + 'fesm2015')) + .toThrowError( + 'The ngcc compiler has changed since the last ngcc build.\n' + + 'Please completely remove `node_modules` and try again.'); + }); + }); });