zoukankan      html  css  js  c++  java
  • 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的目录都是这个样子的。

  • 相关阅读:
    js 时间戳格式化
    有关Safari 浏览器的文章
    前端图片旋转动画
    vue中textarea标签自适应高度
    pip 报错 Fatal error in launcher: Unable to create process using
    HTML列表多点击事件
    js获取浏览器版本信息
    SVG圆形进度条
    基于蚂蚁金服"AntDesignVue-Menu导航菜单"实现根据初始路由自动选中对应菜单解决刷新后菜单选择状态丢失问题(支持根路径菜单)
    java根据权重进行排序
  • 原文地址:https://www.cnblogs.com/oufeng/p/6819320.html
Copyright © 2011-2022 走看看