Compiler generates ngfactory for file with no angular metadata
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Under some circumstances, the AOT compiler can generate a .ngfactory.ts
file for a file with no Angular metadata:
- must be using Angular CLI
- must be using AOT compilation
- must be using
"module": "commonjs"
in tsconfig (the CLI sets it automatically forng serve
, for performance reasons) - must have a namespace import (like
import * as numeral from 'numeral';
) in a file without angular decorators
When all these conditions are met, rebuilds will fail with the following stack trace:
ERROR in : TypeError: Cannot read property 'text' of undefined
at getErrorSpanForNode (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:7880:40)
at createDiagnosticForNodeInSourceFile (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:7804:20)
at Object.createDiagnosticForNode (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:7800:16)
at error (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:24149:22)
at resolveExternalModule (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:25263:17)
at resolveExternalModuleNameWorker (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:25253:20)
at Object.getExternalModuleFileFromDeclaration (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:45763:32)
at tryGetModuleNameFromDeclaration (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:50792:50)
at Object.getExternalModuleNameLiteral (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:50756:20)
at createRequireCall (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:63361:33)
at visitExportDeclaration (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:63423:45)
at sourceElementVisitor (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:63151:28)
at Object.visitNodes (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:51105:48)
at transformCommonJSModule (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:62890:40)
at transformSourceFile (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:62863:27)
at D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:2601:86
at reduceLeft (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:2274:30)
at D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:2601:42
at transformRoot (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:65720:82)
at Object.map (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:1725:29)
at Object.transformNodes (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:65708:30)
at Object.emitFiles (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:68442:28)
at emitWorker (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72519:33)
at D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72481:66
at runWithCancellationToken (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72573:24)
at Object.emit (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72481:20)
at defaultEmitCallback (D:\sandbox\repro-bug-angular-cli\node_modules\@angular\compiler-cli\src\transformers\program.js:33:20)
at D:\sandbox\repro-bug-angular-cli\node_modules\@angular\compiler-cli\src\transformers\program.js:231:109
at Array.map (<anonymous>)
at AngularCompilerProgram.emit (D:\sandbox\repro-bug-angular-cli\node_modules\@angular\compiler-cli\src\transformers\program.js:231:63)
at AngularCompilerPlugin._emit (D:\sandbox\repro-bug-angular-cli\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:749:49)
at Promise.resolve.then.then.then (D:\sandbox\repro-bug-angular-cli\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:582:54)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Expected behavior
Compilation succeed.
Minimal reproduction of the problem with instructions
This issue was originally reported and reproduced in https://github.com/angular/angular-cli/issues/9036:
git clone https://github.com/victornoel/repro-bug-angular-cli
cd repro-bug-angular-cli
npm i
npm start -- --aot
# edit a file, e.g. add `console.log(1);` to `main.ts`
Environment
Angular version: 5.2.2
For Tooling issues:
- Node version: 8.9.1
- Platform: Windows 10
- Angular CLI: 1.6.6
Others:
/cc @chuckjaz
Issue Analytics
- State:
- Created 6 years ago
- Reactions:11
- Comments:14 (8 by maintainers)
Top Results From Across the Web
Angular compiler options
Produces only one .metadata.json file, which contains all the metadata necessary for symbols exported from the library index. In the created .ngfactory.js ...
Read more >Do the ngfactory files themselves have to be compiled in ...
Whenever I compile an Angular 2 application with ngc , it creates the .ngfactory.ts files for every component and then compiles all the ......
Read more >Retrieve your ngfactory, .metadata.json and .ngsummary.json ...
Since Angular 5 ang Angular CLI 1.5, Typescript files are no longer emitted ... with Angular 4 the compiler only generated the extra...
Read more >Understanding Angular Ivy Library Compilation | lacolaco/tech
In this post, I will explain how to compile Angular libraries with ... The VE compiler generated code as a file named *.ngfactory.js...
Read more >Ahead of Time Compilation - AoT in Angular (Performance ...
As you can see, the JiT compiler produces a *.ngfactory.js file for every component and module. If you check the contents of any...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@chuckjaz looked at this problem yesterday and had this to say:
He also suggested a workaround: type the problematic namespace import as any:
The next major version of Angular CLI will do away with CommonJS for
ng serve
so this should also ameliorate the problem. This is not a real fix though, it will only reduce the symptoms.Are you going to include chuckjaz’s workaround in a release? Would be cool if we don’t have to wait the webpack 4 integration and @angular/cli v6 for the real solution.