webpack.dev.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import portfinder from 'portfinder';
  2. import { Configuration } from 'webpack';
  3. import { outputStaticUrl } from '../constant';
  4. import TerminalPrintPlugin from '../TerminalPrintPlugin';
  5. import { chalkINFO } from '../utils/chalkTip';
  6. import { resolveApp } from '../utils/path';
  7. console.log(chalkINFO(`读取: ${__filename.slice(__dirname.length + 1)}`));
  8. export default new Promise((resolve) => {
  9. // 默认端口8000,如果被占用了,会自动递增+1
  10. const defaultPort = 8000;
  11. portfinder
  12. .getPortPromise({
  13. port: defaultPort,
  14. stopPort: 9000,
  15. })
  16. .then((port) => {
  17. const devConfig: Configuration = {
  18. target: 'web',
  19. // https://github.com/webpack/webpack/blob/main/lib/config/defaults.js
  20. mode: 'development',
  21. stats: 'none',
  22. // https://webpack.docschina.org/configuration/devtool/
  23. // devtool: 'eval-cheap-module-source-map',
  24. devtool: 'eval', // eval,具有最高性能的开发构建的推荐选择。
  25. // 这个infrastructureLogging设置参考了vuecli5,如果不设置,webpack-dev-server会打印一些信息
  26. infrastructureLogging: {
  27. level: 'none',
  28. },
  29. devServer: {
  30. client: {
  31. logging: 'none', // https://webpack.js.org/configuration/dev-server/#devserverclient
  32. },
  33. hot: true, // 启用 webpack 的热模块替换功能
  34. // hot: 'only', // 要在构建失败的情况下启用热模块替换而不刷新页面作为后备,请使用hot: 'only'。但在vue项目的话,使用only会导致ts文件没有热更,得使用true
  35. compress: true, // 为所有服务启用gzip 压缩
  36. port, // 开发服务器端口,默认8080
  37. open: false, // 告诉 dev-server 在服务器启动后打开浏览器。
  38. historyApiFallback: {
  39. rewrites: [
  40. /**
  41. * 如果publicPath设置了/abc,就不能直接设置historyApiFallback: true,这样会重定向到vue3-blog-admin根目录下的index.html
  42. * publicPath设置了/abc,就重定向到/abc,这样就可以了
  43. */
  44. {
  45. from: new RegExp(outputStaticUrl(false)),
  46. to: outputStaticUrl(false),
  47. },
  48. ],
  49. },
  50. /**
  51. * devServer.static提供静态文件服务器,默认是 'public' 文件夹。static: false禁用
  52. * 即访问localhost:8080/a.js,其实访问的是public目录的a.js
  53. */
  54. // WARN 因为CopyWebpackPlugin插件会复制public的文件,所以static: false后再访问localhost:8080/a.js,其实还是能访问到public目录的a.js
  55. static: {
  56. watch: true, // 告诉 dev-server 监听文件。默认启用,文件更改将触发整个页面重新加载。可以通过将 watch 设置为 false 禁用。
  57. publicPath: outputStaticUrl(false), // 让它和输入的静态目录对应
  58. directory: resolveApp('./public/'),
  59. },
  60. proxy: {
  61. '/api': {
  62. // target: 'http://localhost:4200',
  63. target: 'http://localhost:4300',
  64. // target: 'https://live.hsslive.cn/aliyun-hk/',
  65. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  66. /**
  67. * changeOrigin,是否修改请求地址的源
  68. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  69. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  70. */
  71. changeOrigin: true,
  72. pathRewrite: {
  73. '^/api': '', // 效果:/api/link/list ==> http://localhost:4300/link/list
  74. // '^/api': '/admin/', // 效果:/api/link/list ==> http://localhost:4300/admin/link/list
  75. },
  76. },
  77. '/prodapi': {
  78. target: 'https://live.hsslive.cn',
  79. secure: false,
  80. changeOrigin: true,
  81. pathRewrite: {
  82. '^/prodapi': '/api/',
  83. },
  84. },
  85. },
  86. },
  87. module: {
  88. rules: [
  89. {
  90. test: /.(ts|tsx)$/,
  91. exclude: /node_modules/,
  92. use: [
  93. {
  94. loader: 'swc-loader',
  95. options: {
  96. jsc: {
  97. parser: {
  98. syntax: 'typescript',
  99. tsx: true,
  100. },
  101. },
  102. },
  103. },
  104. ],
  105. },
  106. {
  107. test: /.(js|jsx|mjs|cjs)$/,
  108. exclude: /node_modules/,
  109. use: [
  110. {
  111. loader: 'swc-loader',
  112. options: {
  113. jsc: {
  114. parser: {
  115. syntax: 'ecmascript',
  116. jsx: true,
  117. },
  118. },
  119. },
  120. },
  121. ],
  122. },
  123. // {
  124. // test: /\.(js|mjs|jsx|ts|tsx)$/,
  125. // exclude: /node_modules/,
  126. // use: [
  127. // {
  128. // loader: 'esbuild-loader',
  129. // options: {
  130. // loader: 'tsx', // Remove this if you're not using JSX
  131. // target: 'esnext', // Syntax to compile to (see options below for possible values)
  132. // },
  133. // },
  134. // ],
  135. // },
  136. ],
  137. },
  138. // @ts-ignore
  139. plugins: [
  140. // 终端打印调试地址
  141. new TerminalPrintPlugin(),
  142. ].filter(Boolean),
  143. optimization: {
  144. /**
  145. * 官网解释:告知 webpack 去辨识 package.json 中的 副作用 标记或规则,
  146. * 以跳过那些当导出不被使用且被标记不包含副作用的模块。'flag' 值在非生产环境默认使用。
  147. * 个人理解:flag,即如果package.json有标识就会用它的标识,
  148. * 但不意味着你的项目的package.json就得设置sideEffects,你的项目不设置,它也会对你
  149. * 项目里面用到的node_modules里面的包的package.json做检查。
  150. * 设置true的话,还会分析源代码的副作用?但测试结果貌似不会,可能我理解有问题,已经
  151. * 提了issue:https://github.com/webpack/webpack/issues/16314
  152. * 设置false的话,即不会检查package.json的sideEffects字段,把所有模块都当成有副作
  153. * 用的(即使某个包的package.json设置sideEffects为false),因为sideEffects并不
  154. * 是npm的package.json合法字段,只是写给webpack识别用的而已
  155. */
  156. // sideEffects: true,
  157. sideEffects: 'flag',
  158. },
  159. };
  160. resolve(devConfig);
  161. })
  162. .catch((error) => {
  163. console.log(error);
  164. });
  165. });