index.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. publicPath: './',
  75. staticDirectory: 'static',
  76. output: {
  77. filename: '[name].js',
  78. chunkFilename: '[name].[chunkhash:8].js',
  79. },
  80. miniCssExtractPluginOption: {
  81. ignoreOrder: true,
  82. filename: 'css/[name].[fullhash].css',
  83. chunkFilename: 'css/[name].[chunkhash].css',
  84. },
  85. postcss: {
  86. autoprefixer: {
  87. enable: true,
  88. config: {},
  89. },
  90. cssModules: {
  91. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  92. config: {
  93. namingPattern: 'module', // 转换模式,取值为 global/module
  94. generateScopedName: '[name]__[local]___[fullhash:base64:5]',
  95. },
  96. },
  97. },
  98. webpackChain(chain) {
  99. // ✅ 优化:配置 Webpack 持久化缓存
  100. chain.cache({
  101. type: 'filesystem',
  102. cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/webpack-h5'),
  103. buildDependencies: {
  104. config: [
  105. __filename,
  106. path.resolve(__dirname, './prod.ts'),
  107. path.resolve(__dirname, './dev.ts'),
  108. ],
  109. },
  110. name: `h5-${process.env.NODE_ENV || 'development'}`,
  111. version: '1.0.0',
  112. });
  113. chain.output.path(path.resolve(__dirname, '../dist/h5'));
  114. chain.optimization.minimize(false); // 彻底关闭压缩
  115. // chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
  116. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin, [
  117. {
  118. configFile: path.resolve(__dirname, '../tsconfig.json'), // 必须指向存在的 tsconfig
  119. },
  120. ]);
  121. // 打开详细日志
  122. //chain.mode('production').stats('verbose');
  123. chain.merge({
  124. resolve: {
  125. fallback: {
  126. util: require.resolve('util/'),
  127. stream: require.resolve('stream-browserify'),
  128. fs: require.resolve('browserify-fs'),
  129. path: require.resolve('path-browserify'),
  130. },
  131. },
  132. });
  133. // ✅ 优化:启用 Terser 并行处理
  134. chain.optimization.minimizer('terser').use(TerserPlugin, [
  135. {
  136. parallel: true, // 多线程并行,提升构建速度
  137. terserOptions: {
  138. compress: false, // 先不压缩,只混淆
  139. mangle: false,
  140. format: {
  141. comments: false,
  142. },
  143. },
  144. // eslint-disable-next-line
  145. } as any]);
  146. chain
  147. .plugin('define-plugin')
  148. // eslint-disable-next-line
  149. .use(DefinePlugin as any, [{
  150. 'process.env.USE_MSW': JSON.stringify(
  151. process.env.USE_MSW || 'false'
  152. ),
  153. 'process.env.NODE_ENV': JSON.stringify(
  154. process.env.NODE_ENV || 'production'
  155. ),
  156. },
  157. ]);
  158. },
  159. },
  160. rn: {
  161. appName: 'taroDemo',
  162. postcss: {
  163. cssModules: {
  164. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  165. },
  166. },
  167. },
  168. };
  169. if (process.env.NODE_ENV === 'development') {
  170. // 本地开发构建配置(不混淆压缩)
  171. return merge({}, baseConfig, devConfig);
  172. }
  173. // 生产构建配置(默认开启压缩混淆等)
  174. return merge({}, baseConfig, prodConfig);
  175. });