index.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // 2025.12.12 这一段解决的问题是在雷神模拟器上运行android时不支持es2020语法的问题
  100. webpackChain(chain) {
  101. chain.module
  102. .rule('force-es5')
  103. .test(/\.(js|mjs)$/)
  104. .include.add([
  105. /node_modules[\\/]ahooks/,
  106. /node_modules[\\/]lodash-es/,
  107. /node_modules[\\/]dayjs/,
  108. /node_modules[\\/]antd-mobile/,
  109. /node_modules[\\/]@ant-design/,
  110. /node_modules[\\/]@cornerstonejs/,
  111. /node_modules[\\/]react-query/,
  112. // ↑ 把自己项目里报错或输出 const/let 的包加进来就行
  113. ])
  114. .end()
  115. .use('babel-loader')
  116. .loader('babel-loader')
  117. .options({
  118. presets: [
  119. ['@babel/preset-env', {
  120. targets: { chrome: '91' }, // 或者 "iOS >= 9, Android >= 5"
  121. useBuiltIns: 'usage',
  122. corejs: 3,
  123. }]
  124. ]
  125. });
  126. // ✅ 优化:配置 Webpack 持久化缓存
  127. chain.cache({
  128. type: 'filesystem',
  129. cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/webpack-h5'),
  130. buildDependencies: {
  131. config: [
  132. __filename,
  133. path.resolve(__dirname, './prod.ts'),
  134. path.resolve(__dirname, './dev.ts'),
  135. ],
  136. },
  137. name: `h5-${process.env.NODE_ENV || 'development'}`,
  138. version: '1.0.0',
  139. });
  140. chain.output.path(path.resolve(__dirname, '../dist/h5'));
  141. chain.optimization.minimize(false); // 彻底关闭压缩
  142. // chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
  143. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin, [
  144. {
  145. configFile: path.resolve(__dirname, '../tsconfig.json'), // 必须指向存在的 tsconfig
  146. },
  147. ]);
  148. // 打开详细日志
  149. //chain.mode('production').stats('verbose');
  150. chain.merge({
  151. resolve: {
  152. fallback: {
  153. util: require.resolve('util/'),
  154. stream: require.resolve('stream-browserify'),
  155. fs: require.resolve('browserify-fs'),
  156. path: require.resolve('path-browserify'),
  157. },
  158. },
  159. });
  160. // ✅ 优化:启用 Terser 并行处理
  161. chain.optimization.minimizer('terser').use(TerserPlugin, [
  162. {
  163. parallel: true, // 多线程并行,提升构建速度
  164. terserOptions: {
  165. compress: false, // 先不压缩,只混淆
  166. mangle: false,
  167. format: {
  168. comments: false,
  169. },
  170. },
  171. // eslint-disable-next-line
  172. } as any]);
  173. chain
  174. .plugin('define-plugin')
  175. // eslint-disable-next-line
  176. .use(DefinePlugin as any, [{
  177. 'process.env.USE_MSW': JSON.stringify(
  178. process.env.USE_MSW || 'false'
  179. ),
  180. 'process.env.NODE_ENV': JSON.stringify(
  181. process.env.NODE_ENV || 'production'
  182. ),
  183. },
  184. ]);
  185. },
  186. },
  187. rn: {
  188. appName: 'taroDemo',
  189. postcss: {
  190. cssModules: {
  191. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  192. },
  193. },
  194. },
  195. };
  196. if (process.env.NODE_ENV === 'development') {
  197. // 本地开发构建配置(不混淆压缩)
  198. return merge({}, baseConfig, devConfig);
  199. }
  200. // 生产构建配置(默认开启压缩混淆等)
  201. return merge({}, baseConfig, prodConfig);
  202. });