clean-webpack-plugin icon indicating copy to clipboard operation
clean-webpack-plugin copied to clipboard

dist not cleared at all when using [hash] in the ouput.path

Open kromit opened this issue 5 years ago • 7 comments

When using this, CleanWebpackPlugin does literally nothing.

output: {
        path: path.join(__dirname, 'dist/static/js/[hash]'),
        filename: '[name].js',
    },

Please just let us clear the whole dist folder. I am going to rimraf this for now.

kromit avatar Nov 06 '19 14:11 kromit

I'm experiencing the same issue as mentioned above. I'm also using rimraf as an alternative.

In case anyone else runs into this issue, you can do one of these options:

In package.json

"scripts": {
  "build": "rimraf dist && webpack",
},

or

In webpack.config.js

const rimraf = require("rimraf");

if (process.env.NODE_ENV === "production") {
  rimraf("dist", {}, () => console.log("Removed all previous build assets.")); 
}

mattcarlotta avatar Nov 15 '19 22:11 mattcarlotta

@mattcarlotta Thanks for posting that- I didn't know that package exists.

djnorrisdev avatar Jan 03 '20 14:01 djnorrisdev

You can use remove-files-webpack-plugin for this.

Config:

new RemovePlugin({
    before: {
        include: [
            './dist'
        ]
    }
})

Note: i'm the creator of this plugin, so, it's a bit of an advertisement.

Amaimersion avatar Feb 26 '20 21:02 Amaimersion

I think the issue is just that it doesn't output anything to console by default anymore. I had to set verbose: true and then manually add some files with obvious names like NO_NOT_DELETE.js so that I could verify it was removing things. It also appears that deletion now happens just before the new files are written so in my case it was so fast I didn't actually notice 😄

daltonamitchell avatar Jun 25 '20 23:06 daltonamitchell

I'm experiencing the same issue as mentioned above. I'm also using rimraf as an alternative.

In case anyone else runs into this issue, you can do one of these options:

In package.json

"scripts": {
  "build": "rimraf dist && webpack",
},

or

In webpack.config.js

const rimraf = require("rimraf");

if (process.env.NODE_ENV === "production") {
  rimraf("dist", {}, () => console.log("Removed all previous build assets.")); 
}

Phew!!! I was going insane with this issue. Thanks for the solution mate.

getsetgopi avatar Dec 02 '20 19:12 getsetgopi

or

In webpack.config.js

const rimraf = require("rimraf");

if (process.env.NODE_ENV === "production") {
  rimraf("dist", {}, () => console.log("Removed all previous build assets.")); 
}

Great suggestion. One note though: rimraf.sync might be a safer choice here than rimraf. Wouldn't want to risk blowing away files mid build!

blazzy avatar Feb 04 '21 05:02 blazzy

When using this, CleanWebpackPlugin does literally nothing.

output: {
        path: path.join(__dirname, 'dist/static/js/[hash]'),
        filename: '[name].js',
    },

Please just let us clear the whole dist folder. I am going to rimraf this for now.

I was facing a similar issue. I ended up using cleanOnceBeforeBuildPatterns to override the behavior:

new CleanWebpackPlugin({
    cleanOnceBeforeBuildPatterns: [path.join(__dirname, 'dist')]
}),

Now the dist folder gets removed for me

haritha2112 avatar Mar 28 '23 22:03 haritha2112