node.js - Angular 7 自定义 webpack.config.js

标签 node.js angular typescript webpack

由于最新的 Angular 已经弃用了“ng eject”命令,我已经开始我的项目,添加名为 webpack.config 的自定义文件:

extra-webpack.config.js

我已遵循本教程: https://codeburst.io/customizing-angular-cli-6-build-an-alternative-to-ng-eject-a48304cd3b21

我已经配置了一切,它似乎工作正常。我的问题是,我想设置我的新文件

extra-webpack.config.js

只有在我启动命令时才工作

"npm run start:dev"

,启动的时候不想用

"npm run start:prod"

我尝试制作一个 if 语句,以检查 module.exports 上的环境是否未设置为生产环境,但我无法从 environment.ts 文件中收集环境。我试过: 从 './src/environments/environment' 导入 {environment};

谁能帮我正确配置?非常感谢

这是我的 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
        })
    ]
};

这是我的 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" }

我还注意到,如果我删除:

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

从 webpack 配置,我可以用

运行它

npm run start:prod

最佳答案

你只需要移动

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

configurations:production 下,使其针对该 production 配置进行定制

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

更多详情:https://github.com/meltedspark/angular-builders/issues/248#issuecomment-466650709

关于node.js - Angular 7 自定义 webpack.config.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53768121/

相关文章:

javascript - 无法使用 ImageMagick Node.js 调整图像大小

javascript - 将 MongoDB/ExpressJS 查询存储到数组中并显示到 HTML 表中

node.js - NodeJS Docker 当前与最新

angular - 在 Angular 7 中,如何从 Observable 中提取结果?

reactjs - React typescript - 类型必须有一个返回迭代器的 '[Symbol.iterator]()' 方法

javascript - 如何从json数据中过滤子菜单并绑定(bind)到angular2中的html

angular - 如何在 Angular 应用程序中为 PDFMake 导入自定义字体?

python - 从 Linux Azure VM 运行 Node 或 Python 应用程序

json - 单元测试时出错 : "Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1"

angular - 在 Asp.NET Core 之上构建 Angular 4 应用程序的最佳方式是什么?