webpack.config.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const config = require('./config.js');
  4. module.exports = {
  5. externals: {
  6. 'react': 'React',
  7. 'react-dom': 'ReactDOM',
  8. 'antd': 'antd',
  9. 'moment': 'moment'
  10. },
  11. resolve: { // 指定可以被 import 的文件后缀
  12. extensions: ['.js', '.jsx'],
  13. alias: { // 配置常用路径
  14. src: path.resolve(__dirname, '../src'),
  15. views: 'src/views',
  16. js: 'src/js',
  17. }
  18. },
  19. module: {
  20. rules: [{
  21. test: /\.(js|jsx)$/,
  22. loader: 'babel-loader',
  23. include: path.resolve(__dirname, '../src'),
  24. }, {
  25. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  26. loader: 'url-loader',
  27. options: {
  28. limit: 10000,
  29. name: path.posix.join(config.prod.subDirectory, 'img/[name].[hash:7].[ext]'),
  30. }
  31. }, {
  32. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  33. loader: 'url-loader',
  34. options: {
  35. limit: 10000,
  36. name: path.posix.join(config.prod.subDirectory, 'fonts/[name].[hash:7].[ext]')
  37. }
  38. }]
  39. },
  40. optimization: {
  41. // splitChunks: {
  42. // chunks: 'all',
  43. // name: 'common',
  44. // },
  45. // runtimeChunk: {
  46. // name: 'runtime',
  47. // }
  48. splitChunks: {
  49. chunks: "all", // 代码块类型 必须三选一: "initial"(初始化) | "all"(默认就是all) | "async"(动态加载)
  50. minSize: 0, // 最小尺寸,默认0
  51. minChunks: 1, // 最小 chunk ,默认1
  52. maxAsyncRequests: 1, // 最大异步请求数, 默认1
  53. maxInitialRequests: 1, // 最大初始化请求书,默认1
  54. name: () => {}, // 名称,此选项可接收 function
  55. cacheGroups: { // 缓存组会继承splitChunks的配置,但是test、priorty和reuseExistingChunk只能用于配置缓存组。
  56. priority: "0", // 缓存组优先级 false | object |
  57. vendor: { // key 为entry中定义的 入口名称
  58. chunks: "initial", // 必须三选一: "initial"(初始化) | "all" | "async"(默认就是异步)
  59. test: /react-router|axios|prop-types/, // 正则规则验证,如果符合就提取 chunk
  60. name: "vendor", // 要缓存的 分隔出来的 chunk 名称
  61. minSize: 0,
  62. minChunks: 1,
  63. enforce: true,
  64. reuseExistingChunk: true // 可设置是否重用已用chunk 不再创建新的chunk
  65. },
  66. codemirror: { // key 为entry中定义的 入口名称
  67. chunks: "initial", // 必须三选一: "initial"(初始化) | "all" | "async"(默认就是异步)
  68. test: /codemirror|react-codemirror2/, // 正则规则验证,如果符合就提取 chunk
  69. name: "codemirror", // 要缓存的 分隔出来的 chunk 名称
  70. minSize: 0,
  71. minChunks: 1,
  72. enforce: true,
  73. reuseExistingChunk: true // 可设置是否重用已用chunk 不再创建新的chunk
  74. }
  75. }
  76. }
  77. }
  78. };