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.

Merging unique CopyWebpackPlugin

See original GitHub issue

Hello, I started with this example and I’ve been struggling trying to figure out how to merge a unique version of CopyWebpackPlugin.

Another issue brought this up, but calling new CopyWebpackPlugin() returns { apply: [Function: apply] } and I can’t get merge.unique to properly set the unique value from the 2nd config. I’m not sure if this is user error or if it’s not feasible. With some tweaking of the “getter” function, it appears I can get webpack-merge to set a single unique value, but it always uses seems to use first instance of CopyWebpackPlugin.

Example code:

const merge = require('webpack-merge');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
const prod = require('./prod.js');
const dev = require('./dev.js');

module.exports = merge({
  customizeArray: merge.unique(
    'plugins',
    ['apply'],
    plugin => plugin.hasOwnProperty('apply') && 'apply'
  ),
})(prod, {
  plugins: [
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static/html/*.html'),
        to: path.resolve(__dirname, '../build'),
        flatten: true,
      },
      {
        from: path.resolve(__dirname, '../static/html/local/*.html'),
        to: path.resolve(__dirname, '../build'),
        flatten: true,
      },
    ]),
  ],
});

As for the why the CopyWebpackPlugin is unique, It seems like it’s possibly related to https://github.com/webpack-contrib/copy-webpack-plugin/issues/268 since the plugins array looks something like this for my webpack config:

[ CleanWebpackPlugin {... },
  LimitChunkCountPlugin {...},
  { apply: [Function: apply] }, // CopyWebpackPlugin is different here
  MiniCssExtractPluginCleanup {},
  MiniCssExtractPlugin {...},
  LodashModuleReplacementPlugin {...}
]
....

I think I am just trying to use merge.unique incorrectly and my example above will just remove the 2nd config’s CopyWebpackPlugin entry. Any recommended approach for doing this given that CopyWebpackPlugin seems to be a special case?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bebrawcommented, Sep 17, 2018

To keep things simple (and less complicated), I would consider doing a copy helper and then applying that per target.

Can you set up a small repo? I can do a basic setup there based on what you have so far.

Sorry for the late reply. Super busy lately.

1reaction
ctaylo21commented, Sep 17, 2018

No worries about the delay, I majorly appreciate the project and all the work you put in. I will try to find some time today to set up a repo to recreate the issue.

We ended up finding better ways to combine/merge our webpack configs so we now just have a single place that uses the CopyWebpackPlugin and relies on environment variables to determine which path to load.

That being said, someone else may run into the issue so having a solution would be nice for the future. I’ll let you know when I can get a small repo set up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the webpack-merge function in webpack-merge
To help you get started, we've selected a few webpack-merge examples, ... import merge from 'webpack-merge' import CopyPlugin from 'copy-webpack-plugin' ...
Read more >
CopyWebpackPlugin | webpack
Copies individual files or entire directories, which already exist, to the build directory. Getting Started. To begin, you'll need to install copy-webpack- ......
Read more >
Upgrading webpack4 webpack-merge react issue
You are specifying babel options in two places, first in .babelrc, then in webpack.commonn.js. The options in webpack.commonn.js takes ...
Read more >
Fancy-webpack-merge NPM
webpack-merge provides a merge function that concatenates arrays and merges objects creating a new object. If functions are encountered, it will execute them, ......
Read more >
webpack-merge | Yarn - Package Manager
webpack-merge provides a merge function that concatenates arrays and merges objects creating a new object. If functions are encountered, it will execute them, ......
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