webpack2利用外掛程式clean-webpack-plugin來清除dist檔案夾中重複的檔案

來源:互聯網
上載者:User

標籤:vendor   tput   app   node   logs   引入   extract   more   log   

設定檔如下

/** * 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 imstall --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的目錄都是這個樣子的。

 

 

webpack2利用外掛程式clean-webpack-plugin來清除dist檔案夾中重複的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.