使用Webpack构建Angular 9的生产版本

7
我有一个Angular应用程序,我经常使用最新的Webpack打包工具进行更新和构建。几天前,我将该项目升级到Angular 9,并遇到了一些无法解决的问题。
  1. npm run build:prod
    错误 TS2307:找不到模块“'./app/app.module.ngfactory'”。
    我认为这是由于webpack配置中的某个部分引起的,因为在Angular 9中模块导入已更改。但是,我并不完全确定。
  plugins: [
    new ngw.AngularCompilerPlugin({
      tsConfigPath: path.resolve(rootPath, 'tsconfig.aot.json'),
      entryModule: path.resolve(rootPath, 'src', 'app', 'app.module#AppModule')
    })
  ]
  1. npm run test
    找不到文件: ./node_modules/jest-preset-angular/InlineHtmlStripStylesTransformer (解析为: .../angular-webpack-build/node_modules/jest-preset-angular/InlineHtmlStripStylesTransformer)
    这个错误对我来说有些神秘。我看了一些关于类似问题的帖子,但那些建议并没有起作用。

以下是我的配置文件,整个项目都在GitHub上可用:https://github.com/aszidien/angular-webpack-build

package.json

...
  "scripts": {
    "build:dev": "cross-env NODE_ENV=development webpack --mode development",
    "build:prod": "cross-env NODE_ENV=production webpack --mode production",
    "test": "jest"
  }
...

tsconfig.aot.json

{
  "compilerOptions": {
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "skipMetadataEmit": true
  }
}

webpack.config.common.js

const webpackMerge = require('webpack-merge');

const commonConfig = require('./webpack.config.common');

module.exports = webpackMerge(commonConfig, {
  devtool: 'cheap-module-eval-source-map',
  output: {
    publicPath: '/',
    filename: 'bundle.js',
    chunkFilename: '[id].chunk.js'
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          {
            loader: 'awesome-typescript-loader', options: {
              transpileOnly: true
            }
          },
          { loader: 'angular2-template-loader' },
          { loader: 'angular-router-loader' }
        ]
      }
    ]
  },
  devServer: {
    historyApiFallback: true,
    stats: 'minimal'
  }
});

webpack.config.prod.js

const webpackMerge = require('webpack-merge');

const commonConfig = require('./webpack.config.common');

module.exports = webpackMerge(commonConfig, {
  devtool: 'cheap-module-eval-source-map',
  output: {
    publicPath: '/',
    filename: 'bundle.js',
    chunkFilename: '[id].chunk.js'
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          {
            loader: 'awesome-typescript-loader', options: {
              transpileOnly: true
            }
          },
          { loader: 'angular2-template-loader' },
          { loader: 'angular-router-loader' }
        ]
      }
    ]
  },
  devServer: {
    historyApiFallback: true,
    stats: 'minimal'
  }
});
1个回答

0
在 Angular 9 中使用 Ivy,不再需要 ngfactory 文件,您应该删除 main-aot.ts 文件。

仅仅删除 main-aot.ts 文件会产生与执行 build:dev 任务相同的结果。编译将会正常工作,但是 JS 文件不会被压缩。 :/ - Dimnox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接