From 3211432d2ab62b9e941d980a27b1825f5395dc9e Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Tue, 24 Jul 2018 16:53:23 +0300 Subject: [PATCH] feat(ivy): add support for esm2015 and esm5 in ngcc `PackageParser` (#25406) Since non-flat module formats (esm2015, esm5) have different structure than their flat counterparts (and since we are operating on JS files inside `node_modules/`, we need to configure TS to include deeply nested JS files (by specifying a sufficiently high `maxNodeModuleJsDepth`). Remains to be determined if this has any (noticeable) performance implications. PR Close #25406 --- packages/compiler-cli/src/ngcc/src/main.ts | 5 +++-- .../ngcc/src/transform/package_transformer.ts | 7 ++++++- packages/compiler-cli/test/ngcc/ngcc_spec.ts | 18 ++++++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/compiler-cli/src/ngcc/src/main.ts b/packages/compiler-cli/src/ngcc/src/main.ts index fece2d1369..878507821c 100644 --- a/packages/compiler-cli/src/ngcc/src/main.ts +++ b/packages/compiler-cli/src/ngcc/src/main.ts @@ -10,12 +10,13 @@ import {PackageTransformer} from './transform/package_transformer'; export function mainNgcc(args: string[]): number { const packagePath = resolve(args[0]); + const format = args[1] || 'fesm2015'; - // TODO: find all the package tyoes to transform + // TODO: find all the package types to transform // TODO: error/warning logging/handling etc const transformer = new PackageTransformer(); - transformer.transform(packagePath, 'fesm2015'); + transformer.transform(packagePath, format); return 0; } diff --git a/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts b/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts index ab974015c0..67f2658643 100644 --- a/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts +++ b/packages/compiler-cli/src/ngcc/src/transform/package_transformer.ts @@ -46,7 +46,12 @@ export class PackageTransformer { const targetNodeModules = sourceNodeModules.replace(/node_modules$/, 'node_modules_ngtsc'); const entryPointPaths = getEntryPoints(packagePath, format); entryPointPaths.forEach(entryPointPath => { - const options: ts.CompilerOptions = {allowJs: true, rootDir: entryPointPath}; + const options: ts.CompilerOptions = { + allowJs: true, + maxNodeModuleJsDepth: Infinity, + rootDir: entryPointPath, + }; + const host = ts.createCompilerHost(options); const packageProgram = ts.createProgram([entryPointPath], options, host); const entryPointFile = packageProgram.getSourceFile(entryPointPath) !; diff --git a/packages/compiler-cli/test/ngcc/ngcc_spec.ts b/packages/compiler-cli/test/ngcc/ngcc_spec.ts index aab460be00..bb8fcd8ff4 100644 --- a/packages/compiler-cli/test/ngcc/ngcc_spec.ts +++ b/packages/compiler-cli/test/ngcc/ngcc_spec.ts @@ -72,11 +72,11 @@ describe('ngcc behavioral tests', () => { setupNodeModules(support); }); - it('should run ngcc without errors', () => { + it('should run ngcc without errors for fesm2015', () => { const nodeModulesPath = path.join(basePath, 'node_modules'); console.error(nodeModulesPath); const commonPath = path.join(nodeModulesPath, '@angular/common'); - const exitCode = mainNgcc([commonPath]); + const exitCode = mainNgcc([commonPath, 'fesm2015']); console.warn(find('node_modules_ngtsc').filter(p => p.endsWith('.js') || p.endsWith('map'))); @@ -85,4 +85,18 @@ describe('ngcc behavioral tests', () => { expect(exitCode).toBe(0); }); + + it('should run ngcc without errors for esm2015', () => { + const nodeModulesPath = path.join(basePath, 'node_modules'); + console.error(nodeModulesPath); + const commonPath = path.join(nodeModulesPath, '@angular/common'); + const exitCode = mainNgcc([commonPath, 'esm2015']); + + console.warn(find('node_modules_ngtsc').filter(p => p.endsWith('.js') || p.endsWith('map'))); + + console.warn(cat('node_modules_ngtsc/@angular/common/esm2015/src/directives/ng_if.js').stdout); + console.warn(cat('node_modules_ngtsc/@angular/common/esm2015/http/src/module.js').stdout); + + expect(exitCode).toBe(0); + }); });