问题描述
我正在尝试将已经与延迟加载模块一起使用的App转换为AOT.我正在使用@ ngtools/webpack工具包来编译AOT代码,但是我遇到了一个错误,即Angular似乎找不到懒加载模块的代码.
I am trying to convert an App that was already working with Lazy loaded modules into AOT. I am using the @ngtools/webpack toolkit to compile the AOT code, however I am getting into an error that Angular cant find the Lazy loaded module's code as it seems.
ERROR in ./src/ngfactory async
Module not found: Error: Can't resolve '/Library/WebServer/Documents/envato-teams/src/ngfactory/src/app/components/container/projects.ngfactory.ts' in '/Library/WebServer/Documents/envato-teams/src/ngfactory'
@ ./src/ngfactory async
@ ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js
@ ./src/ngfactory/src/app/app.module.ngfactory.ts
@ ./src/main.aot.ts
@ multi main
在我的应用程序路由定义中值得一提的是,该项目的模块延迟加载:
Worth mention in my app routes definition this project's module is loaded lazily :
{
path: 'projects', loadChildren: './components/container/projects#ProjectModule'
},
这是我的设置的样子:
tsconfig:
...
"angularCompilerOptions": {
"genDir": "./src/ngfactory",
"entryModule": "src/app/app.module#AppModule"
}
...
Webpack:
new ngtools.AotPlugin({
tsConfigPath: './tsconfig.aot.json',
}),
Main.aot.ts
Main.aot.ts
/*
* Providers provided by Angular
*/
import { platformBrowser } from '@angular/platform-browser';
import { AppModuleNgFactory } from './ngfactory/src/app/app.module.ngfactory';
import { Servicesconfig } from './app/services/index';
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
在webpack中,我通过@ ngtools/Webpack来编译ts文件:
In webpack I am compiling ts files with @ngtools/Webpack by doing :
// Support for .ts files.
{
test: /\.ts$/,
loaders: ['@ngtools/webpack'],
exclude: [/\.(spec|e2e)\.ts$/]
},
谢谢您的帮助!
推荐答案
我在为AOT和延迟加载的模块而苦苦挣扎.
I was struggling myself with AOT and Lazy loaded modules.
选择一个或另一个并不是产品构建的选择.
即使我真的确实需要这些功能,我也无法获得它们,不得不放弃.直到今天!
Even tho I really needed those features together, I was not able to get them and had to give up. Until today !
angular-cli
两天前发布了一个版本: 1.0.0-beta.21 其中支持AOT和延迟加载!
angular-cli
made a release two days ago : 1.0.0-beta.21 which supports AOT and Lazy loading !
在angular-cli项目中:
In your angular-cli project :
npm cache clean
npm install --save-dev angular-cli@latest
ng init
享受!
PS:非常感谢angular-cli团队在这里做出了出色的工作……!
PS : Big thanks to angular-cli team which made an awesome work here ... !
我做了一些基准测试:
EDIT :
I did some benchmarks :
+-----------------------+-------------+--------------+-----------+-------------+
| | Main bundle | Chunk 0 | Scripting | First paint |
+-----------------------+-------------+--------------+-----------+-------------+
| ng serve | 4.5 MB | Not splitted | 6075 ms | 5500+ ms |
| ng serve --prod | 334 KB | Not splitted | 5587 ms | 4750+ ms |
| ng serve --aot | 3.3 MB | 326 KB | 4011 ms | 4400+ ms |
| ng serve --prod --aot | 243 KB | 18.1 Kb | 3860 ms | 4250+ ms |
+-----------------------+-------------+--------------+-----------+-------------+
(结果不是很好,因为我打开了很多东西,有3个显示器,笔记本电脑也很疼^ __ ^).
(results aren't super good because I have a lot of things opened and 3 monitors and my laptop is in pain ^__^).
这就是我们可以记住的:
---prod --aot
版本大小比--prod
版本
小 27%-编写脚本时,--prod --aot
版本比--prod
版本快 31%-AOT很酷!
-可能存在一个不带aot
标志的错误,因为除非我错过了什么,否则我找不到我期望的延迟加载块,并且在主程序包中找到了假定的延迟加载代码.我在Github上打开了问题.
Here's what we can remember from that :
- The --prod --aot
build size is 27% smaller than --prod
build
- The --prod --aot
build is 31% faster when scripting than --prod
build
- AOT is cool !
- There's probably a bug without aot
flag because unless I missed something, I couldn't find the lazy loaded chunk I expected and I found the supposed lazy loaded code into the main bundle. I opened an issue on Github.
这篇关于延迟加载的Angular2 AOT无法解析[延迟模块的路径] .ngfactory.ts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!