webpack.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //根据Webpack中的tsconfig路径加载模块 插件
  2. const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
  3. const path = require('path');
  4. const config = require('./config'); //基础信息配置
  5. const constants = require('./constants'); //常量配置
  6. const { proxySetting } = require('./proxy/proxy'); //常量配置
  7. const styleRules = require('./rules/styleRules'); //样式规则配置
  8. const jsRules = require('./rules/jsRules'); //js规则配置
  9. const fileRules = require('./rules/fileRules'); //图片文件规则配置
  10. const plugins = require('./plugins'); //插件配置
  11. const { assetsPath, resolve } = require('./utils'); //工具类
  12. const apiMocker = require('webpack-api-mocker');
  13. const mocker = path.resolve(__dirname, '../mock/index.js');
  14. const env = require('./parseEnv');
  15. const packageName = require('../package.json').name;
  16. const conf = {
  17. mode: 'development', //模式通过选择 development, production 默认值为 production。
  18. entry: { app: ['./src/index.js'] }, //入口。webpack从哪里开始构建项目依赖
  19. optimization: {
  20. runtimeChunk: true,
  21. sideEffects: true, // 默认false,表示是否移除无副作用的模块
  22. //分开打js
  23. splitChunks: {
  24. // include all types of chunks
  25. chunks: 'all',
  26. cacheGroups: {
  27. react_vendor: {
  28. test: /[/\\]node_modules[/\\](react|react-dom|react-document-title|react-iframe)[/\\]/,
  29. name: 'react_vendor',
  30. chunks: 'all',
  31. },
  32. antv_vendor: {
  33. test: /[/\\]node_modules[/\\](@antv\/*)[/\\]/,
  34. name: 'antv_vendor',
  35. chunks: 'all',
  36. },
  37. antd_vendor: {
  38. test: /[/\\]node_modules[/\\](antd)[/\\]/,
  39. name: 'antd_vendor',
  40. chunks: 'all',
  41. },
  42. antd_icons_vendor: {
  43. test: /[/\\]node_modules[/\\](@ant-design\/*)[/\\]/,
  44. name: 'antd_icons_vendor',
  45. chunks: 'all',
  46. },
  47. util_vendor: {
  48. test: /[/\\]node_modules[/\\](immer|moment|lodash-es|axios|ahooks|marked)[/\\]/,
  49. name: 'util_vendor',
  50. chunks: 'all',
  51. },
  52. ui_vendor: {
  53. test: /[/\\]node_modules[/\\](classnames|styled-components)[/\\]/,
  54. name: 'ui_vendor',
  55. chunks: 'all',
  56. },
  57. json_edit_vendor: {
  58. test: /[/\\]node_modules[/\\](jsoneditor)[/\\]/,
  59. name: 'json_edit_vendor',
  60. chunks: 'all',
  61. },
  62. defaultVendors: {
  63. test: /[/\\]node_modules[/\\]/,
  64. priority: -10,
  65. reuseExistingChunk: true,
  66. },
  67. default: {
  68. minChunks: 2,
  69. priority: -20,
  70. reuseExistingChunk: true,
  71. },
  72. },
  73. },
  74. },
  75. output: {
  76. path: config.assetsRoot, //路径
  77. filename: env.NODE_ENV === 'development' ? '[name].js' : assetsPath('js/[name].[contenthash].js'), //打包后的文件bundle名称
  78. chunkFilename: env.NODE_ENV === 'development' ? '[name].js' : assetsPath('js/[name].[id].[contenthash].js'), //chunk 文件指的就是要懒加载的代码。
  79. publicPath: process.env.NODE_ENV === 'development' ? '/' : '/mark-tool/', //公共路径
  80. pathinfo: false,
  81. clean: true, // 构建之前清理
  82. library: `${packageName}`,
  83. libraryTarget: 'umd',
  84. },
  85. resolve: {
  86. //配置模块如何解析
  87. //按顺序解析这些后缀名文件
  88. extensions: constants.FILE_EXTENSIONS,
  89. plugins: [
  90. new TsconfigPathsPlugin({
  91. configFile: resolve('tsconfig.webpack.json'),
  92. extensions: constants.FILE_EXTENSIONS,
  93. }),
  94. ],
  95. },
  96. module: {
  97. //处理不同文件的规则
  98. rules: [...styleRules, ...jsRules, ...fileRules],
  99. },
  100. plugins, //插件配置引入
  101. stats: 'minimal',
  102. target: 'web',
  103. devtool: config.sourceMap, //生产sourcemap
  104. };
  105. if (process.env.NODE_ENV === 'development') {
  106. conf.devServer = {
  107. // 不显示模块信息
  108. stats: 'errors-warnings',
  109. // 日志级别- 'info': 'silent' | 'trace' | 'debug' | 'info' | 'warn' | 'error'
  110. clientLogLevel: 'warn',
  111. //端口
  112. port: config.devPort,
  113. //热加载
  114. hot: true,
  115. inline: true, //缺少该配置,输入路由会404
  116. historyApiFallback: true, //缺少该配置,输入路由会404
  117. //为每个静态文件开启 gzip
  118. compress: true,
  119. disableHostCheck: true,
  120. progress: true,
  121. open: true, // 自动打开浏览器
  122. proxy:
  123. env._SETTING_ENABLE_PROXY_API && env._SETTING_ENABLE_PROXY_API.replace(/"/g, '') === 'true' ? proxySetting : {}, // 代理接口转发
  124. before(app) {
  125. if (env._SETTING_ENABLE_PROXY_API && env._SETTING_ENABLE_MOCK_API.replace(/"/g, '') === 'true')
  126. apiMocker(app, mocker, {
  127. changeHost: true,
  128. });
  129. },
  130. };
  131. }
  132. module.exports = conf;