copy-webpack-plugin: New files are not being copied in watch-mode

I skimmed over the related bugs (#252 ) and they seem to be fixed, so I thought maybe it’s a new bug in the newest 5-th version as minor version is low.

  • Operating System: windows 10
  • Node Version: 10.15.3
  • NPM Version: 6.4.1
  • webpack Version: 4.31.0
  • copy-webpack-plugin Version: 5.0.3

Expected Behavior

On watch start everything works fine http://prntscr.com/nt54s1

then I copy new image file into /assets/sub/3.jpg

I expect the new file to be copied to /dist/assets/sub/3.jpg cause watcher is running.

Actual Behavior

New file doesn’t get copied into /dist/assets/sub/3.jpg

http://prntscr.com/nt560z

until I rename some existing file (1.jpg or 2.jpg) then 3.jpg gets properly copied suddenly. Or I should re-run watcher command or build again

Code

I have a simplified config like this:

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


module.exports = {
    mode: 'development',

    devtool: 'inline-source-map',

    entry: {
        'main': './assets/fake.js',
    },
    output: {
        filename: '[name].bundle.js',
    },

    plugins: [
        new CleanWebpackPlugin({verbose: true}),
        new CopyWebpackPlugin([
                {
                    from: './assets/**',
                    ignore: ['*/_src/**/*'],
                    to: '[path][name].[ext]',
                }
            ],
            {copyUnmodified: true}
            //{logLevel: 'debug', copyUnmodified: true}
        )
    ],
};

How Do We Reproduce?

I’ve created a repo where you can reproduce this behavior https://github.com/ekonoval/webpack-copy

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 11
  • Comments: 37 (12 by maintainers)

Most upvoted comments

I found the root cause after reading thru change log and code of clean-webpack-plugin

Since clean-webpack-plugin@2.0.0, you need to add an option to prevent unchanged files from removal, while copy-webpack-plugin only emit files when they are changed.

new CleanWebpackPlugin({
    cleanStaleWebpackAssets: false,
});

https://github.com/johnagan/clean-webpack-plugin/issues/125

So their “breaking” changes accidenlty affect this lib. 😭

I think I just ran into the same issue as outlined by @y0y0z above:

  • initial compilation copies files
  • subsequent compilations do not include those files, unless the files themselves were changed

of note, I’m using clean-webpack-plugin to remove all items in the directory on every compilation (due to restrictions of the environment I’m working in), so in effect every time I save any file, the files watched by copy-webpack-plugin are removed

If you would like a repo to look at, I can provide that as well.

@y0y0z it is open source, i can’t spend all of my time on this, you can start testing and search problem, it is in my todo

Thanks again, in my todo list (try to find time on this week to investigate this problem, there are a lot of other issues)

In next major release we will always emit assets, so no need this option for CleanWebpackPlugin

Close in favor https://github.com/webpack-contrib/copy-webpack-plugin/issues/261, will be fixed in next release, workaround:

 new CleanWebpackPlugin({
      cleanStaleWebpackAssets: false,
    }),

Looks like assets won’t get removed with this temporary work-around. If you delete a file, you’ll see it doesn’t get removed in the target/dist directory.

@y0y0z feel free to investigate, i am write here when i starting search problem

If you would like a repo to look at, I can provide that as well.

@frehner: please do so and make sure that clean-webpack-plugin is a part of it 😃

Here’s the repo and how to get started: https://github.com/CanopyTax/single-spa-inspector#how-to-contribute

You should only need to follow steps 1-6. Once you’ve run npm start, there will be a folder called build that will be “cleaned” and then assets put back into it on every file save. There’s no need for you to actually open Firefox or anything 😃

I just downgraded clean-webpack-plugin to v1, but you can upgrade it again to see it not working.

My current workaround was actually to downgrade clean-webpack-plugin back to v1. For some reason that makes it all work again, so I’m not sure who’s broken right now haha. But those two plugins don’t appear to work well together at the moment.

+1 - I had to alter my workflow to not use copy-webpack-plugin because of this 🙁

@evilebottnawi: i have just modified my previous comment to include a link.