问题
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
ts
files ases2015
modules, 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.ts
file. - After step 1, all related
ts
files in your project will be compiled ases2015
modules. If you're usingsystemjs
as a module loader andcommonjs
modules in your development environment (as in Angular's tutorials), after usingrollup
, you need to compile yourts
files one more time ascommonjs
modules, in order to avoid issues withsystemjs
.
Here are the exact steps:
- Compile your
app.module
withngc
andtsconfig-aot.json
intoaot
folder ases2015
modules. - Compile
main-aot.ts
files withtsc
andtsconfig-compile-aot.json
, again ases2015
modules and generate yourjs
files. - 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/ts
files withtsconfig.json
to convert them tocommonjs
modules again.
I have created a demo application that is using these steps:
https://github.com/forCrowd/Labs-Angular-AOTConfiguration
- Install node packages
- Run
gulp
-default
task - Open
index.html
for "Development - SystemJS" case - Open
index-aot.html
for "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