Webpack2 using plug-in Clean-webpack-plugin to clear duplicate files in Dist folder

Source: Internet
Author: User

The configuration file is as follows

/** * 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: [NewExtracttextplugin (' Styles.css '),        NewWebpack.optimize.CommonsChunkPlugin ({name:' Vendor ', Minchunks:function(module) {//This configuration assumes that the bootstrap you have introduced exists in the Node_modules directory                returnModule.context && module.context.indexOf (' node_modules ')!==-1; }        }),        //To avoid a change in the hash value of the vendor.*.js, you need to output a manifest.*.js file        NewWebpack.optimize.CommonsChunkPlugin ({name:' Manifest '//But since there is no more common modules between them we end up with just the runtime code included in the manifest File        })    ]};

The first time you run NPM Run build (webpack)

The folder for Dist is this:

Modify the "./app/index.js" content for the second time to run NPM run build

The Dist folder is like this: both Main.*.js and manifest.*.js have been repeatedly added once.

Modify the "./app/index.js" content for the third time to run NPM run build

The Dist folder is like this: Main.*.js and manifest.*.js have been added again.

Came here to say that the landlord is very silent Ah, I run build can be before the main.*.js and Manifest.*.js are deleted once, only keep the public vendor.*.js file is good.

So using Googel Dafa, found that there is a plug-in called Clean-webpack-plugin can meet my needs, but also easy to use.

// Installing plugins

// Introducing the plugin const Cleanwebpackplugin = require (' Clean-webpack-plugin ');

//Add Cleanwebpackplugin plugin in Webpack.config.js/** * 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: [NewExtracttextplugin (' Styles.css '),        NewWebpack.optimize.CommonsChunkPlugin ({name:' Vendor ', Minchunks:function(module) {//This configuration assumes that the bootstrap you have introduced exists in the Node_modules directory                returnModule.context && module.context.indexOf (' node_modules ')!==-1; }        }),        //To avoid a change in the hash value of the vendor.*.js, you need to output a manifest.*.js file        NewWebpack.optimize.CommonsChunkPlugin ({name:' Manifest '//But since there is no more common modules between them we end up with just the runtime code included in the manifest File        }),        NewCleanwebpackplugin ([' Dist/main.*.js ', ' dist/manifest.*.js ',],//Match deleted file {root: __dirname,//root directoryVerbosetrue,//turn on console output informationDryfalse                    //Enable Delete files            }        )    ]};

After such a configuration, no matter how many times the NPM run build is executed, the Dist directory looks like this.

Webpack2 using plug-in Clean-webpack-plugin to clear duplicate files in Dist folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.