index.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { defineConfig, type UserConfigExport } from '@tarojs/cli';
  2. import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
  3. import devConfig from './dev';
  4. import prodConfig from './prod';
  5. import path from 'path';
  6. import TerserPlugin from 'terser-webpack-plugin';
  7. import { DefinePlugin } from 'webpack';
  8. // https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
  9. export default defineConfig<'webpack5'>(async (merge) => {
  10. const baseConfig: UserConfigExport<'webpack5'> = {
  11. projectName: 'zsis',
  12. date: '2025-5-29',
  13. designWidth: 750,
  14. deviceRatio: {
  15. 640: 2.34 / 2,
  16. 750: 1,
  17. 375: 2,
  18. 828: 1.81 / 2,
  19. },
  20. sourceRoot: 'src',
  21. outputRoot: 'dist',
  22. plugins: ['@tarojs/plugin-http'],
  23. defineConstants: {
  24. // 开发环境下使用空字符串,实际请求会自动拼接当前域名
  25. API_BASE_URL_FROM_WEBPACK:
  26. process.env.NODE_ENV === 'development'
  27. ? '""'
  28. : (`"${process.env.TARO_API_URL}"`),
  29. MQTT_BROKER_URL_FROM_WEBPACK: `"${process.env.TARO_MQTT_URL}"`,
  30. },
  31. copy: {
  32. patterns: [],
  33. options: {},
  34. },
  35. framework: 'react',
  36. compiler: 'webpack5',
  37. // ✅ 优化:启用 Webpack 持久化缓存(在 webpackChain 中配置)
  38. cache: {
  39. enable: true, // Taro 层面启用缓存
  40. },
  41. mini: {
  42. postcss: {
  43. pxtransform: {
  44. enable: true,
  45. config: {},
  46. },
  47. cssModules: {
  48. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  49. config: {
  50. namingPattern: 'module', // 转换模式,取值为 global/module
  51. generateScopedName: '[name]__[local]___[hash:base64:5]',
  52. },
  53. },
  54. },
  55. webpackChain(chain) {
  56. // ✅ 优化:配置 Webpack 持久化缓存
  57. chain.cache({
  58. type: 'filesystem',
  59. cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/webpack-mini'),
  60. buildDependencies: {
  61. config: [
  62. __filename,
  63. path.resolve(__dirname, './prod.ts'),
  64. path.resolve(__dirname, './dev.ts'),
  65. ],
  66. },
  67. name: `mini-${process.env.NODE_ENV || 'development'}`,
  68. version: '1.0.0',
  69. });
  70. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
  71. },
  72. },
  73. h5: {
  74. esnextModules: [],
  75. publicPath: './',
  76. staticDirectory: 'static',
  77. output: {
  78. filename: '[name].js',
  79. chunkFilename: '[name].[chunkhash:8].js',
  80. },
  81. miniCssExtractPluginOption: {
  82. ignoreOrder: true,
  83. filename: 'css/[name].[fullhash].css',
  84. chunkFilename: 'css/[name].[chunkhash].css',
  85. },
  86. postcss: {
  87. autoprefixer: {
  88. enable: true,
  89. config: {},
  90. },
  91. cssModules: {
  92. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  93. config: {
  94. namingPattern: 'module', // 转换模式,取值为 global/module
  95. generateScopedName: '[name]__[local]___[fullhash:base64:5]',
  96. },
  97. },
  98. },
  99. webpackChain(chain) {
  100. /* 1️⃣ 排除 svg(防止被 base64)这是为了把svg解析为组件 ,用于svg icon*/
  101. if (chain.module.rules.has('image')) {
  102. chain.module
  103. .rule('image')
  104. .exclude.add(/\.svg$/)
  105. }
  106. if (chain.module.rules.has('media')) {
  107. chain.module
  108. .rule('media')
  109. .exclude.add(/\.svg$/)
  110. }
  111. if (chain.module.rules.has('asset')) {
  112. chain.module
  113. .rule('asset')
  114. .exclude.add(/\.svg$/)
  115. }
  116. chain.module
  117. .rule('svgr-icons')
  118. .before('image')
  119. .test(/\.svg$/)
  120. .include.add(
  121. path.resolve(__dirname, '../src/assets/Icons')
  122. )
  123. .end()
  124. .use('svgr')
  125. .loader('@svgr/webpack')
  126. .options({
  127. dimensions: false,
  128. icon: false,
  129. });
  130. // chain.module
  131. // .rule('svgr')
  132. // .test(/\.svg$/)
  133. // .include.add(
  134. // path.resolve(__dirname, '../src/assets/Icons')
  135. // )
  136. // .when(
  137. // true,
  138. // (rule) => {
  139. // rule
  140. // .loader('@svgr/webpack')
  141. // .options({
  142. // dimensions: false,
  143. // icon: false,
  144. // });
  145. // }
  146. // );
  147. // 2025.12.12 这一段解决的问题是在雷神模拟器上运行android时不支持es2020语法的问题
  148. chain.module
  149. .rule('force-es5')
  150. .test(/\.(js|mjs)$/)
  151. .include.add([
  152. /node_modules[\\/]ahooks/,
  153. /node_modules[\\/]lodash-es/,
  154. /node_modules[\\/]dayjs/,
  155. /node_modules[\\/]antd-mobile/,
  156. /node_modules[\\/]@ant-design/,
  157. /node_modules[\\/]@cornerstonejs/,
  158. /node_modules[\\/]react-query/,
  159. // ↑ 把自己项目里报错或输出 const/let 的包加进来就行
  160. ])
  161. .end()
  162. .use('babel-loader')
  163. .loader('babel-loader')
  164. .options({
  165. presets: [
  166. ['@babel/preset-env', {
  167. targets: { chrome: '91' }, // 或者 "iOS >= 9, Android >= 5"
  168. useBuiltIns: 'usage',
  169. corejs: 3,
  170. }]
  171. ]
  172. });
  173. // ✅ 优化:配置 Webpack 持久化缓存
  174. chain.cache({
  175. type: 'filesystem',
  176. cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/webpack-h5'),
  177. buildDependencies: {
  178. config: [
  179. __filename,
  180. path.resolve(__dirname, './prod.ts'),
  181. path.resolve(__dirname, './dev.ts'),
  182. ],
  183. },
  184. name: `h5-${process.env.NODE_ENV || 'development'}`,
  185. version: '1.0.0',
  186. });
  187. chain.output.path(path.resolve(__dirname, '../dist/h5'));
  188. chain.optimization.minimize(false); // 彻底关闭压缩
  189. // chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
  190. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin, [
  191. {
  192. configFile: path.resolve(__dirname, '../tsconfig.json'), // 必须指向存在的 tsconfig
  193. },
  194. ]);
  195. // 打开详细日志
  196. //chain.mode('production').stats('verbose');
  197. chain.merge({
  198. resolve: {
  199. fallback: {
  200. util: require.resolve('util/'),
  201. stream: require.resolve('stream-browserify'),
  202. fs: require.resolve('browserify-fs'),
  203. path: require.resolve('path-browserify'),
  204. },
  205. },
  206. });
  207. // ✅ 优化:启用 Terser 并行处理
  208. chain.optimization.minimizer('terser').use(TerserPlugin, [
  209. {
  210. parallel: true, // 多线程并行,提升构建速度
  211. terserOptions: {
  212. compress: false, // 先不压缩,只混淆
  213. mangle: false,
  214. format: {
  215. comments: false,
  216. },
  217. },
  218. // eslint-disable-next-line
  219. } as any]);
  220. chain
  221. .plugin('define-plugin')
  222. // eslint-disable-next-line
  223. .use(DefinePlugin as any, [{
  224. 'process.env.USE_MSW': JSON.stringify(
  225. process.env.USE_MSW || 'false'
  226. ),
  227. 'process.env.NODE_ENV': JSON.stringify(
  228. process.env.NODE_ENV || 'production'
  229. ),
  230. },
  231. ]);
  232. },
  233. },
  234. rn: {
  235. appName: 'taroDemo',
  236. postcss: {
  237. cssModules: {
  238. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  239. },
  240. },
  241. },
  242. };
  243. if (process.env.NODE_ENV === 'development') {
  244. // 本地开发构建配置(不混淆压缩)
  245. return merge({}, baseConfig, devConfig);
  246. }
  247. // 生产构建配置(默认开启压缩混淆等)
  248. return merge({}, baseConfig, prodConfig);
  249. });