npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ajcrites-custom-webpack-test

v7.2.0

Published

Custom webpack builders for Angular build facade. Allow to modify Angular build configuration without ejecting it

Downloads

5

Readme

Custom webpack builders for Angular build facade

npm version npm

Allow customizing build configuration without ejecting webpack configuration (ng eject)

Prerequisites:

Usage

  1. npm i -D @angular-builders/custom-webpack
  2. In your angular.json:
    "projects": {
        ...
        "[project]": {
             ...
             "architect": {
                    ...
                    "[architect-target]": {
                              "builder": "@angular-builders/custom-webpack:[browser|server|karma]"
                              "options": {
                                    ...
                              }
    Where:
    • [project] is the name of the project to which you want to add the builder
    • [architect-target] is the name of build target you want to run (build, serve, test etc. or any custom target)
    • [browser|server|karma] one of the supported builders - browser, server or karma
  3. If [architect-target] is not one of the predefined targets (like build, serve, test etc.) then run it like this:
    ng run [project]:[architect-target]
    If it is one of the predefined targets, you can run it with ng [architect-target]

For example

  • angular.json:
    "projects": {
        ...
        "example-app": {
             ...
             "architect": {
                    ...
                    "build": {
                              "builder": "@angular-builders/custom-webpack:browser"
                              "options": {
                                    ...
                              }
  • Run the build: ng build

Builders

Custom webpack browser

Extended @angular-devkit/build-angular:browser builder that allows to specify additional webpack configuration (on top of the existing under the hood). The builder will run the same build as @angular-devkit/build-angular:browser does with extra parameters that are specified in the provided webpack configuration.

Builder options:

  • All the @angular-devkit/build-angular:browser options
  • customWebpackConfig: see below

angular.json Example:

"architect": {
    ...
    "build": {
              "builder": "@angular-builders/custom-webpack:browser"
              "options": {
                     "customWebpackConfig": {
                        "path": "./extra-webpack.config.js",
                        "mergeStrategies": { "externals": "replace" }
                     }
                     "outputPath": "dist/my-cool-client",
                     "index": "src/index.html",
                     "main": "src/main.ts",
                     "polyfills": "src/polyfills.ts",
                     "tsConfig": "src/tsconfig.app.json"
              }

In this example externals entry from extra-webpack.config.js will replace externals entry from Angular CLI underlying webpack config.

Custom webpack server

Extended @angular-devkit/build-angular:server builder that allows to specify additional webpack configuration (on top of the existing under the hood). The builder will run the same build as @angular-devkit/build-angular:server does with extra parameters that are specified in the provided webpack configuration.

Builder options:

  • All the @angular-devkit/build-angular:server options
  • customWebpackConfig: see below

angular.json Example:

"architect": {
    ...
    "build": {
              "builder": "@angular-builders/custom-webpack:server"
              "options": {
                     "customWebpackConfig": {
                        "path": "./extra-webpack.config.js",
                        "mergeStrategies": { "module.rules": "prepend" },
                        "replaceDuplicatePlugins": true
                     }
                    "outputPath": "dist/my-cool-server",
                    "main": "src/main.server.ts",
                    "tsConfig": "src/tsconfig.server.json"
              }

In this example module.rules entry from extra-webpack.config.js will be prepended to module.rules entry from Angular CLI underlying webpack config.
Since loaders are evaluated from right to left this will effectively mean that the loaders you define in your custom configuration will be applied after the loaders defined by Angular CLI.

Custom webpack Karma

Extended @angular-devkit/build-angular:karma builder that allows to specify additional webpack configuration (on top of the existing under the hood). The builder will run the same build as @angular-devkit/build-angular:karma does with extra parameters that are specified in the provided webpack configuration.

Builder options:

  • All the @angular-devkit/build-angular:karma options
  • customWebpackConfig: see below

angular.json Example:

"architect": {
    ...
    "test": {
              "builder": "@angular-builders/custom-webpack:karma"
              "options": {
                     "customWebpackConfig": {
                        "path": "./extra-webpack.config.js"
                     }
                    "main": "src/test.ts",
                    "polyfills": "src/polyfills.ts",
                    "tsConfig": "src/tsconfig.spec.json",
                    "karmaConfig": "src/karma.conf.js",
                    ...
              }

Custom webpack config object

This object defines your custom webpack configuration. It is defined by the following properties:

  • path: path to the extra webpack configuration, defaults to webpack.config.js. Notice that this configuration shall contain only modifications and additions, you don't have to specify the whole webpack configuration.
    Thus, if you'd like to add some options to style-loader (which already there because of default Angular configuration), you only have to specify this part of the loader:

     {
       test: /\.css$/,
       use: [
         {loader: 'style-loader', options: {...}}
       ]
     }

    The builder will take care of merging the delta with the existing configuration provided by Angular.

  • mergeStrategies: webpack config merge strategies, can be append | prepend | replace per webpack config entry. Defaults to append.

    • append: appends the given entry configuration (in custom webpack config) to the existing Angular CLI webpack configuration.
    • prepend: prepends the given entry configuration (in custom webpack config) to the existing field configuration (in Angular CLI webpack config). The custom loaders config will be added to the beginning of the existing loaders array.
    • replace: replaces the given entry configuration entirely. The custom webpack config will replace the Angular CLI webpack config (for this particular entry). See webpack-merge for more info.
  • replaceDuplicatePlugins: Defaults to false. If true, the plugins in custom webpack config will replace the corresponding plugins in default Angular CLI webpack configuration. If false, the default behavior will be applied. Note that if true, this option will override mergeStrategies for plugins field.

Merging plugins configuration:

If in your custom configuration you specify a plugin that is already added by Angular CLI then by default the two instances will be merged.
In case of the conflicts your configuration will override the existing one.
Thus, if you'd like to modify an existing plugin configuration, all you have to do is specify the delta you want to change.
For example, if you'd like to add an additional entry in excludeChunks list of HtmlWebpackPlugin you only have to specify this single entry:

 new HtmlWebpackPlugin({
    "excludeChunks": [
      "webworker"
    ]
 })

Keep in mind though that if there are default values in the plugin's constructor, they would override the corresponding values in the existing instance. So these you have to set explicitly to the same values Angular sets.
You can check out an example for plugins merge in the unit tests and in this issue.

Further reading