index.ts 4.7 KB

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