简体   繁体   English

Angular 7自定义webpack.config.js

[英]Angular 7 custom webpack.config.js

since newest Angular has deprecated 'ng eject' command, I've started my project with adding custom file with webpack.config named: 由于最新的Angular已弃用'ng弹出'命令,因此我开始使用webpack.config添加自定义文件来启动项目,该文件名为:

extra-webpack.config.js 超webpack.config.js

I've followed this tutorial: https://codeburst.io/customizing-angular-cli-6-build-an-alternative-to-ng-eject-a48304cd3b21 我遵循了本教程: https : //codeburst.io/customizing-angular-cli-6-build-an-alternative-to-ng-eject-a48304cd3b21

I have configured everything and it seems to work fine. 我已经配置好了一切,而且看起来工作正常。 My problem is, that I would like to set my new file 我的问题是,我想设置新文件

extra-webpack.config.js 超webpack.config.js

to work only when I start command 仅在我启动命令时才能工作

"npm run start:dev" “ npm运行开始:dev”

, I don't want to use it when I start ,开始时我不想使用它

"npm run start:prod" “ npm运行开始:产品”

I've tried to make an if statement, to check if environment is not set to production on module.exports, but I can't gather environment from environment.ts file. 我试图做出一个if语句,以检查是否在module.exports上未将环境设置为生产,但是我无法从environment.ts文件中收集环境。 I've tried with: import {environment} from './src/environments/environment'; 我尝试过:从'./src/environments/environment'导入{environment};

Can anyone help me with proper configuration? 有人可以帮助我进行正确的配置吗? Thanks a lot 非常感谢

Here is my extra-webpack.config.js file: 这是我的extra-webpack.config.js文件:

'use strict'; 
const path = require('path'); const ForkTsCheckerWebpackPlugin = 
require('fork-ts-checker-webpack-plugin');

module.exports = {
    optimization: {
        splitChunks: {
          chunks: 'async',
          minSize: 30000,
          maxSize: 0,
          minChunks: 1,
          maxAsyncRequests: 5,
          maxInitialRequests: 3,
          automaticNameDelimiter: '~',
          name: true,
          cacheGroups: {
            vendors: {
              test: /[\\/]node_modules[\\/]/,
              priority: -10
            },
            default: {
              minChunks: 2,
              priority: -20,
              reuseExistingChunk: true
            }
          }
        }
      },
    context: __dirname,
    output: {
        pathinfo: false
    },
    mode: 'development',
    optimization: {
        removeAvailableModules: false,
        removeEmptyChunks: false,
        splitChunks: false
    },
    module: {
        rules: [
            {
                test: /\.ts?$/,
                include: path.resolve(__dirname, 'src'),
                use: [{
                    loader: 'ts-loader',
                    options: {
                        transpileOnly: true,
                        experimentalWatchApi: true,
                    },
                }]
              }
        ]
    },
    resolve: {
        extensions: [ '.ts', '.js' ]
    },
    plugins: [
        new ForkTsCheckerWebpackPlugin({
            tslint: true
        })
    ]
};

this is my angular.json file: 这是我的angular.json文件:

{   "$schema": "./node_modules/@angular/cli/lib/config/schema.json",   "version": 1,   "newProjectRoot": "projects",   "projects": {
    "my-portal": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js"
            },
            "outputPath": "dist/my-portal",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/app/styles/style.scss"
            ],
            "scripts": []
          },
          "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"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/dev-server:generic",
          "options": {
            "browserTarget": "my-portal:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "my-portal:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "my-portal:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "my-portal-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "my-portal:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "my-portal:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }   },   "defaultProject": "my-portal" }

I have also noticed that, if I removed: 我还注意到,如果删除了:

plugins: [
        new ForkTsCheckerWebpackPlugin({
            tslint: true
        })
    ]

from webpack configuration, I'm able to run it with 从webpack配置中,我可以使用

npm run start:prod npm运行开始:产品

You just need to MOVE the config of 您只需要移动以下配置

"customWebpackConfig": {
  "path": "./extra-webpack.config.js"
},

to under configurations:production to make it customized for that production configuration 到以下configurations:production以使其针对该production配置进行定制

"configurations": {
  "production": {
    "customWebpackConfig": {
      "path": "./extra-webpack.config.js"
    },
    "fileReplacements": [],
...

More details: https://github.com/meltedspark/angular-builders/issues/248#issuecomment-466650709 更多详细信息: https : //github.com/meltedspark/angular-builders/issues/248#issuecomment-466650709

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM