plugins
选项用于以多种方式自定义 webpack 构建过程。Webpack 附带了多种内置插件,可在 webpack.[plugin-name]
下使用。有关插件和文档的列表,请参阅 插件页面,但请注意社区中还有更多插件。
webpack 插件数组。例如,DefinePlugin
允许您创建可在编译时配置的全局常量。这对于允许开发版本和发行版本之间有不同的行为很有用。从 webpack 5.87.0 开始,可以使用假值有条件地禁用特定插件。
webpack.config.js
module.exports = {
//...
plugins: [
new webpack.DefinePlugin({
// Definitions...
}),
false && new webpack.IgnorePlugin(), // disabled conditionally
],
};
一个更复杂的示例,使用多个插件,可能如下所示
webpack.config.js
var webpack = require('webpack');
// importing plugins that do not come by default in webpack
var DashboardPlugin = require('webpack-dashboard/plugin');
// adding plugins to your configuration
module.exports = {
//...
plugins: [
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(),
],
};