webpack2利用插件clean-webpack-plugin來清除dist文件夾中重複的文件

配置文件以下css

/**
 * Created by oufeng on 2017/5/6.
 */
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: {
        main: './app/index.js',
        vendor: ['moment']
    },
    output: {
        filename: '[name].[chunkhash].js',
        path: path.resolve(__dirname, 'dist')
    },
    module:{
        rules:[
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    use: 'css-loader'
                })
            },
            {
                test: /.woff|.woff2|.svg|.eot|.ttf/,
                use: 'url-loader?prefix=font/&limit=10000'
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('styles.css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: function (module) {
                // 該配置假定你引入的 bootstrap 存在於 node_modules 目錄中
                return module.context && module.context.indexOf('node_modules') !== -1;
            }
        }),
        //爲了不vendor.*.js的hash值發生改變須要輸出一個manifest.*.js文件
        new webpack.optimize.CommonsChunkPlugin({
            name: 'manifest' //But since there are no more common modules between them we end up with just the runtime code included in the manifest file
        })
    ]
};

 

第一次運行  npm run build (webpack)時node

dist的文件夾是這樣的:webpack

 

第二次 修改一下 "./app/index.js"的內容 再 運行 npm run build git

dist的文件夾是這樣的: main.*.js和manifest.*.js都重複增長了一次。github

 

 

第三次 修改一下 "./app/index.js"的內容 再 運行 npm run build web

dist的文件夾是這樣的: main.*.js和manifest.*.js又重複增長了一次。npm

 

來到這裏樓主表示很無語啊,我run build的時候能不能把 以前的main.*.js和manifest.*.js都刪除一次暱,只保留公共的vendor.*.js文件就好啦。bootstrap

因而使用Googel大法,發現有一個插件叫clean-webpack-plugin能夠知足個人需求,並且簡單易用。app

 

//安裝插件
npm install --save-dev clean-webpack-plugin 

 

//引入插件
const CleanWebpackPlugin = require('clean-webpack-plugin');

 

//webpack.config.js中添加CleanWebpackPlugin插件
/**
 * Created by oufeng on 2017/5/6.
 */
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
    entry: {
        main: './app/index.js',
        vendor: ['moment']
    },
    output: {
        filename: '[name].[chunkhash].js',
        path: path.resolve(__dirname, 'dist')
    },
    module:{
        rules:[
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    use: 'css-loader'
                })
            },
            {
                test: /.woff|.woff2|.svg|.eot|.ttf/,
                use: 'url-loader?prefix=font/&limit=10000'
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('styles.css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: function (module) {
                // 該配置假定你引入的 bootstrap 存在於 node_modules 目錄中
                return module.context && module.context.indexOf('node_modules') !== -1;
            }
        }),
        //爲了不vendor.*.js的hash值發生改變須要輸出一個manifest.*.js文件
        new webpack.optimize.CommonsChunkPlugin({
            name: 'manifest' //But since there are no more common modules between them we end up with just the runtime code included in the manifest file
        }),
        new CleanWebpackPlugin(
            ['dist/main.*.js','dist/manifest.*.js',],  //匹配刪除的文件
            {
                root: __dirname,                 //根目錄
                verbose:  true,                  //開啓在控制檯輸出信息
                dry:      false                  //啓用刪除文件
            }
        )
    ]
};

 

這樣的配置以後,不管怎麼執行多少次的npm run build 後dist的目錄都是這個樣子的。svg

 

相關文章
相關標籤/搜索