johnagan / clean-webpack-plugin

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

Removes whole project when outputpath is root #183

Open thet opened 4 years ago

thet commented 4 years ago

Issue description or question

When using clean-webpack-plugin when webpacks output.path is the project root, everything is blindly deleted including the .git directory. IMO it should only remove old build assets - I don't know if it's possible to find out which one are build assets and which one are not.

Webpack Config

The config is in webpack/base.config.js.

process.traceDeprecation = true;
const path = require('path');
const webpack = require('webpack');

// plugins
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = env => {
    return {
        entry: {
            "bundle": path.resolve(__dirname, "../src/init.js"),
        },
        output: {
            filename: "[name].js",
            path: path.resolve(__dirname, '../'),
        },
        },
        plugins: [
            new CleanWebpackPlugin(),
        ]
    };
};

Environment

  System:
    OS: Linux 5.4 Ubuntu 20.04.1 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 6.36 GB / 23.39 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 12.16.3 - ~/.nvm/versions/node/v12.16.3/bin/node
    Yarn: 1.22.5 - /home/_thet/data/dev/syslab/REPOS/patternslib/Patterns-es6/node_modules/.bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v12.16.3/bin/npm
  npmPackages:
    clean-webpack-plugin: ^3.0.0 => 3.0.0 
    webpack: ^4.43.0 => 4.44.1 
Sociosarbis commented 4 years ago

What about use cleanOnceBeforeBuildPatterns option, and then you can only remove the files that match your defined patterns.

alpadev commented 4 years ago

To my understanding, the idea in general is it shouldn't be possible to delete the working directory and/or outside of it without setting dangerouslyAllowCleanPatternsOutsideProject to true. Though it looks like this isn't properly working depending on the folder structure and how webpack is run.

@thet would you mind adding some more details about that?

As far as I can tell your project looks something like this:

app
├── src
│   └── init.js
├── webpack
│   └── base.config.js
└── bundle.js

It would be nice to know the location of the package.json (or if there is more than one) and how you run webpack e.g. like npx webpack ... or using some npm script like npm run build.

Edit: the removal of the working directory is caused by del version being outdated - that is already fixed with https://github.com/johnagan/clean-webpack-plugin/pull/165 ... You likely have to use cleanOnceBeforeBuildPatterns though as Sociosarbis mentioned e.g.

new CleanWebpackPlugin({
  cleanOnceBeforeBuildPatterns: ['bundle.js']
})
ganeshrvel commented 4 years ago

this happened to me just now! luckily I had the backup of my project.

Please document this in the readme and warn users of such incidents

strarsis commented 3 years ago

Yes, this is very bad indeed. Even the .git folder was removed which meant that I lost everything since the last git push. That's very bad.

ganeshrvel commented 3 years ago

They should put a check for the root directory ASAP!

eddjimlab commented 3 years ago

It removes everything except webpack files !!!