index.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
  8. export default defineConfig<'webpack5'>(async (merge) => {
  9. const baseConfig: UserConfigExport<'webpack5'> = {
  10. projectName: 'zsis',
  11. date: '2025-5-29',
  12. designWidth: 750,
  13. deviceRatio: {
  14. 640: 2.34 / 2,
  15. 750: 1,
  16. 375: 2,
  17. 828: 1.81 / 2,
  18. },
  19. sourceRoot: 'src',
  20. outputRoot: 'dist',
  21. plugins: ['@tarojs/plugin-http'],
  22. defineConstants: {
  23. // 开发环境下使用空字符串,实际请求会自动拼接当前域名
  24. API_BASE_URL_FROM_WEBPACK:
  25. process.env.NODE_ENV === 'development'
  26. ? '""'
  27. : '"http://101.43.219.60:7700"',
  28. MQTT_BROKER_URL_FROM_WEBPACK: '"ws://192.168.1.115:8083/mqtt"',
  29. },
  30. copy: {
  31. patterns: [],
  32. options: {},
  33. },
  34. framework: 'react',
  35. compiler: 'webpack5',
  36. cache: {
  37. enable: false, // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
  38. },
  39. mini: {
  40. postcss: {
  41. pxtransform: {
  42. enable: true,
  43. config: {},
  44. },
  45. cssModules: {
  46. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  47. config: {
  48. namingPattern: 'module', // 转换模式,取值为 global/module
  49. generateScopedName: '[name]__[local]___[hash:base64:5]',
  50. },
  51. },
  52. },
  53. webpackChain(chain) {
  54. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
  55. },
  56. },
  57. h5: {
  58. publicPath: './',
  59. staticDirectory: 'static',
  60. output: {
  61. filename: '[name].js',
  62. chunkFilename: 'js/[name].[chunkhash:8].js',
  63. },
  64. miniCssExtractPluginOption: {
  65. ignoreOrder: true,
  66. filename: 'css/[name].[fullhash].css',
  67. chunkFilename: 'css/[name].[chunkhash].css',
  68. },
  69. postcss: {
  70. autoprefixer: {
  71. enable: true,
  72. config: {},
  73. },
  74. cssModules: {
  75. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  76. config: {
  77. namingPattern: 'module', // 转换模式,取值为 global/module
  78. generateScopedName: '[name]__[local]___[fullhash:base64:5]',
  79. },
  80. },
  81. },
  82. webpackChain(chain) {
  83. chain.cache(false);
  84. chain.output.path(path.resolve(__dirname, '../dist/h5'));
  85. chain.optimization.minimize(false); // 彻底关闭压缩
  86. // chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
  87. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin, [
  88. {
  89. configFile: path.resolve(__dirname, '../tsconfig.json'), // 必须指向存在的 tsconfig
  90. },
  91. ]);
  92. // 打开详细日志
  93. //chain.mode('production').stats('verbose');
  94. chain.merge({
  95. resolve: {
  96. fallback: {
  97. util: require.resolve('util/'),
  98. stream: require.resolve('stream-browserify'),
  99. fs: require.resolve('browserify-fs'),
  100. path: require.resolve('path-browserify'),
  101. },
  102. },
  103. });
  104. chain.optimization.minimizer('terser').use(TerserPlugin, [
  105. {
  106. parallel: false, // 单线程,错误不会丢
  107. terserOptions: {
  108. compress: false, // 先不压缩,只混淆
  109. mangle: false,
  110. format: {
  111. comments: false,
  112. },
  113. },
  114. // eslint-disable-next-line
  115. } as any]);
  116. chain
  117. .plugin('define-plugin')
  118. // eslint-disable-next-line
  119. .use(DefinePlugin as any, [{
  120. 'process.env.USE_MSW': JSON.stringify(
  121. process.env.USE_MSW || 'false'
  122. ),
  123. 'process.env.NODE_ENV': JSON.stringify(
  124. process.env.NODE_ENV || 'production'
  125. ),
  126. },
  127. ]);
  128. },
  129. },
  130. rn: {
  131. appName: 'taroDemo',
  132. postcss: {
  133. cssModules: {
  134. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  135. },
  136. },
  137. },
  138. };
  139. if (process.env.NODE_ENV === 'development') {
  140. // 本地开发构建配置(不混淆压缩)
  141. return merge({}, baseConfig, devConfig);
  142. }
  143. // 生产构建配置(默认开启压缩混淆等)
  144. return merge({}, baseConfig, prodConfig);
  145. });