index.ts 9.3 KB

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