johnagan / clean-webpack-plugin

A webpack plugin to remove your build folder(s) before building
MIT License
1.96k stars 135 forks source link

Unmatched files are deleted in v2 #132

Closed shravan2x closed 5 years ago

shravan2x commented 5 years ago

Issue description or question

Perhaps I'm missing something here, but files I didn't expect to be removed are being removed by the plugin in v2. In v1, I used ['index.html', '*.js', '*.js.map', '*.css', '*.css.map'] to remove specific files in my output directory, which worked fine.

When upgrading to v2, I set the same values as cleanOnceBeforeBuildPatterns: ['index.html', '*.js', '*.js.map', '*.css', '*.css.map']. However, files that shouldn't be matched are still deleted by the plugin:

clean-webpack-plugin: dry ..\App\wwwroot\favicon.ico
clean-webpack-plugin: dry ..\App\wwwroot\manifest.json

Webpack Config

    new CleanWebpackPlugin(/*['index.html', '*.js', '*.js.map', '*.css', '*.css.map'],*/ {
        dry: true,
        verbose: true,
        cleanOnceBeforeBuildPatterns: ['index.html', '*.js', '*.js.map', '*.css', '*.css.map'],
        dangerouslyAllowCleanPatternsOutsideProject: true
    }),

Environment

Run: npx envinfo --system --binaries --npmPackages clean-webpack-plugin,webpack

  System:
    OS: Windows 10
    CPU: (8) x64 Intel(R) Core(TM) i7-6820HK CPU @ 2.70GHz
    Memory: 20.71 GB / 63.96 GB
  Binaries:
    Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
shravan2x commented 5 years ago

Possibly the same issue as https://github.com/johnagan/clean-webpack-plugin/issues/128 ?

shravan2x commented 5 years ago

Reverting to 1.0.1 is a temporary fix.

chrisblossom commented 5 years ago

Try:

    new CleanWebpackPlugin(/*['index.html', '*.js', '*.js.map', '*.css', '*.css.map'],*/ {
        dry: true,
        verbose: true,
        cleanOnceBeforeBuildPatterns: ['index.html', '*.js', '*.js.map', '*.css', '*.css.map'],
        cleanStaleWebpackAssets: false,
    }),
shravan2x commented 5 years ago

If I add that option, no files get deleted anymore, not even the ones that cleanOnceBeforeBuildPatterns should match.

chrisblossom commented 5 years ago

Have you tried:

    new CleanWebpackPlugin({
        dry: true,
        verbose: true,
        cleanStaleWebpackAssets: false,
    }),

By default, this plugin removes all files in webpack's output.path. If nothing is being removed, it is my guess you have nested files and your patterns do not match that. See sindresorhus/del#patterns. You're probably wanting: **/*.js ...

shravan2x commented 5 years ago

No, all the patterns I have mentioned are expected at the root of the output directory. I have checked the link you sent me and am confident that my patterns are correct.

tobni commented 5 years ago

I have the same exact issue. Using webpack 4.29.4.

chrisblossom commented 5 years ago

@tobni please post your clean-webpack-plugin configuration, what exactly is happening, and what you expect to happen. Most likely I'll need a minimal example repository to further help.

dotnetshadow commented 5 years ago

@chrisblossom I too have the same issue

Basically what's happening is when I first run webpack everything is fine.

But if I go to edit say a javascript file and it gets transpiled etc in watch mode, your plugin runs and for some reason it starts deleting the js related files even though they are being excluded or not told to be deleted.

new CleanWebpackPlugin({
                cleanOnceBeforeBuildPatterns: [],
                cleanAfterEveryBuildPatterns: ['css/admin', 'css/fonts', 'css/vendor/kendo-custom', 'css/site'],
                protectWebpackAssets: false,
                dry: true,
                verbose: true
            }),
clean-webpack-plugin: dry wwwroot\js\vendor\bootstrap.bundle.min.js.map
clean-webpack-plugin: dry wwwroot\js\vendor\jquery.min.js
clean-webpack-plugin: dry wwwroot\js\vendor\jquery.min.map
clean-webpack-plugin: dry wwwroot\js\vendor\jquery.validate.min.js
clean-webpack-plugin: dry wwwroot\js\vendor\jquery.validate.unobtrusive.min.js
clean-webpack-plugin: dry wwwroot\js\vendor\kendo.ui.core.min.js
clean-webpack-plugin: dry wwwroot\js\vendor\kendo.ui.core.min.js.map

I wanted to remove the file css/admin which is js file that gets generated by webpack for css files when extracting. As a result I only want to delete those files. If I put a negative pattern such as 'css/**/*' then it won't delete that file.

UPDATE
I think perhaps I can fix it by using cleanAfterEveryBuildPatterns: ['!css/**/*', '!js/**/*','css/admin', 'css/fonts', 'css/vendor/kendo-custom', 'css/site'] putting the negative pattern first then deleting what I want

chrisblossom commented 5 years ago

@dotnetshadow You could also look into the cleanStaleWebpackAssets: false option. Looks like you are using plugins that do not persist webpack assets during rebuilds.

chrisblossom commented 5 years ago

Closing this issue because it has stalled and multiple people are trying to piggyback on it instead of opening their own issue. @shravan2x please reopen with an example repository if you have not solved your issue yet.