2018-07-16 03:49:56 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-08-28 06:32:01 -04:00
|
|
|
import {existsSync, readFileSync, readdirSync, statSync} from 'fs';
|
|
|
|
import * as mockFs from 'mock-fs';
|
2018-07-30 13:06:51 -04:00
|
|
|
import {join} from 'path';
|
2018-08-09 10:59:10 -04:00
|
|
|
const Module = require('module');
|
2018-07-16 03:49:56 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
import {mainNgcc} from '../../src/main';
|
|
|
|
import {getAngularPackagesFromRunfiles, resolveNpmTreeArtifact} from '../../../test/runfile_helpers';
|
2018-07-16 03:49:56 -04:00
|
|
|
|
2018-08-28 06:32:01 -04:00
|
|
|
describe('ngcc main()', () => {
|
|
|
|
beforeEach(createMockFileSystem);
|
|
|
|
afterEach(restoreRealFileSystem);
|
2018-07-16 03:49:56 -04:00
|
|
|
|
2018-07-24 09:53:23 -04:00
|
|
|
it('should run ngcc without errors for fesm2015', () => {
|
2019-03-20 09:47:58 -04:00
|
|
|
expect(() => mainNgcc({baseSourcePath: '/node_modules', formats: ['fesm2015']})).not.toThrow();
|
2018-07-16 03:49:56 -04:00
|
|
|
});
|
2018-07-24 09:53:23 -04:00
|
|
|
|
2018-07-25 06:06:32 -04:00
|
|
|
it('should run ngcc without errors for fesm5', () => {
|
2019-03-20 09:47:58 -04:00
|
|
|
expect(() => mainNgcc({baseSourcePath: '/node_modules', formats: ['fesm5']})).not.toThrow();
|
2018-07-25 06:06:32 -04:00
|
|
|
});
|
|
|
|
|
2018-07-24 09:53:23 -04:00
|
|
|
it('should run ngcc without errors for esm2015', () => {
|
2019-03-20 09:47:58 -04:00
|
|
|
expect(() => mainNgcc({baseSourcePath: '/node_modules', formats: ['esm2015']})).not.toThrow();
|
2018-07-24 09:53:23 -04:00
|
|
|
});
|
2018-07-25 06:06:32 -04:00
|
|
|
|
|
|
|
it('should run ngcc without errors for esm5', () => {
|
2019-03-20 09:47:58 -04:00
|
|
|
expect(() => mainNgcc({baseSourcePath: '/node_modules', formats: ['esm5']})).not.toThrow();
|
2018-08-28 06:32:01 -04:00
|
|
|
});
|
2019-03-20 09:47:58 -04:00
|
|
|
|
|
|
|
it('should only compile the given package entry-point (and its dependencies)', () => {
|
|
|
|
mainNgcc({
|
|
|
|
baseSourcePath: '/node_modules',
|
|
|
|
formats: ['esm2015'],
|
|
|
|
targetEntryPointPath: '@angular/common'
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(loadPackage('@angular/common').__modified_by_ngcc__).toEqual({
|
|
|
|
esm2015: '0.0.0-PLACEHOLDER',
|
|
|
|
es2015: '0.0.0-PLACEHOLDER',
|
|
|
|
});
|
|
|
|
expect(loadPackage('@angular/core').__modified_by_ngcc__).toEqual({
|
|
|
|
esm2015: '0.0.0-PLACEHOLDER',
|
|
|
|
es2015: '0.0.0-PLACEHOLDER',
|
|
|
|
});
|
|
|
|
expect(loadPackage('@angular/common/testing').__modified_by_ngcc__).toBeUndefined();
|
|
|
|
expect(loadPackage('@angular/common/http').__modified_by_ngcc__).toBeUndefined();
|
|
|
|
});
|
2018-08-28 06:32:01 -04:00
|
|
|
});
|
|
|
|
|
2018-07-25 06:06:32 -04:00
|
|
|
|
2018-08-28 06:32:01 -04:00
|
|
|
function createMockFileSystem() {
|
2019-03-20 09:47:58 -04:00
|
|
|
mockFs({
|
|
|
|
'/node_modules/@angular': loadAngularPackages(),
|
|
|
|
'/node_modules/rxjs': loadDirectory(resolveNpmTreeArtifact('rxjs', 'index.js')),
|
|
|
|
});
|
2018-08-09 10:59:10 -04:00
|
|
|
spyOn(Module, '_resolveFilename').and.callFake(mockResolve);
|
2018-08-28 06:32:01 -04:00
|
|
|
}
|
2018-07-25 06:06:32 -04:00
|
|
|
|
2018-08-28 06:32:01 -04:00
|
|
|
function restoreRealFileSystem() {
|
|
|
|
mockFs.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-25 13:48:27 -05:00
|
|
|
/** Load the built Angular packages into an in-memory structure. */
|
|
|
|
function loadAngularPackages(): Directory {
|
2018-08-28 06:32:01 -04:00
|
|
|
const packagesDirectory: Directory = {};
|
2019-01-25 13:48:27 -05:00
|
|
|
|
|
|
|
getAngularPackagesFromRunfiles().forEach(
|
|
|
|
({name, pkgPath}) => { packagesDirectory[name] = loadDirectory(pkgPath); });
|
|
|
|
|
2018-08-28 06:32:01 -04:00
|
|
|
return packagesDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load real files from the filesystem into an "in-memory" structure,
|
|
|
|
* which can be used with `mock-fs`.
|
|
|
|
* @param directoryPath the path to the directory we want to load.
|
|
|
|
*/
|
|
|
|
function loadDirectory(directoryPath: string): Directory {
|
|
|
|
const directory: Directory = {};
|
|
|
|
|
|
|
|
readdirSync(directoryPath).forEach(item => {
|
|
|
|
const itemPath = join(directoryPath, item);
|
|
|
|
if (statSync(itemPath).isDirectory()) {
|
|
|
|
directory[item] = loadDirectory(itemPath);
|
|
|
|
} else {
|
|
|
|
directory[item] = readFileSync(itemPath, 'utf-8');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Directory {
|
|
|
|
[pathSegment: string]: string|Directory;
|
|
|
|
}
|
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
function mockResolve(request: string): string|null {
|
|
|
|
if (existsSync(request)) {
|
|
|
|
const stat = statSync(request);
|
2018-08-09 10:59:10 -04:00
|
|
|
if (stat.isFile()) {
|
2019-03-20 09:47:58 -04:00
|
|
|
return request;
|
2018-08-09 10:59:10 -04:00
|
|
|
} else if (stat.isDirectory()) {
|
2019-03-20 09:47:58 -04:00
|
|
|
const pIndex = mockResolve(request + '/index');
|
2018-08-09 10:59:10 -04:00
|
|
|
if (pIndex && existsSync(pIndex)) {
|
|
|
|
return pIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const ext of ['.js', '.d.ts']) {
|
2019-03-20 09:47:58 -04:00
|
|
|
if (existsSync(request + ext)) {
|
|
|
|
return request + ext;
|
2018-08-09 10:59:10 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-20 09:47:58 -04:00
|
|
|
if (request.indexOf('/node_modules') === 0) {
|
|
|
|
// We already tried adding node_modules so give up.
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return mockResolve(join('/node_modules', request));
|
|
|
|
}
|
2019-01-25 13:44:49 -05:00
|
|
|
}
|
2019-03-20 09:47:58 -04:00
|
|
|
|
|
|
|
function loadPackage(packageName: string) {
|
|
|
|
return JSON.parse(readFileSync(`/node_modules/${packageName}/package.json`, 'utf8'));
|
|
|
|
}
|