From 5f23a1223f32dbde138214823a4f0aeaf541ae4d Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Fri, 15 Dec 2017 14:51:42 -0800 Subject: [PATCH] fix(compiler-cli): do not force type checking on .js files The compiler host would force any file that is in node_modules into the list of files that needed to be type checked which captures .js files if `allowJs` is set to `true`. This should have only forced .d.ts files into the project to enable generation of factories. Fixes: #19757 --- .../src/transformers/compiler_host.ts | 2 +- packages/compiler-cli/test/ngc_spec.ts | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/transformers/compiler_host.ts b/packages/compiler-cli/src/transformers/compiler_host.ts index d095ffda54..a66e46ab62 100644 --- a/packages/compiler-cli/src/transformers/compiler_host.ts +++ b/packages/compiler-cli/src/transformers/compiler_host.ts @@ -130,7 +130,7 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos moduleName, containingFile.replace(/\\/g, '/'), this.options, this, this.moduleResolutionCache) .resolvedModule; - if (rm && this.isSourceFile(rm.resolvedFileName)) { + if (rm && this.isSourceFile(rm.resolvedFileName) && DTS.test(rm.resolvedFileName)) { // Case: generateCodeForLibraries = true and moduleName is // a .d.ts file in a node_modules folder. // Need to set isExternalLibraryImport to false so that generated files for that file diff --git a/packages/compiler-cli/test/ngc_spec.ts b/packages/compiler-cli/test/ngc_spec.ts index 5702658061..7008ae3e0c 100644 --- a/packages/compiler-cli/test/ngc_spec.ts +++ b/packages/compiler-cli/test/ngc_spec.ts @@ -1667,6 +1667,38 @@ describe('ngc transformer command-line', () => { `); expect(main(['-p', path.join(basePath, 'src/tsconfig.json')])).toBe(0); }); + + it('should not type check a .js files from node_modules with allowJs', () => { + write('src/tsconfig.json', `{ + "extends": "../tsconfig-base.json", + "compilerOptions": { + "noEmitOnError": true, + "allowJs": true, + "declaration": false + }, + "files": ["test-module.ts"] + }`); + write('src/test-module.ts', ` + import {Component, NgModule} from '@angular/core'; + import 'my-library'; + + @Component({ + template: 'hello' + }) + export class HelloCmp {} + + @NgModule({ + declarations: [HelloCmp], + }) + export class MyModule {} + `); + write('src/node_modules/t.txt', ``); + write('src/node_modules/my-library/index.js', ` + export someVar = 1; + export someOtherVar = undefined + 1; + `); + expect(main(['-p', path.join(basePath, 'src/tsconfig.json')])).toBe(0); + }); }); describe('formatted messages', () => {