johnagan / clean-webpack-plugin

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

cleanOnceBeforeBuildPatterns, Like #118, it runs every watched build. #153

Closed cosmoKenney closed 5 years ago

cosmoKenney commented 5 years ago

Issue description or question

Like issue #118, all assets in cleanOnceBeforeBuildPatterns are removed during a rebuild via webpack watch. However webpack is only writing the changed files back to the output directories. This makes webpack watch unusable since most of the assets are removed during a rebuild. In this particular project output.path is the root (./) of the project. And there are 50 entry pages. I use html-webpack-plugin to write*.inc files to /inc I use extract-text-webpack-plugin to write *.css files to /css I use file-loader to write images to /img I use output.filename: "js/[name].js" to write my .js files to /js. I need to specify folder and file-extension wildcards (`js/.js) in thecleanOnceBeforeBuildPatternsconfiguration, because there are files (web.config`) in each of the above mentioned folders that need to stay there.

Webpack Config

My webpack config is too large and made up of multiple parts in separate files to list here. Relevant info is below:

const clientRoot = path.resolve( __dirname, "./" );
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// ...
    output: {
        path: clientRoot, 
        filename: "js/[name].js",
        publicPath: "/"
    },
// ...
    plugins: [
        new CleanWebpackPlugin( { cleanOnceBeforeBuildPatterns: ['inc/*.inc', 'js/*.js', 'img/*.png', 'img/*.jpg', 'img/*.gif', 'css/*.css'], verbose: true, dry: true } ),

Environment

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

npx envinfo --system --binaries --npmPackages clean-webpack-plugin,webpack
npx: installed 1 in 1.353s

  System:
    OS: Windows 10
    CPU: (12) x64 Intel(R) Xeon(R) E-2176M  CPU @ 2.70GHz
    Memory: 17.33 GB / 31.84 GB
  Binaries:
    Node: 11.12.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.7.0 - C:\Program Files\nodejs\npm.CMD

If you need any more info, just let me know.

cosmoKenney commented 5 years ago

...I've been experimenting with cleanStaleWebpackAssets and that seems to reduce the set of files that are removed during a watched rebuild. However it is removing files that it has no business removing. For instance it removed all webpac created files from /img. But then webpack doesn't rewrite those during a watched rebuild.

chrisblossom commented 5 years ago

Are you saying when you set cleanStaleWebpackAssets: false files are still being removed on rebuild?

However it is removing files that it has no business removing.

The issue is some plugins don't persist their assets as part of webpack's asset list. Since you are using an advanced webpack setup, I'd recommend setting cleanStaleWebpackAssets: false or you can manually add negative glob patterns to cleanAfterEveryBuildPatterns that match the files you do not want removed.

cosmoKenney commented 5 years ago

@chrisblossom yes! During a webpack --watch build that was started due to saving a file, will run the same clean-webpack-plugin logic as an initial build with cleanOnceBeforeBuildPatterns setup. See my sample config excerpt in the first post.

chrisblossom commented 5 years ago

webpack --watch on a build due to saving a file will execute just like an initial build with cleanOnceBeforeBuildPatterns setup like in my sample config above

I don't understand this sentence.

Try this and paste the output of verbose: true:

new CleanWebpackPlugin( { cleanStaleWebpackAssets: false, verbose: true, dry: true } ),
cosmoKenney commented 5 years ago

@chrisblossom I need to use cleanOnceBeforeBuildPatterns because my webpack generated assets are in sub-folders of the root of my project. Without any file specs specified in cleanOnceBeforeBuildPatterns all of my project files are removed -- I tried this yesterday: new CleanWebpackPlugin( { cleanStaleWebpackAssets: false, verbose: true, dry: true } ),

chrisblossom commented 5 years ago

Are you saying your source files and build files are in the same directory?

Please paste the output. Rename any sensitive filenames.

cosmoKenney commented 5 years ago

This is a classic asp.net website, so the source files start at the root. I'll probably just run some deletes before I start webpack. This is a large legacy project and so the output of the suggested config contains hundreds or thousands of files.

chrisblossom commented 5 years ago

I am still having a very hard time understanding your issue / what exactly you need. Please reword in a very clear straight forward way. Pretend I don't have access to your project ;) Or even better, create a minimal repository that demonstrates your isssue.

cleanOnceBeforeBuildPatterns only runs once, not on rebuilds. If your issue is only with rebuilds, set cleanStaleWebpackAssets: false and nothing will be automatically removed.

I'll probably just run some deletes before I start webpack.

Definitely a good option.

cosmoKenney commented 5 years ago

@chrisblossom I just created a test project locally and I cannot replicate. I'll look into this some more on my end as my webpack build is rather complex.