Angular 12 + webpack 5 + AOT 构建 - 无法生成 ngfactory 文件

标签 angular webpack angular2-aot

我正在将我的 Angular 8 项目升级到 Angular 12,同时升级到 webpack 5。该项目在开发模式下构建并运行良好,但在 AOT 模式下无法构建。我的 main-aot.ts 文件正在寻找未生成的 app.module.ngfactory 文件。过程中没有报其他错误。我的问题:

  1. 现在可以使用 AngularWebpackPlugin 而不是 AngularCompilerPlugin 吗?
  2. 我的 main-aot.ts 文件(如下)正确吗?我使用与 Angular 8 相同的内容。
  3. 如果未生成 ngfactory 文件,是否有办法获取错误消息,指出问题的原因?
  4. 我还应该做些什么来创建 ngfactory 文件吗?

注释

  • Angular 的确切版本是 12.2.11。 webpack版本是5.59.1。
  • 我跳过了如下所示的文件内容中的几行。如果需要的话我可以添加更多。

ma​​in-aot.ts

import { platformBrowser } from "@angular/platform-browser";
import { enableProdMode } from "@angular/core";
import { AppModuleNgFactory } from "./app/app.module.ngfactory";

enableProdMode();

platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

webpack-prod.js

module: {
    rules: [
        {
            test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
            loader: '@ngtools/webpack'
        },
        ...
    ]
}
...
plugins: [
    new AngularWebpackPlugin({
        tsconfig: './tsconfig-aot.json',
    }),
    ...
],
...

tsconfig-aot.json

{
    "compilerOptions": {
        ...
    },
    "files": [
        "clientApp/app/app.module.ts",
        "clientApp/main-aot.ts",
        "clientApp/polyfills.ts",
        "clientApp/vendor.ts"
    ],
    "angularCompilerOptions": {
        "skipMetadataEmit": true,
        "trace": true,
        "enableIvy": true  /* Setting false does not help */
    },
    ...
}

构建输出(相关内容)

92% sealing asset processing TerserPlugin(node:6128)
...
modules by path ./node_modules/ 3.31 MiB
  ...
modules by path ./clientApp/ 9.56 KiB
  ./clientApp/vendor.ts + 1 modules 647 bytes [built] [code generated]
  ./clientApp/polyfills.ts + 1 modules 8.64 KiB [built] [code generated]
  ./clientApp/main-aot.ts 296 bytes [built] [code generated]
    92 ms (resolving: 28 ms, restoring: 0 ms, integration: 0 ms, building: 64 ms, storing: 0 ms)
...
ERROR in ./clientApp/main-aot.ts 4:0-64
Module not found: Error: Can't resolve './app/app.module.ngfactory' in 'D:\...\clientApp'
resolve './app/app.module.ngfactory' in 'D:\...\clientApp'
  using description file: D:\...\package.json (relative path: ./clientApp)
    using description file: D:\...\package.json (relative path: ./clientApp/app/app.module.ngfactory)
      (several lines for: no extension, .ts, .js, .json, .css, .scss, .html, and as directory)
        D:\...\clientApp\app\app.module.ngfactory doesn't exist
...

最佳答案

我从 Alan Agius 得到了答案上GitHub :

This is expected. ngfactory files are no longer generated with Ivy. You can remove .ngfactory references.

import { platformBrowser } from "@angular/platform-browser";
import { enableProdMode } from "@angular/core";
import { AppModule } from "./app/app.module";

enableProdMode();

platformBrowser().bootstrapModule(AppModule);

关于Angular 12 + webpack 5 + AOT 构建 - 无法生成 ngfactory 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69681366/

相关文章:

angular - 错误在 "path"找不到模块 : Error: Can't resolve - Angular 6 Webpack

webpack - sass-resource-loader 等效于汇总

javascript - 如何使用 Webpack 将预处理的 CSS 作为字符串导入?

javascript - AOT : Error: Uncaught (in promise): TypeError: Cannot read property '$' of undefined

Angular 2 AOT 不起作用。为什么 "platformBrowser().bootstrapModuleFactory"会拉取带有附加前缀 "/src/linker/ng_module_factory"的链接?

json - 如何在 Typescript 中声明具有特殊类型的变量

angular - 如何在 ngFor 中引用控件?

angular - 如何设置 Angular 通用模块以抓取 Angular 11 应用程序?

angular - @ngtools\webpack AOT 不工作或卡住在 95% emitting

angular - 'pipe' 号码找不到