angular14 webpack < 5 used to include polyfills for node.js core modules by defa

410 阅读1分钟

本文是加mars3d最新版本3.3.14运行报错

解决方法:

1. 下载对应版本的,自定义webpack配置

npm i @angular-builders/custom-webpack

  1. angular.json修改:

两个位置:

"builder": "@angular-builders/custom-webpack:browser",

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

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "globe": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "less"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "outputPath": "dist/globe",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "less",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              {
                "glob": "**/*",
                "input": "node_modules/mars3d-cesium/Build/Cesium",
                "output": "assets/mars3d-cesium"
              },
              {
                "glob": "**/*",
                "input": "public",
                "output": ""
              }
            ],
            "styles": [
              "node_modules/mars3d-cesium/Build/Cesium/Widgets/widgets.css",
              "node_modules/mars3d/dist/mars3d.css",
              "src/styles.less"
            ],
            "scripts": [],
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js"
            }
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-builders/custom-webpack:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "globe:build:production"
            },
            "development": {
              "browserTarget": "globe:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "globe:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "less",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.less"
            ],
            "scripts": []
          }
        }
      }
    }
  }
}

  1. 添加文件extra-webpack.config.js,跟angular.json同级
module.exports = {
  resolve: {
    fallback: {
      fs: false,
      Buffer: false,
      http: false,
      https: false,
      zlib: false,
      url: false
    },
  },
  module: {
    unknownContextCritical: false,
  }
};