feat(ivy): implement ngcc build marker (#25557)
`ngcc` adds marker files to each folder that has been compiled, containing the version of the ngcc used. When compiling, it will ignore folders that contain these marker files, as long as the version matches. PR Close #25557
This commit is contained in:

committed by
Misko Hevery

parent
2a672a97ab
commit
b0cb134815
@ -6,8 +6,10 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {existsSync, readFileSync, writeFileSync} from 'fs';
|
||||
import * as mockFs from 'mock-fs';
|
||||
import {EntryPoint, findAllPackageJsonFiles, getEntryPoints} from '../../src/transform/utils';
|
||||
|
||||
import {EntryPoint, checkMarkerFile, findAllPackageJsonFiles, getEntryPoints, writeMarkerFile} from '../../src/transform/utils';
|
||||
|
||||
function createMockFileSystem() {
|
||||
mockFs({
|
||||
@ -95,6 +97,11 @@ describe('EntryPoint', () => {
|
||||
const entryPoint = new EntryPoint('/foo/bar', '../baz/qux/../quux.js', '/typings/foo/bar.d.ts');
|
||||
expect(entryPoint.entryFileName).toBe('/foo/baz/quux.js');
|
||||
});
|
||||
|
||||
it('should expose the package root for the entry point file', () => {
|
||||
const entryPoint = new EntryPoint('/foo/bar', '../baz/qux/../quux.js', '/typings/foo/bar.d.ts');
|
||||
expect(entryPoint.packageRoot).toBe('/foo/bar');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findAllPackageJsonFiles()', () => {
|
||||
@ -138,7 +145,14 @@ describe('getEntryPoints()', () => {
|
||||
afterEach(restoreRealFileSystem);
|
||||
|
||||
it('should return the entry points for the specified format from each `package.json`', () => {
|
||||
const entryPoints = getEntryPoints('/node_modules/@angular/common', 'fesm2015');
|
||||
const entryPoints = getEntryPoints(
|
||||
[
|
||||
'/node_modules/@angular/common/package.json',
|
||||
'/node_modules/@angular/common/http/package.json',
|
||||
'/node_modules/@angular/common/http/testing/package.json',
|
||||
'/node_modules/@angular/common/testing/package.json'
|
||||
],
|
||||
'fesm2015');
|
||||
entryPoints.forEach(ep => expect(ep).toEqual(jasmine.any(EntryPoint)));
|
||||
|
||||
const sortedPaths = entryPoints.map(x => x.entryFileName).sort();
|
||||
@ -151,17 +165,18 @@ describe('getEntryPoints()', () => {
|
||||
});
|
||||
|
||||
it('should return an empty array if there are no matching `package.json` files', () => {
|
||||
const entryPoints = getEntryPoints('/node_modules/@angular/other', 'fesm2015');
|
||||
const entryPoints = getEntryPoints([], 'fesm2015');
|
||||
expect(entryPoints).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return an empty array if there are no matching formats', () => {
|
||||
const entryPoints = getEntryPoints('/node_modules/@angular/common', 'fesm3000');
|
||||
const entryPoints = getEntryPoints(['/node_modules/@angular/common/package.json'], 'fesm3000');
|
||||
expect(entryPoints).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return an entry point even if the typings are not specified', () => {
|
||||
const entryPoints = getEntryPoints('/node_modules/@angular/no-typings', 'fesm2015');
|
||||
const entryPoints =
|
||||
getEntryPoints(['/node_modules/@angular/no-typings/package.json'], 'fesm2015');
|
||||
expect(entryPoints.length).toEqual(1);
|
||||
expect(entryPoints[0].entryFileName)
|
||||
.toEqual('/node_modules/@angular/no-typings/fesm2015/index.js');
|
||||
@ -169,3 +184,57 @@ describe('getEntryPoints()', () => {
|
||||
expect(entryPoints[0].dtsEntryRoot).toEqual(entryPoints[0].entryRoot);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Marker files', () => {
|
||||
beforeEach(createMockFileSystem);
|
||||
afterEach(restoreRealFileSystem);
|
||||
|
||||
describe('writeMarkerFile', () => {
|
||||
it('should write a file containing the version placeholder', () => {
|
||||
expect(existsSync('/node_modules/@angular/common/__modified_by_ngcc_for_fesm2015__'))
|
||||
.toBe(false);
|
||||
expect(existsSync('/node_modules/@angular/common/__modified_by_ngcc_for_esm5__')).toBe(false);
|
||||
|
||||
writeMarkerFile('/node_modules/@angular/common/package.json', 'fesm2015');
|
||||
expect(existsSync('/node_modules/@angular/common/__modified_by_ngcc_for_fesm2015__'))
|
||||
.toBe(true);
|
||||
expect(existsSync('/node_modules/@angular/common/__modified_by_ngcc_for_esm5__')).toBe(false);
|
||||
expect(
|
||||
readFileSync('/node_modules/@angular/common/__modified_by_ngcc_for_fesm2015__', 'utf8'))
|
||||
.toEqual('0.0.0-PLACEHOLDER');
|
||||
|
||||
writeMarkerFile('/node_modules/@angular/common/package.json', 'esm5');
|
||||
expect(existsSync('/node_modules/@angular/common/__modified_by_ngcc_for_fesm2015__'))
|
||||
.toBe(true);
|
||||
expect(existsSync('/node_modules/@angular/common/__modified_by_ngcc_for_esm5__')).toBe(true);
|
||||
expect(
|
||||
readFileSync('/node_modules/@angular/common/__modified_by_ngcc_for_fesm2015__', 'utf8'))
|
||||
.toEqual('0.0.0-PLACEHOLDER');
|
||||
expect(readFileSync('/node_modules/@angular/common/__modified_by_ngcc_for_esm5__', 'utf8'))
|
||||
.toEqual('0.0.0-PLACEHOLDER');
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkMarkerFile', () => {
|
||||
it('should return false if the marker file does not exist', () => {
|
||||
expect(checkMarkerFile('/node_modules/@angular/common/package.json', 'fesm2015')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true if the marker file exists and contains the correct version', () => {
|
||||
writeFileSync(
|
||||
'/node_modules/@angular/common/__modified_by_ngcc_for_fesm2015__', '0.0.0-PLACEHOLDER',
|
||||
'utf8');
|
||||
expect(checkMarkerFile('/node_modules/@angular/common/package.json', 'fesm2015')).toBe(true);
|
||||
});
|
||||
|
||||
it('should throw if the marker file exists but contains the wrong version', () => {
|
||||
writeFileSync(
|
||||
'/node_modules/@angular/common/__modified_by_ngcc_for_fesm2015__', 'WRONG_VERSION',
|
||||
'utf8');
|
||||
expect(() => checkMarkerFile('/node_modules/@angular/common/package.json', 'fesm2015'))
|
||||
.toThrowError(
|
||||
'The ngcc compiler has changed since the last ngcc build.\n' +
|
||||
'Please completely remove `node_modules` and try again.');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user