index.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. .add(path.resolve(__dirname, '../src/assets/imgs/VirtualHuman'))
  124. .end()
  125. .use('svgr')
  126. .loader('@svgr/webpack')
  127. .options({
  128. dimensions: false,
  129. icon: false,
  130. });
  131. // chain.module
  132. // .rule('svgr')
  133. // .test(/\.svg$/)
  134. // .include.add(
  135. // path.resolve(__dirname, '../src/assets/Icons')
  136. // )
  137. // .when(
  138. // true,
  139. // (rule) => {
  140. // rule
  141. // .loader('@svgr/webpack')
  142. // .options({
  143. // dimensions: false,
  144. // icon: false,
  145. // });
  146. // }
  147. // );
  148. // ⚡ 优化:只在生产环境编译 node_modules(开发环境跳过,提升 20-30% 构建速度)
  149. // 2025.12.12 这一段解决的问题是在雷神模拟器上运行android时不支持es2020语法的问题
  150. if (process.env.NODE_ENV === 'production') {
  151. chain.module
  152. .rule('force-es5')
  153. .test(/\.(js|mjs)$/)
  154. .include.add([
  155. /node_modules[\\/]ahooks/,
  156. /node_modules[\\/]lodash-es/,
  157. /node_modules[\\/]dayjs/,
  158. /node_modules[\\/]antd-mobile/,
  159. /node_modules[\\/]@ant-design/,
  160. /node_modules[\\/]@cornerstonejs/,
  161. /node_modules[\\/]react-query/,
  162. // ↑ 把自己项目里报错或输出 const/let 的包加进来就行
  163. ])
  164. .end()
  165. .use('babel-loader')
  166. .loader('babel-loader')
  167. .options({
  168. presets: [
  169. ['@babel/preset-env', {
  170. targets: { chrome: '91' }, // 或者 "iOS >= 9, Android >= 5"
  171. useBuiltIns: 'usage',
  172. corejs: 3,
  173. }]
  174. ]
  175. });
  176. }
  177. // ✅ 优化:配置 Webpack 持久化缓存
  178. chain.cache({
  179. type: 'filesystem',
  180. cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/webpack-h5'),
  181. buildDependencies: {
  182. config: [
  183. __filename,
  184. path.resolve(__dirname, './prod.ts'),
  185. path.resolve(__dirname, './dev.ts'),
  186. ],
  187. },
  188. name: `h5-${process.env.NODE_ENV || 'development'}`,
  189. version: '1.0.0',
  190. });
  191. chain.output.path(path.resolve(__dirname, '../dist/h5'));
  192. chain.optimization.minimize(false); // 彻底关闭压缩
  193. // chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
  194. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin, [
  195. {
  196. configFile: path.resolve(__dirname, '../tsconfig.json'), // 必须指向存在的 tsconfig
  197. },
  198. ]);
  199. // 打开详细日志
  200. //chain.mode('production').stats('verbose');
  201. chain.merge({
  202. resolve: {
  203. fallback: {
  204. util: require.resolve('util/'),
  205. stream: require.resolve('stream-browserify'),
  206. fs: require.resolve('browserify-fs'),
  207. path: require.resolve('path-browserify'),
  208. },
  209. },
  210. });
  211. // ✅ 优化:启用 Terser 并行处理
  212. chain.optimization.minimizer('terser').use(TerserPlugin, [
  213. {
  214. parallel: true, // 多线程并行,提升构建速度
  215. terserOptions: {
  216. compress: false, // 先不压缩,只混淆
  217. mangle: false,
  218. format: {
  219. comments: false,
  220. },
  221. },
  222. // eslint-disable-next-line
  223. } as any]);
  224. chain
  225. .plugin('define-plugin')
  226. // eslint-disable-next-line
  227. .use(DefinePlugin as any, [{
  228. 'process.env.USE_MSW': JSON.stringify(
  229. process.env.USE_MSW || 'false'
  230. ),
  231. 'process.env.NODE_ENV': JSON.stringify(
  232. process.env.NODE_ENV || 'production'
  233. ),
  234. },
  235. ]);
  236. },
  237. },
  238. rn: {
  239. appName: 'taroDemo',
  240. postcss: {
  241. cssModules: {
  242. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  243. },
  244. },
  245. },
  246. };
  247. if (process.env.NODE_ENV === 'development') {
  248. // 本地开发构建配置(不混淆压缩)
  249. return merge({}, baseConfig, devConfig);
  250. }
  251. // 生产构建配置(默认开启压缩混淆等)
  252. return merge({}, baseConfig, prodConfig);
  253. });