问题描述
我正在尝试完成Angular的AOT教程:
https://angular.io/docs/ts/latest/cookbook/aot -compiler.html
I am trying to complete Angular's AOT tutorial:
https://angular.io/docs/ts/latest/cookbook/aot-compiler.html
使用ngc
部分有效,并且生成aot
文件夹.但是,当涉及到rollup
部分的摇树时,我碰到了这个错误:
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)
我想念什么吗?
@角度:2.4.4
汇总:0.41.4
@angular: 2.4.4
rollup: 0.41.4
推荐答案
Angular的AOT教程似乎缺少了一步.使用ngc
后,它仅在aot
文件夹中生成ts
文件.下一步,您需要再次编译这些文件,以生成必要的js
文件.
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.
这里有两个重要说明:
- 您需要将这些
ts
文件编译为es2015
模块,以便从摇树"中受益.这意味着必须再有一个配置文件(tsconfig-compile-aot.json
)仅指向main-aot.ts
文件. - 在步骤1之后,项目中所有相关的
ts
文件将被编译为es2015
模块.如果在开发环境中将systemjs
用作模块加载器和commonjs
模块(如Angular的教程中所述),则在使用rollup
之后,您需要将ts
文件再编译一次,作为commonjs
模块,以避免systemjs
出现问题.
- 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
.
以下是确切步骤:
- 使用
ngc
和tsconfig-aot.json
将app.module
编译为es2015
模块到aot
文件夹中. - 再次使用
tsc
和tsconfig-compile-aot.json
编译main-aot.ts
文件作为es2015
模块,并生成您的js
文件. - 将输入文件(
main-aot.js
)传递到rollup
.现在,它应该会生成您的输出文件,而不会出现任何错误. - 如果要继续使用
systemjs
进行开发,请使用tsconfig.json
编译app/ts
文件,以将其再次转换为commonjs
模块.
- 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.
我创建了一个使用以下步骤的演示应用程序:
https://github.com/forCrowd/Labs-Angular-AOTConfiguration
I have created a demo application that is using these steps:
https://github.com/forCrowd/Labs-Angular-AOTConfiguration
- 安装节点软件包
- 运行
gulp
-default
任务 - 针对开发-SystemJS"案例打开
index.html
- 为生产-AOT和汇总摇树"案例打开
index-aot.html
- 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
它还包含build-invalid
gulp任务,该任务跳过了第二步,表明它导致出现无法解析'app.module.ngfactory'"错误.
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.
这篇关于角度AOT&汇总-无法解析"app.module.ngfactory"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!