内置 Webpack Plugin

plugin 别名 默认是否开启/开启环境
npm-install-webpack-plugin npm 否
webpack.DefinePlugin define 是
webpack.NamedModulesPlugin nameModule 是/dev
webpack.HashedModuleIdsPlugin hashModule 是/test,prod
webpack.optimize.ModuleConcatenationPlugin module 是
webpack.NoEmitOnErrorsPlugin error 是
webpack.HotModuleReplacementPlugin hot 是/dev
webpack-manifest-resource-plugin manifest 是
progress-bar-webpack-plugin progress 是/dev
directory-named-webpack-plugin directoryname 是
mini-css-extract-plugin extract 是/test, prod
webpack.optimize.CommonsChunkPlugin commonsChunk 是
html-webpack-plugin html 是
webpack.optimize.UglifyJsPlugin uglifyJs 是/prod
imagemin-webpack-plugin imagemini 是/prod
webpack-bundle-analyzer analyzer 否
命令行开启:
easy build —size
stats-webpack-plugin stats 否
命令行开启:
easy build —size stats
speed-measure-webpack-plugin speed 否
命令行开启:
easy build —speed
service-worker-precache-webpack-plugin serviceworker 否
clean-webpack-plugin clean 是(test, prod), >=easywebpack@4.6.0
optimize-css-assets-webpack-plugin cssmini 是(prod), >=easywebpack@4.7.1
copy-webpack-plugin copy 否
webpack-filter-warnings-plugin filter 是
>=easywebpack@4.10.0
vconsole-webpack-plugin vconsole easywebpack@4.12.0

config.plugins 配置

config.plugins 非必须,支持 Object | Array。 这里的plugins 是对 Webpack plugins 的简化和增强。

  • 兼容 Webpack 原生数组配置

  • [增强]支持通过别名对内置 plugin 插件的开启和禁用,以及参数配置

  • [增强]支持通过别名的方式添加 plugin 插件

插件配置规范

  • 内置 plugin 扩展参数统一通过 args 节点配置
  1. // ${app_root}/webpack.config.js
  2. module.exports = {
  3. plugins:[
  4. {
  5. ${plugin别名}:{} // 值可以为 Boolean | Object
  6. }
  7. ]
  8. }
  9. 或
  10. module.exports = {
  11. plugins:{
  12. ${plugin别名}:{} // 值可以为 Boolean | Object
  13. }
  14. }
  15. }

插件配置举例

  1. // ${app_root}/webpack.config.js
  2. module.exports = {
  3. ......
  4. plugins:[
  5. { uglifyJs: false }, // 禁用内置js/css压缩插件配置
  6. { imagemini: false }, // 禁用内置图片压缩插件配置
  7. { ts: true }, // 开启内置插件配置
  8. { copy: [ { from: 'app/web/asset', to: 'public/asset' } ], // 已内置插件配置
  9. new webpack.NoEmitOnErrorsPlugin(), // 原生组件
  10. new webpack.HotModuleReplacementPlugin(), //原生组件
  11. {
  12. env: ['dev'] // 非必需,支持 dev, test, prod 默认所有
  13. name: new webpack.NamedModulesPlugin() // 插件实例
  14. }
  15. ]
  16. }

添加新插件

我要添加一个全新且 easywebpack 没有内置的 webpack-visualizer-plugin 插件, 可以这样配置如下:

  • Webpack 原生方式
  1. const VisualizerPlugin = require('webpack-visualizer-plugin');
  2. module.exports = {
  3. plugins:[
  4. new VisualizerPlugin({ filename: './visualizer.html' });
  5. ]
  6. }
  7. 或:
  8. {
  9. plugins:[
  10. visualizer:{
  11. env: ['dev'], // 开发环境启用
  12. name: 'webpack-visualizer-plugin',
  13. args: {
  14. filename: './visualizer.html'
  15. }
  16. }
  17. ]
  18. }
  • 指定环境生效
  1. {
  2. plugins:{
  3. visualizer:{
  4. env: ['dev'], // 开发环境启用
  5. name: 'webpack-visualizer-plugin',
  6. args: {
  7. filename: './visualizer.html'
  8. }
  9. }
  10. }
  11. }

或

  1. var Visualizer = require('webpack-visualizer-plugin');
  2. {
  3. plugins:{
  4. visualizer:{
  5. env: ['dev'], // 开发环境启用
  6. name: new Visualizer({ filename: './visualizer.html'})
  7. }
  8. }
  9. }

修改内置插件参数

这里以修改已经内置 easywebpack 的压缩插件 uglifyJs 配置信息为例:

  1. {
  2. plugins: [{
  3. uglifyJs: {
  4. uglifyOptions: {
  5. warnings: false,
  6. compress: {
  7. dead_code: true,
  8. drop_console: true,
  9. drop_debugger: true
  10. },
  11. output: {
  12. comments: false
  13. }
  14. }
  15. }
  16. }
  17. ]
  1. // easywebpack 3
  2. {
  3. plugins:{
  4. uglifyJs: {
  5. args: {
  6. compress: {
  7. warnings: false,
  8. dead_code: true,
  9. drop_console: true,
  10. drop_debugger: true
  11. }
  12. }
  13. }
  14. }
  15. }
  16. // easywebpack 4 (args 参数可省略)
  17. {
  18. plugins:{
  19. uglifyJs: {
  20. args: {
  21. uglifyOptions: {
  22. warnings: false,
  23. compress: {
  24. dead_code: true,
  25. drop_console: true,
  26. drop_debugger: true
  27. },
  28. output: {
  29. comments: false
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }

添加环境变量 webpack.DefinePlugin

  1. // ${app_root}/webpack.config.js
  2. module.exports = {
  3. plugins:[{
  4. define:{
  5. 'BUILD_MODE': process.env.BUILD_ENV
  6. }
  7. }]
  8. }
  9. 或
  10. module.exports = {
  11. plugins:{
  12. define:{
  13. 'BUILD_MODE': process.env.BUILD_ENV
  14. }
  15. }
  16. }

自动加载模块 webpack.ProvidePlugin

  1. // ${app_root}/webpack.config.js
  2. module.exports = {
  3. plugins:[{
  4. provide:{
  5. $: 'jquery',
  6. jQuery: 'jquery'
  7. }
  8. }
  9. }]
  10. 或
  11. module.exports = {
  12. plugins:{
  13. provide:{
  14. $: 'jquery',
  15. jQuery: 'jquery'
  16. }
  17. }
  18. }