【问题标题】:Angular 7 - change detection doesn't work with ngx-build-plus custom webpack configurationAngular 7 - 更改检测不适用于 ngx-build-plus 自定义 webpack 配置
【发布时间】:2019-09-25 08:36:56
【问题描述】:

我正在使用 ngx-build-plus 为我的 Angular 7 应用程序自定义构建过程。
我添加了带有以下代码的 webpack.partial.js 文件:

const webpack = require('webpack');

module.exports = {
  output: {
    jsonpFunction: 'header'
  },
  plugins: [
    new webpack.ProvidePlugin({
      Promise: 'es6-promise'
    })
  ]
}

要运行应用程序,我使用以下命令:

ng serve --extra-webpack-config webpack.extra.js -o

这是我的 angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "header": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "ngx-build-plus:build",
          "options": {
            "outputPath": "dist/header",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/scss/bootstrap.scss",
              "src/styles.scss"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",  
              "node_modules/popper.js/dist/umd/popper.min.js",  
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],
            "es5BrowserSupport": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            },
            "en": {
              "aot": true,
              "i18nFile": "src/translate/messages.en.xlf",
              "i18nFormat": "xlf",
              "i18nLocale": "en",
              "i18nMissingTranslation": "error"
            },
            "fr": {
              "aot": true,
              "i18nFile": "src/translate/messages.fr.xlf",
              "i18nFormat": "xlf",
              "i18nLocale": "fr",
              "i18nMissingTranslation": "error"
            }
          }
        },
        "serve": {
          "builder": "ngx-build-plus:dev-server",
          "options": {
            "browserTarget": "header:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "header:build:production"
            },
            "en": {
              "browserTarget": "header:build:en"
            },
            "fr": {
              "browserTarget": "header:build:fr"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "header:build"
          }
        },

      }
    },

  },
  "defaultProject": "header",
  "schematics": {
    "@schematics/angular:component": {
      "styleext": "scss"
    }
  }
}

如果我在没有自定义配置的情况下运行应用程序,应用程序绝对可以正常工作,如下所示:

ng serve

但是,当我使用自定义 webpack 配置运行应用程序时,更改检测 无法正常工作。在组件类中更新的变量不会在模板/视图中更新。

使用 webpack.partial.js 有什么问题

【问题讨论】:

    标签: angular webpack


    【解决方案1】:

    为了使用自定义 webpack 配置,我们需要 @angular-builders/custom-webpack 依赖项。将其作为 devDependency 添加到您的项目中,如下所示:

    $ npm i @angular-builders/custom-webpack -D
    

    然后我们可以将 moment 安装到我们的项目中并将其导入到我们的项目中:

    $ npm i moment --save
    

    然后我们可以创建一个自定义的 webpack 配置,删除我们没有特别选择保留的任何语言环境。

    在项目的根目录中,使用以下内容创建一个名为 custom-webpack.config.js 的新文件:

    const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
    
    module.exports = {
     plugins: [
      new MomentLocalesPlugin({
       localesToKeep: ['fr']
      })
     ]
    };
    

    这需要我们安装moment-locales-webpack-plugin:

    $ npm i moment-locales-webpack-plugin -D
    

    之后,我们需要配置 angular.json 来使用这个新配置。在架构师/构建对象内部,将构建器从 @angular-devkit/build-angular:browser 更新为 @angular-builders/custom-webpack:browser 并添加 customWebpackConfig 键:

    "architect": {
     "build": {
      "builder": "@angular-builders/custom-webpack:browser",
      "options": {
       "customWebpackConfig": {
        "path": "./custom-webpack.config.js",
        "replaceDuplicatePlugins": true
       }
      }
     }
    }
    

    如果我们只希望最终构建的应用程序使用 webpack 配置,这就是我们需要做的。但是,如果我们想在使用 ng serve 的同时测试这个配置,我们还有一件事要做。

    从 npm 中安装 @angular-builders/dev-server 自定义 Web 服务器:

    $ npm i @angular-builders/dev-server -D
    

    然后我们可以在 angular.json 文件中的 serve/builder 中更新它:

    "serve": {
     "builder": "@angular-builders/custom-webpack:dev-server"
    }
    

    【讨论】:

    • ngx-build-plus@angular-builders/custom-webpack 实现自定义 webpack 配置的替代方案。我当前的设置在处理 i18n 时效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多