问题
I am trying to complete Angular's AOT tutorial:
https://angular.io/docs/ts/latest/cookbook/aot-compiler.html
Using ngc part works and it generates aot folder.
However, when it comes to tree-shaking with rollup part, I bump into this error:
Error: Could not resolve '../aot/app/app.module.ngfactory' from ...\app\main-aot.js
at Error (native)
at ...\node_modules\rollup-plugin-node-resolve\dist\rollup-plugin-node-resolve.cjs.js:78:21
at ...\node_modules\resolve\lib\async.js:56:18
at load (...\node_modules\resolve\lib\async.js:70:43)
at onex (...\node_modules\resolve\lib\async.js:93:31)
at ...\node_modules\resolve\lib\async.js:23:47
at FSReqWrap.oncomplete (fs.js:82:15)
Am I missing something?
@angular: 2.4.4
rollup: 0.41.4
回答1:
Angular's AOT tutorial seems missing a step; after using ngc, it generates only ts files in aot folder. As the next step, you need to compile these files one more time, to generate the necessary js files.
There are two important notes here:
- You need to compile these
tsfiles ases2015modules, in order to benefit from "tree-shaking". This means there must be one more config file (tsconfig-compile-aot.json) that only points tomain-aot.tsfile. - After step 1, all related
tsfiles in your project will be compiled ases2015modules. If you're usingsystemjsas a module loader andcommonjsmodules in your development environment (as in Angular's tutorials), after usingrollup, you need to compile yourtsfiles one more time ascommonjsmodules, in order to avoid issues withsystemjs.
Here are the exact steps:
- Compile your
app.modulewithngcandtsconfig-aot.jsonintoaotfolder ases2015modules. - Compile
main-aot.tsfiles withtscandtsconfig-compile-aot.json, again ases2015modules and generate yourjsfiles. - Pass your entry file (
main-aot.js) torollup. Now it should generate your output file without any errors. - When you want to continue to your development with
systemjs, compile yourapp/tsfiles withtsconfig.jsonto convert them tocommonjsmodules again.
I have created a demo application that is using these steps:
https://github.com/forCrowd/Labs-Angular-AOTConfiguration
- Install node packages
- Run
gulp-defaulttask - Open
index.htmlfor "Development - SystemJS" case - Open
index-aot.htmlfor "Production - AOT & Rollup Tree-shaking" case
Also it contains build-invalid gulp task that skips this second step, to show that it results in having "Could not resolve 'app.module.ngfactory'" error.
来源:https://stackoverflow.com/questions/41740545/angular-aot-rollup-could-not-resolve-app-module-ngfactory