'ng build' 在环境中production 设置为true 时是否使用AOT 编译?

Does 'ng build' use AOT compilation when production is set to true in environment?

据我了解,在 ng build 期间默认使用 AOT 编译,并且仅在开发 (ng serve) 中需要设置 --aot 标志。

但现在我面前有一个项目,它在多个位置使用了类 和来自@angular/compiler 的方法。然而 'ng build' with production: true 在环境中的结果没有任何失败并且似乎知道编译器。 如果我使用 'ng serve --aot' 但是我会收到预期的 'Uncaught Error: Runtime compiler is not loaded'.

所以怎么回事?是否默认使用AOT

顺便说一句:该项目使用 Angular 版本 ^4.0.0。


编辑

@Melou 和@PierreDuc 向我展示了我错在哪里:ng build --prodng build -e prod 不同。环境中的生产设置不会改变编译过程。 --prod 还设置了 --target=production 这将触发 AOT 编译。

如果您使用 Angular 4.0.0,您必须使用 angular-cli > 1.0.0,它在生产模式下默认使用 aot。

Angular-cli 替换您的部分代码以使其工作。

例如,在您的 main.ts 中,您使用 bootstrapModule,它被替换为 bootstrapModuleFactory

如果你检查生成的 dist/main.xxxxx.bundle.js(例如使用像 grep grep bootstrapModuleFactory main.xxxxx.bundle.js 这样的工具),你会查看对 bootstrapModuleFactory

的调用

使用build command默认不使用AOT。仅当您添加 --prod 参数时。这会将 --target 设置为生产环境,启用 AOT 并禁用源映射:

# these are equivalent
ng build --target=production --environment=prod
ng build --prod --env=prod
ng build --prod