插件 | Plugins

Plugins

plugins 选项用于以各种方式自定义webpack 构建过程。webpack 附带了各种内置插件,可以通过 webpack.[plugin-name] 访问这些插件。请查看这个页面获取插件列表和对应文档,但请注意这只是其中一部分,社区中还有许多插件。

注意:本页仅讨论使用插件,但如果您有兴趣编写自己的插件,请访问 Writing a Plugin

plugins

array

webpack 插件列表。例如,当多个 bundle 共享一些相同的依赖,CommonsChunkPlugin 有助于提取这些依赖到共享的 bundle 中,来避免重复打包。可以像这样添加:

plugins: [ new webpack.optimize.CommonsChunkPlugin{ ... }) ]

一个更复杂的例子,使用多个插件,可能看起来像这样:

var webpack = require('webpack') // importing plugins that do not come by default in webpack var ExtractTextPlugin = require('extract-text-webpack-plugin' var DashboardPlugin = require('webpack-dashboard/plugin' // adding plugins to your configuration plugins: [ // build optimization plugins new webpack.optimize.CommonsChunkPlugin{ name: 'vendor', filename: 'vendor-[hash].min.js', }), new webpack.optimize.UglifyJsPlugin{ compress: { warnings: false, drop_console: false, } }), new ExtractTextPlugin{ filename: 'build.min.css', allChunks: true, }), new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), // compile time plugins new webpack.DefinePlugin{ 'process.env.NODE_ENV': '"production"', }), // webpack-dev-server enhancement plugins new DashboardPlugin(), new webpack.HotModuleReplacementPlugin(), ]