Carlos Torrecillas
Carlos Torrecillas

Reputation: 5756

Run customWebpackConfig in Angular 13

I recently updated to Angular 13 from Angular 11 with the latest gratest packages of all components I need and I'm struggling to get the custom-webpack angular builder to run when I perform ng serve.

In my scenario, I perform the CSS optimization through the custom web pack plugin CSS Purge therefore it's critical for me to have it working and I'm sure it's possible. If I ng build --configuration production however, it works like a charm and angular.json does not moan at all.

The main issue I have when I perform ng serve is that I get the following error:

Schema validation failed with the following errors:
  Data path "" must NOT have additional properties(customWebpackConfig).

The current angular.json file has the following setup:

    {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "newwebsite-app": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "outputPath": "dist/browser",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "buildOptimizer": false,
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/robots.txt",
              "src/sitemap.xml"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "customWebpackConfig": {
              "path": "./webpack.config.js"
            }
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": {
                "scripts": true,
                "styles": {
                  "minify": true,
                  "inlineCritical": false
                },
                "fonts": true
              },
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/custom-webpack:dev-server",
          "options": {
            "customWebpackConfig": {
              "path": "./webpack.config.js"
            },
            "browserTarget": "newwebsite-app:build",
            "port": 10150
          },
          "configurations": {
            "production": {
              "browserTarget": "newwebsite-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "newwebsite-app: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",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "newwebsite-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "newwebsite-app:serve:production"
            }
          }
        },
        "server": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/server",
            "main": "server.ts",
            "tsConfig": "tsconfig.server.json"
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "sourceMap": false,
              "optimization": {
                "scripts": false,
                "styles": true
              }
            }
          }
        },
        "serve-ssr": {
          "builder": "@nguniversal/builders:ssr-dev-server",
          "options": {
            "browserTarget": "newwebsite-app:build",
            "serverTarget": "newwebsite-app:server"
          },
          "configurations": {
            "production": {
              "browserTarget": "newwebsite-app:build:production",
              "serverTarget": "newwebsite-app:server:production"
            }
          }
        },
        "prerender": {
          "builder": "@nguniversal/builders:prerender",
          "options": {
            "browserTarget": "newwebsite-app:build:production",
            "serverTarget": "newwebsite-app:server:production",
            "routes": [
              "/"
            ]
          },
          "configurations": {
            "production": {}
          }
        }
      }
    }
  },
  "defaultProject": "newwebsite-app",
  "cli": {
    "analytics": false
  }
}

Now note that in order to migrate, at least in my case to Angular 13.0.2, I had to remove the "extractCss: true" bit because I got an error related to that (instead I added the inlineCriticalCss option), I did put the buildOptimizer: false in the browser part otherwise it will not work just with aot: false, fair enough, BUT I can't get the webpack running when I do ng serve.

The package.json dependencies I have:

"dependencies": {
    "@angular/animations": "13.0.2",
    "@angular/common": "13.0.2",
    "@angular/compiler": "13.0.2",
    "@angular/core": "13.0.2",
    "@angular/forms": "13.0.2",
    "@angular/platform-browser": "13.0.2",
    "@angular/platform-browser-dynamic": "13.0.2",
    "@angular/platform-server": "13.0.2",
    "@angular/router": "13.0.2",
    "@fortawesome/angular-fontawesome": "0.10.1",
    "@fortawesome/fontawesome-svg-core": "1.2.36",
    "@fortawesome/free-brands-svg-icons": "5.15.4",
    "@fortawesome/free-solid-svg-icons": "5.15.4",
    "@nguniversal/common": "13.0.1",
    "@nguniversal/express-engine": "13.0.1",
    "bootstrap": "5.1.3",
    "express": "4.17.1",
    "ng-recaptcha": "9.0.0",
    "ngx-cookie": "5.0.2",
    "rxjs": "7.4.0",
    "tslib": "2.3.0",
    "zone.js": "0.11.4"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "^13.0.0",
    "@angular-devkit/build-angular": "^13.0.3",
    "@angular-eslint/builder": "13.0.1",
    "@angular-eslint/eslint-plugin": "13.0.1",
    "@angular-eslint/eslint-plugin-template": "13.0.1",
    "@angular-eslint/schematics": "13.0.1",
    "@angular-eslint/template-parser": "13.0.1",
    "@angular/cli": "13.0.3",
    "@angular/compiler-cli": "13.0.2",
    "@angular/language-service": "13.0.2",
    "@nguniversal/builders": "13.0.1",
    "@types/express": "4.17.3",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "2.0.6",
    "@types/node": "13.9.1",
    "@typescript-eslint/eslint-plugin": "5.3.0",
    "@typescript-eslint/parser": "5.3.0",
    "codelyzer": "6.0.0",
    "eslint": "^8.2.0",
    "jasmine-core": "3.8.0",
    "jasmine-spec-reporter": "5.0.0",
    "karma": "6.3.0",
    "karma-chrome-launcher": "3.1.0",
    "karma-coverage-istanbul-reporter": "3.0.2",
    "karma-jasmine": "4.0.0",
    "karma-jasmine-html-reporter": "1.5.0",
    "postcss-scss": "4.0.2",
    "protractor": "~7.0.0",
    "purgecss-webpack-plugin": "4.1.3",
    "ts-loader": "9.2.6",
    "ts-node": "10.1.0",
    "tslint": "6.1.3",
    "typescript": "4.4.3"
  }

Note that prior the migration I have had everything working fine. This just seems to be happening when I update the custom-webpack library which it seems I have to I want to run the latest packages. If I take a look at its documentation here: https://www.npmjs.com/package/@angular-builders/custom-webpack#Custom-webpack-browser To me the syntax looks fine therefore I don't know how to solve this issue - apart from raising it on the project webpage.

Any help is much appreciated, thanks!

UPDATE!

Actually found out what was happening by comparing my angular.json against the thread here: https://stackoverflow.com/a/61768757/5293466 and in the serve section I had to remove the customWebpackConfig stuff. Then everything started working!!!

Upvotes: 7

Views: 14961

Answers (1)

Salman Kazemi
Salman Kazemi

Reputation: 64

step 1: npm install --save-dev @angular-builders/[email protected] step 2: Open angular.json in your code editor. Inside of the architect/build object, update the builder from @angular-devkit/build-angular:browser to @angular-builders/custom-webpack:browser and add the customWebpackConfig key:

Upvotes: 2

Related Questions