Merging unique CopyWebpackPlugin
See original GitHub issueHello,
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:
- Created 5 years ago
- Comments:9 (3 by maintainers)

Top Related StackOverflow Question
To keep things simple (and less complicated), I would consider doing a
copyhelper 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.
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
CopyWebpackPluginand 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.