webpack.dev.ts 9.1 KB

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