copy-webpack-plugin: Build enters an infinite loop
- Operating System: Windows 10
- Node Version: v15.12.0
- NPM Version: 7.6.3
- webpack Version: 5.31.2
- copy-webpack-plugin Version: 8.1.1
Expected Behavior
npm run build -- --watch
should build the project only once.- When a file is changed, the project should be rebuilt only once.
Actual Behavior
Multiple odd things happen.
-
npm run build -- --watch
builds the project twice.
This does not happen with
npm run build
. This does not happen if I remove copy-webpack-plugin from webpack’splugins
. -
The build enters an infinite loop if you do either one of these two things:
-
npm run build -- --watch
- Modify any
./some-file*.txt
or./index.js
-
- Uncomment the first and only line of code inside
./index.js
npm run build -- --watch
- Uncomment the first and only line of code inside
Everything works as expected if I remove copy-webpack-plugin from webpack’s
plugins
. -
Code
webpack.config.js
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = function getWebpackConfig(env, argv) {
const mode = argv.nodeEnv || argv.mode || 'production';
const copyPlugin = new CopyWebpackPlugin({
patterns: [
{ from: './some-file*.txt', },
]
});
return {
mode,
entry: './index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [ copyPlugin ],
};
}
How Do We Reproduce?
Test repo: https://github.com/aleab/copy-webpack-plugin_build-loop
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 16 (7 by maintainers)
@alexander-akait Finally, I managed to fix it by adding the
assets
in the ignored path. At first I was trying to add thebuild
directory to the ignored and it wasn’t working.so at the end:
webpack.config:
We have the same issue with
and
When adding
static
to ignored of watchOptions like thisit doesnt rebuild infinitely anymore but the static folder isnt watched anymore. So whenever we change something within the static folder (e.g. we have css files inside the static folder) we would have to restart the whole watch process for it to be copied over into the build directory again (which in our case is really annoying given the fact we have css files in that directory). So I dont think this is “Nice to have” but a bug that should be resolved asap.
I faced the same problem.
Helps solve the problem, but then the copy only happens on the first build
I mean, it is an acceptable working solution, but I have many files in the root directory that I would have to manually add to the ignored list and I don’t really like that, it just seems unclean. The best solution for my use case is to either have a way to tell this plugin what to add to the watch list (i.e. to not add the context directory), or to manually resolve the glob pattern beforehand inside webpack.config.js (which is what I’ll be doing).
This is definitely outside the scope of this plugin, but the cleanest solution would be a way of telling the watcher to only report changes to files that match the original glob pattern within the context directory.
That is also, in my opinion, what the user would expect. If I tell copy-wepback-plugin to copy
./some-file*.txt
I would expect the plugin to tell the compiler that those files are meaningful and should be watched for changes. What I wouldn’t expect is the side effect of the compiler thinking that everything else inside./
is also meaningful.I understand that watching the directory is necessary, otherwise you’d miss potential new files/directories matching the glob pattern, but I think that there needs to be a way of telling the watcher/compiler that “Yes, you do need to watch the directory, but don’t forget why you are watching it: within that directory the only meaningful files that should trigger a rebuild are those that match the pattern(s)” without having to explicitly add the files that do not match the pattern(s) to the ignore list.