plugin.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Change theme plugin
  2. import MergeLessPlugin from 'antd-pro-merge-less'
  3. // import AntDesignThemePlugin from 'antd-theme-webpack-plugin'
  4. import path from 'path'
  5. import LodashModuleReplacementPlugin from 'lodash-webpack-plugin'
  6. import CompressionPlugin from 'compression-webpack-plugin'
  7. import MiniCssExtractPlugin from 'mini-css-extract-plugin'
  8. function getModulePackageName(module) {
  9. if (!module.context) return null
  10. const nodeModulesPath = path.join(__dirname, '../node_modules/')
  11. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  12. return null
  13. }
  14. const moduleRelativePath = module.context.substring(nodeModulesPath.length)
  15. const [moduleDirName] = moduleRelativePath.split(path.sep)
  16. let packageName = moduleDirName
  17. // handle tree shaking
  18. if (packageName.match('^_')) {
  19. // eslint-disable-next-line prefer-destructuring
  20. packageName = packageName.match(/^_(@?[^@]+)/)[1]
  21. }
  22. return packageName
  23. }
  24. export default config => {
  25. // pro 和 开发环境再添加这个插件
  26. if (process.env.APP_TYPE === 'site' || process.env.NODE_ENV !== 'production') {
  27. // 将所有 less 合并为一个供 themePlugin使用
  28. const outFile = path.join(__dirname, '../.temp/ant-design-pro.less')
  29. const stylesDir = path.join(__dirname, '../src/')
  30. config.plugin('merge-less').use(MergeLessPlugin, [
  31. {
  32. stylesDir,
  33. outFile
  34. }
  35. ])
  36. // config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  37. // {
  38. // antDir: path.join(__dirname, '../node_modules/antd'),
  39. // stylesDir,
  40. // varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  41. // mainLessFile: outFile, // themeVariables: ['@primary-color'],
  42. // indexFileName: 'index.html',
  43. // generateOne: true,
  44. // lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js'
  45. // }
  46. // ])
  47. }
  48. // 这里的作用是改css的名称为乱码
  49. /*
  50. // 这里的作用是讲config输出,然后会看到extract-css,然后再修改filename、chunkFilename
  51. const temp = config.toString({
  52. ...config.toConfig(),
  53. module: {
  54. defaultRules: [
  55. {
  56. use: [
  57. {
  58. loader: 'banner-loader',
  59. options: { prefix: 'banner-prefix.txt' },
  60. },
  61. ],
  62. },
  63. ],
  64. },
  65. })
  66. console.log(temp)
  67. */
  68. config.output.filename('[name].bundle.[hash:8].js')
  69. config.output.chunkFilename('[chunkhash:8].chunk.js')
  70. config.plugin('extract-css').use(
  71. new MiniCssExtractPlugin({
  72. filename: '[name].[hash:8].css',
  73. chunkFilename: '[id].[hash:8].css'
  74. })
  75. )
  76. // 开启gzip
  77. if (process.env.NODE_ENV === 'production') {
  78. // 开启gzip
  79. config.plugin('compression-webpack-plugin').use(
  80. new CompressionPlugin({
  81. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  82. threshold: 10240, // 对超过10k的数据压缩
  83. deleteOriginalAssets: false // 不删除源文件
  84. })
  85. )
  86. }
  87. // https://github.com/lodash/lodash-webpack-plugin#feature-sets
  88. // 这里会报“predicate is not a function ”,如果不添加的话
  89. config.plugin('lodash-webpack-plugin').use(
  90. new LodashModuleReplacementPlugin({
  91. shorthands: true,
  92. collections: true,
  93. paths: true
  94. })
  95. )
  96. // https://github.com/Beven91/webpack-ant-icon-loader
  97. // code split @ant-design/icons
  98. // config.module
  99. // .rule('@ant-design/icons')
  100. // .include.add(require.resolve('@ant-design/icons/lib/dist'))
  101. // .end()
  102. // .use('ant-icon')
  103. // .loader('webpack-ant-icon-loader')
  104. config.module
  105. .rule('svg')
  106. .include.add(/.svg$/)
  107. .end()
  108. .exclude.add(/goldfish\.svg/)
  109. .end()
  110. .test(/\.svg(\?v=\d+\.\d+\.\d+)?$/)
  111. .use('babel')
  112. .loader('babel-loader')
  113. .end()
  114. .use('url-loader')
  115. .loader('url-loader')
  116. .end()
  117. .use('@svgr/webpack')
  118. .loader('@svgr/webpack')
  119. .options({
  120. babel: false
  121. })
  122. .options({
  123. icon: true
  124. })
  125. // optimize chunks
  126. // https://github.com/ant-design/ant-design-pro/blob/master/config/plugin.config.js
  127. // config.optimization.splitChunks({
  128. // cacheGroups: {
  129. // styles: {
  130. // name: '[chunkhash:8].css',
  131. // test: /\.(css|less)$/,
  132. // chunks: 'async',
  133. // minChunks: 1,
  134. // minSize: 0,
  135. // },
  136. // },
  137. // });
  138. }