johnagan / clean-webpack-plugin

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

Multiple webpack configs #122

Closed viceice closed 5 years ago

viceice commented 5 years ago

Issue description or question

Using multiple webpack configs is not possible, because the latest build will delete the files from other build.

Webpack Config

const CleanWebpackPlugin = require('clean-webpack-plugin');
const merge = require('webpack-merge');
const BASE = {
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
        libraryTarget: 'amd',
    },
    plugins: [new CleanWebpackPlugin()],
};

module.exports = [
    merge(BASE, {
        entry: {
            'jui-chart': './src/jui-chart.js',
        },
        externals: { jsdom: 'jsdom' },
        plugins: [
            new webpack.ProvidePlugin({
                window: ['jsdom', 'window'],
                document: ['jsdom', 'document'],
            }),
        ],
    }),
    merge(BASE, {
        entry: {
            jsdom: './src/jsdom.js',
        },
    }),
];

Environment

  System:
    OS: Windows 10
    CPU: (4) x64 Quad-Core AMD Opteron(tm) Processor 2378
    Memory: 5.27 GB / 18.47 GB
  Binaries:
    Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.15.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
chrisblossom commented 5 years ago

This is expected if webpack's output.path is nested or shared. You will want to clean webpack's output.path before running webpack (See #60 for an example).

new CleanWebpackPlugin({
    cleanOnceBeforeBuildPatterns: [], // disable initial clean
});
mindplay-dk commented 5 years ago

Probably a safer (more correct?) approach is to let each webpack config use a different destination sub-folder? And then add a CleanWebpackPlugin to each config, targeting the specific destination sub-folder?

(If all you want is to wipe the destination folder on startup, you can do that by just adding a rm -rf command to your build/watch scripts.)

viceice commented 5 years ago

@mindplay-dk For my setup they have to be in the same folder, because they will be required from there

prithveesh commented 4 years ago

I had a similar issue. I create another Webpack config with entry point as a empty file and output folder as the folder which i want to clean. And I ran those webpack in sequence. That cleaned my output folder and I didn't face any issue with multiple configs.

` const path = require('path'); const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = { mode: 'production', entry: { clean: './clean.js', }, output: { path: path.resolve('./build/client'), }, plugins: [ new CleanWebpackPlugin(), ], } `