webpack2利用插件clean-webpack-plugin来清除dist文件夹中重复的文件

配置文件如下

复制代码
/**
 * 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)时

dist的文件夹是这样的:

 

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

dist的文件夹是这样的: main.*.js和manifest.*.js都重复增加了一次。

 

 

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

dist的文件夹是这样的: main.*.js和manifest.*.js又重复增加了一次。

 

来到这里楼主表示很无语啊,我run build的时候能不能把 之前的main.*.js和manifest.*.js都删除一次昵,只保留公共的vendor.*.js文件就好啦。

于是使用Googel大法,发现有一个插件叫clean-webpack-plugin可以满足我的需求,而且简单易用。

 

//安装插件
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的目录都是这个样子的。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值