62

In previous versions of Angular there was an option for eject so that you could modify your webpack configuration as you please.
One of the most common use cases for this feature was adding custom webpack loaders.

In Angular 6 this option has been removed, so currently there is literally no way to get the webpack config (beside looking it up in angular source code).

Is there any way to add a custom webpack config to Angular application that uses @angular/cli 6+? Or alternatively, is there any way to "eject" the webpack config the new Angular CLI uses under the hood?

2 Answers 2

81

You can use angular-builders library that allows you extending the existing browser and server targets (and more than that) with a custom webpack config.

The usage is pretty simple:

  1. Install the library: npm i -D @angular-builders/custom-webpack

  2. Modify your angular.json:

    "architect": {
       ...
       "build": {
           "builder": "@angular-builders/custom-webpack:browser",
           "options": {
                  "customWebpackConfig": {
                     "path": "./extra-webpack.config.js",
                     "replaceDuplicatePlugins": true
                  },
                  "outputPath": "dist/my-cool-library",
                  "index": "src/index.html",
                  "main": "src/main.ts",
                  "polyfills": "src/polyfills.ts",
                  "tsConfig": "src/tsconfig.app.json"
                  ...
           }
    
  3. Add extra-webpack.config.js to the root of your application

  4. Put the extra configuration inside extra-webpack.config.js (just a plain webpack configuration)

Sign up to request clarification or add additional context in comments.

13 Comments

I can't get this working in my project. It doesn't load the extra-webpack.config.js at all. I installed all dependencies following the instructions. Any tips how to debug?
Please open an issue on github: github.com/meltedspark/angular-builders/issues. Specify your angular.json, your package.json and your extra-webpack.config.js.
The latter. build-webpack just runs a webpack build (or webpack-dev-server) with a configuration you provide. No other functionality involved. Think of it as a way to run webpack build with Angular CLI. @angular-builders/custom-webpack on the other hand runs default Angular build chain while allowing to customize this build by providing a delta Webpack config that complements the default build configuration.
|
9

For Angular 8 @angular-builders/dev-server:generic has been deprecated and @angular-builders/custom-webpack:dev-server is being used instead, source: https://github.com/just-jeb/angular-builders/blob/master/MIGRATION.MD.

On top of this you might need to run npm i @angular-devkit/architect@latest @angular-devkit/build-angular@latest @angular-devkit/core@latest @angular-devkit/schematics@latest if after migrating you would have seen the following error architect_1.createBuilder is not a function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.