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.

Extending webpack configuration with plugin zaps assets directory from dist

See original GitHub issue

Describe the bug When extending the Angular webpack configuration with another plugin, assets are no longer generated under the dist directory.

I have some files I want included in the build. They are in a newly created directory under src. I use a simple one line plugin spec for CopyWebpackPlugin to perform the copy. Upon running the build, I see my copy as expected in the dist directory, but the assets directory (in which I placed an image file in an images sub-directory) is no longer present under the dist directory.

To Reproduce Steps to reproduce the behavior:

  1. Create a new Angular application using ng new.
  2. Install @angular-builders/custom-webpack, and copy-webpack-plugin.
  3. Add files for the plugin to work on; I created a directory called i18n under src with a single file in it, en.json.
  4. Add an images directory under src/assets; populate images directory with a single image file.
  5. Add a custom webpack file (I added a file named webpack.extension.js) in the project root directory that includes a plugins section; my content below:
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    plugins: [
        new CopyWebpackPlugin([{ from: './src/i18n/*.json', to: './i18n', flatten: true }])
    ]
};
  1. Modify angular.json to specify customWebpackConfig:
      ...
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./webpack.extension.js"
            },
            "outputPath": "dist/test-app",
            ...
  1. Run a build: npm run build.

Expected behavior The expected result is that the dist directory includes my new directory, i18n, as well as the assets directory.

Actual behavior The dist directory includes my new directory, i18n, but NOT the assets directory.

Builder:

Libraries

  • angular-devkit/build-angular: 0.13.1

Additional context

  • If the CopyWebpackPlugin plugin is commented out in angular.json, the assets directory will be included under the dist directory.
  • Zip of my test application reproducing the issue is attached: test-app.zip

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
just-jebcommented, Mar 16, 2019

The problem is here.
If the plugin is not an instance of a class (but a closure with apply, like it was in copy-webpack-plugin) the merger will merge the two instances while overriding the first apply.

Regarding this specific problem (with copy-webpack-plugin), it should work with the newer versions of webpack (since this commit), so probably in Angular 8 the problem won’t exist.

This issue still has to be tackled though, because no one can promise that all the plugins will be instances of classes.
One possible solution is to not merge instances of Object, thus having multiple instances of a plugin that is implemented as a closure.

1reaction
wballonicommented, Mar 10, 2019

Good news, using the exported function form worked. See snippet below:

const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = (config, options) => {
    config.plugins.push(
        new CopyWebpackPlugin([{ from: './src/i18n/*', to: './i18n' flatten: true }])
    );
    return config;
};

With this form in place, and using @angular-builders/dev-server:generic, both build and serve work as expected. Not sure why the static webpack config is not working (I’ve confirmed it again), but at least I have a working solution. Thanks for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

using custom webpack with angular-builders? - Stack Overflow
What do I add to custom-webpack.config.js to get it to copy over those asset folders into dist/ in the way I described above?...
Read more >
Output Management - webpack
Now run an npm run build and inspect the /dist folder. If everything went well you should now only see the files generated...
Read more >
How do I configure bud properly? - Roots Discourse
I know a webpack.config.js is being generated, but where can I configure the aliases in the bud config? For now I set the...
Read more >
Extending Webpack in the SharePoint Framework toolchain
Extend the Webpack configuration with custom loaders and plug-ins. Webpack is a JavaScript module bundler.
Read more >
Managing Images with webpack - LearnHowToProgram.com
Now that we are using webpack as a module bundler, all of our source code is being combined in a single dist folder....
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