question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Different webpack.config.js files for different configurations not working with v9.2

See original GitHub issue

Hello, attempting to implement your recommended angular.json for different webpack configurations per https://github.com/just-jeb/angular-builders/issues/248#issuecomment-466650709

   "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "outputPath": "dist/electron-angular-native",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.png",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
                "customWebpackConfig": {
                    "path": "./prod-webpack.config.js"
                },
            },
            "development": {
                "customWebpackConfig": {
                    "path": "./dev-webpack.config.js"
                },
            }
          }
        },

Unfortunately, with v9.20 I can’t get the webpack.config.js files to be picked up unless they’re specified within the top-level options.

I can get it running with dev defaults only:

   "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
                "path": "./dev-webpack.config.js"
            },
            "outputPath": "dist/electron-angular-native",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.png",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
                "customWebpackConfig": {
                    "path": "./prod-webpack.config.js"
                },
            },
            "development": {
            }
          }
        },

… but then prod-webpack.config.js is ignored when building with --configuration=production

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
kmarazcommented, Jan 21, 2021

Hi, just an update for those who found this after Angular 11.1.0 release: You will probably need to use AngularWebpackPlugin instead of AngularCompilerPlugin.

For example in my case it looks like this:

import { AngularWebpackPlugin } from '@ngtools/webpack/src/ivy/plugin';

...
  const index = config.plugins?.findIndex((p) => {
    return p instanceof AngularWebpackPlugin;
  });
  if (index) {
    // @ts-expect-error pluginOptions is a private property
    const oldOptions = config.plugins?.[index].pluginOptions;
    oldOptions.directTemplateLoading = false;
    config.plugins?.splice(index);
    config.plugins?.push(new AngularWebpackPlugin(oldOptions));
  }
1reaction
DaveA-Wcommented, Oct 13, 2020

My problem was that my custom prod-webpack.config.js had module rules to try and transform *.html templates as described in this Medium article.

var path =  require('path');

module.exports = {
  module: {
    rules: [{
      test: /\.html$/,
      use: ['data-cy-loader']
    }]
  },
  resolveLoader: {
    alias: {
      'data-cy-loader': path.join(__dirname, 'data-cy-loader.js')
    }
  }
}

This config file was being loaded correctly.

However, if your production config options specify ahead-of-time aot: true compilation (which most prod configs for Angular 8+ do) then .html templates don’t get processed the same way they do for just-in-time (JIT) builds which is what you may be using for ng serve.

If your issue looks to be related - i.e., your customWebpackConfig is trying to transform .html templates - then this discussion gives some leads that may be worth following up: https://github.com/IAMtheIAM/angular-webpack-nodejs-starter/issues/4

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use multiple configuration files in webpack?
There are some ways to accomplish that. Perhaps the simplest one is specifying the config file to use. Read more about webpack usage...
Read more >
How to set up different Webpack configurations for ...
Here I'll show you the most straight forward method I've found. First, create 3 files: webpack.config.dev.js : This will be your Webpack config...
Read more >
How to Use Multiple Webpack Configurations - YouTube
If you are new to Webpack, you quickly noticed how many configurations it has available. Not only are there too many, but it...
Read more >
Configuration - webpack
dev is an online tool for creating custom webpack configurations. It allows you to select various features that will be combined and added...
Read more >
All You Need to Know about Webpack in Examples - Medium
You will know how to configure webpack, add assets, add css files, bundle your js file, starting a server, prod and dev build...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found