webpack.lib.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var path = require('path');
  2. var webpack = require('webpack');
  3. const WebpackMerge = require('webpack-merge');
  4. const baseConfig = require('./webpack.base.js');
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const TerserPlugin = require('terser-webpack-plugin');
  7. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  8. module.exports = WebpackMerge.merge(baseConfig,{
  9. mode: 'production',
  10. // 发布组件
  11. entry: './packages/index.js',
  12. output: {
  13. path: path.resolve(__dirname, './dist'),
  14. publicPath: '/dist/',
  15. filename: 'pc-component-v3.js',
  16. library: {
  17. type: 'module',
  18. },
  19. //library: 'pc-component-v3',
  20. //libraryTarget: 'umd',
  21. // 「devtool 中模块」的文件名模板(用于冲突)
  22. //umdNamedDefine: false,
  23. },
  24. experiments: {
  25. outputModule: true,
  26. },
  27. optimization: {
  28. minimize: true, // 压缩 bundle
  29. minimizer: [
  30. new TerserPlugin({
  31. parallel: true,
  32. terserOptions: {
  33. // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
  34. format: {
  35. // 保留 webpackIgnore: true 的注解
  36. comments: /(\s*webpackIgnore)/i,
  37. },
  38. },
  39. }),
  40. ],
  41. },
  42. //devtool: 'source-map',
  43. plugins: (module.exports.plugins || []).concat([
  44. // CSS 提取
  45. new MiniCssExtractPlugin({
  46. filename: 'pc-component-v3.css',
  47. }),
  48. // 打包分析插件
  49. // https://github.com/webpack-contrib/webpack-bundle-analyzer
  50. //下面是该插件的默认配置,一般无需修改
  51. new BundleAnalyzerPlugin({
  52. // 可以是`server`,`static`或`disabled`。
  53. // 在`server`模式下,分析器将启动HTTP服务器来显示软件包报告。
  54. // 在“静态”模式下,会生成带有报告的单个HTML文件。
  55. // 在`disabled`模式下,你可以使用这个插件来将`generateStatsFile`设置为`true`来生成Webpack Stats JSON文件。
  56. analyzerMode: 'server',
  57. // 将在“服务器”模式下使用的主机启动HTTP服务器。
  58. analyzerHost: '127.0.0.1',
  59. // 将在“服务器”模式下使用的端口启动HTTP服务器。
  60. analyzerPort: 8888,
  61. // 路径捆绑,将在`static`模式下生成的报告文件。
  62. // 相对于捆绑输出目录。
  63. reportFilename: 'report.html',
  64. // 模块大小默认显示在报告中。
  65. // 应该是`stat`,`parsed`或者`gzip`中的一个。
  66. // 有关更多信息,请参见“定义”一节。
  67. defaultSizes: 'parsed',
  68. // 在默认浏览器中自动打开报告
  69. openAnalyzer: true,
  70. // 如果为true,则Webpack Stats JSON文件将在bundle输出目录中生成
  71. generateStatsFile: false,
  72. // 如果`generateStatsFile`为`true`,将会生成Webpack Stats JSON文件的名字。
  73. // 相对于捆绑输出目录。
  74. statsFilename: 'stats.json',
  75. // stats.toJson()方法的选项。
  76. // 例如,您可以使用`source:false`选项排除统计文件中模块的来源。
  77. // 在这里查看更多选项:https: //github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21
  78. statsOptions: null,
  79. logLevel: 'info', // 日志级别。可以是'信息','警告','错误'或'沉默'。
  80. }),
  81. ]),
  82. // 不把第三方库打包到bundle中
  83. externals: {
  84. jQuery: 'window.$',
  85. jquery: 'window.$',
  86. $: 'window.$',
  87. 'bootstrap': 'bootstrap',
  88. 'moment': 'moment',
  89. // 不将vue代码打包进我们的组件库代码中,如果将vue代码打包进组件库中则会报错
  90. 'vue': 'vue',
  91. 'vue-i18n': 'vue-i18n',
  92. 'vue-router': 'vue-router',
  93. 'v-tooltip': 'v-tooltip',
  94. 'vuedraggable': 'vuedraggable',
  95. 'amis': 'amis',
  96. },
  97. });