@ud-angular-builders/custom-webpack
TypeScript icon, indicating that this package has built-in type declarations

8.0.18 • Public • Published

Custom webpack builders for Angular build facade

Fork from angular-builders but with enhancement.

Angular 7 or 8 does not allow you to transform the wepback configuration throw options. However, Angular has @angular-devkit allowing powerful stuff including tweaking into Angular building process.

@ud-angular-builders/custom-webpack is an Angular Builder allowing to transform Angular Webpack configuration. It also allows to transform the output index.html

The webpack configuration is being passed with angular builder context, angular build options and the angular webpack configuration. It can be useful to write customized webpack confiugration and webpack plugins.

Also, if the webpack configuration export value is a function, it is possible to return the configuration choosing to override or not the basic angular webpack configuration.

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

This documentation is for version 8 only. Find documentation for version 7 here.

Prerequisites:

Usage

  1. npm i -D @up-angular-builders/custom-webpack
  2. In your angular.json:
    "projects": {
      ...
      "[project]": {
        ...
        "architect": {
          ...
          "[architect-target]": {
            "builder": "@up-angular-builders/custom-webpack:[browser|server|karma|dev-server]"
            "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|dev-server] one of the supported builders - browser, server, karma or dev-server
  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": "@up-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) and index.html tranformations. 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. It will also run transformation on index.html if specified.

Builder options:

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

angular.json Example:

"architect": {
  ...
  "build": {
    "builder": "@up-angular-builders/custom-webpack:browser"
    "options": {
      "customWebpackConfig": {
        "path": "./extra-webpack.config.js",
        "mergeStrategies": { "externals": "replace" }
      }
      "indexTransform": "./index-html-transform.js"
      "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 while all the rest will be appended. In addition index.html will be modified by the function exported from ./index-html-transform.js.

Custom webpack dev-server

Enhanced @angular-devkit/build-angular:dev-server builder that leverages the custom webpack builder to get webpack configuration.

Unlike the default @angular-devkit/build-angular:dev-server it doesn't use @angular-devkit/build-angular:browser configuration to run the dev server. Instead it uses customWebpackConfiguration from browserTarget and runs custom webpack dev server build.

Thus, if you use @up-angular-builders/custom-webpack:dev-server along with @up-angular-builders/custom-webpack:browser, ng serve will run with custom configuration provided in the latter.

Example

angular.json:

"architect": {
  ...
  "build": {
    "builder": "@up-angular-builders/custom-webpack:browser"
    "options": {
      "customWebpackConfig": {
         path: "./extra-webpack.config.js"
      }
      ...
    } 
  },
  "serve": {
    "builder": "@up-angular-builders/custom-webpack:dev-server",
    "options": {
        "browserTarget": "my-project:build"
    }
  }

In this example dev-server will use custom-webpack:browser builder, hence modified webpack config, when invoking the serve target.

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) and index.html tranformations. 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": "@up-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 while all the rest will be appended. 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.

You can check a full example here

Now, you can write the webpack configuration in ./extra-webpack.config.js:

const path = require('path');
const { AddAssetIndexPlugin } = require('@up-angular-builders/custom-webpack');

module.exports = buildParameters => {
    // the properties of buildParameters are
    // const { builderContext, buildOptions, baseWebpackConfig } = buildParamters;

    const configuration = {
        module: {
             rules: [
                {
                    test: /\.css$/,
                    loader: 'css-loader',
                    options: {
                        name: `[name][hash].[ext]`,
                    },
                 }
            ],
        },
        plugins: [
            new webpack.DefinePlugin({
              ENVIRONMENT: JSON.stringify('browser')
            }),
            new webpack.NormalModuleReplacementPlugin(/(.*)\$environment\$(\.*)/, function (resource) {
              resource.request = resource.request.replace(/\$environment\$/, 'browser');
            }),

            new AddAssetIndexPlugin([
                {
                    filepath: 'src/font/**/*.woff2',
                    attributes: {
                        as: 'font',
                        rel: 'preload'
                    },
                    // deployUrl: 'public/deploy',
                    hash: true,
                    place: 'head',
                    sri: true,
                    outputDir: filepath => {
                        const split = filepath.split('src/font/');
                        const newpath = path.join('assets/bust-cached-font', split[1]);

                        return path.dirname(newpath);
                    }
                }
            ], builderParameters)
        ]
    };

    // ovveride allows to merge or override the Angular webpack configuration
    // return configration directly is the same as the following.
    return { configuration, ovveride: false };
};

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) and index.html tranformations. 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": "@up-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 option defines your custom webpack configuration. If not specified at all, plain Angular build will run.
The following properties are available:

  • path: path to the extra webpack configuration, defaults to webpack.config.js. The configuration file can export either an object or a function. If it is an object it 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.
    In more complicated cases you'd probably want to use a function instead of an object.
  • 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 allow cyclic dependencies that include dynamic imports you only have to specify this single entry:

new CircularDependencyPlugin({
  allowAsyncCycles: true,
})

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.

Custom Webpack config function

If customWebpackConfig.path file exports a function, the behaviour of the builder changes : no more automatic merge is applied, instead the function is called with the base Webpack configuration and must return the new configuration.

The function is called with the build context, the base config and the builder options as parameters.

If the function return a webpack confiugration object, mergeStrategies and replaceDuplicatePlugins options have no effect.

However, if the function return { configuration : webpackConfig; override: true}, then mergeStrategies and replaceDuplicatePlugins options will be in effect.

custom-webpack.config.js example :

const webpack = require('webpack');
const pkg = require('./package.json');

module.exports = (config, options) => {
  config.plugins.push(
    new webpack.DefinePlugin({
      'APP_VERSION': JSON.stringify(pkg.version),
    }),
  );

  return config;
};

Index transform

Important:

Requires @angular-devkit/build-angular@0.801 and @up-angular-builders/custom-webpack@8.1.0.

Since Angular 8 index.html is not generated as part of the Webpack build. If you want to modify your index.html you should use indexTransform option.
indexTransform is a path (relative to workspace root) to a .js file that exports transformation function for index.html.
Function signature is as following:

(options: TargetOptions, indexHtmlContent: string) => string|Promise<string>;

or, in other words, the function receives target options and original index.html content (generated by Angular CLI) and returns a new content as string or Promise.
TargetOptions follows target definition from this schema and looks like this:

export interface Target {
    configuration?: string;
    project: string;
    target: string;
}

It is useful when you want to transform your index.html according to the build options.

Example

angular.json:

"architect": {
  ...
  "build": {
    "builder": "@up-angular-builders/custom-webpack:browser"
    "options": {
      "indexTransform": "./index-html-transform.js"
      ...
    }

index-html-transform.js:

module.exports = (targetOptions, indexHtml) => {
    const i = indexHtml.indexOf('</body>');
    const config = `<p>Configuration: ${targetOptions.configuration}</p>`
    return `${indexHtml.slice(0,i)}
            ${config}
            ${indexHtml.slice(i)}`
}

In the example we add a paragraph with build configuration to your index.html. It is a very simple example without any asynchronous code but you can also return a Promise from this function.

Full example here.

Further reading

Package Sidebar

Install

npm i @ud-angular-builders/custom-webpack

Weekly Downloads

0

Version

8.0.18

License

MIT

Unpacked Size

91.6 kB

Total Files

44

Last publish

Collaborators

  • milottit