index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { defineConfig, type UserConfigExport } from '@tarojs/cli'
  2. import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'
  3. import { UnifiedWebpackPluginV5 } from 'weapp-tailwindcss/webpack'
  4. import devConfig from './dev'
  5. import prodConfig from './prod'
  6. // 获取当前编译平台(默认微信小程序)
  7. const getPlatform = () => {
  8. // 从环境变量获取编译平台,Taro 会自动设置TARO_ENV 环境变量
  9. return process.env.TARO_ENV || 'weapp'
  10. }
  11. // 获取当前环境(开发/生产)
  12. const getEnv = () => {
  13. return process.env.NODE_ENV || 'development'
  14. }
  15. export default defineConfig<'webpack5'>(async (merge, { command, mode }) => {
  16. // 动态生成输出目录:dist/平台/环境
  17. const platform = getPlatform()
  18. const env = getEnv()
  19. const outputDir = `dist/${platform}/${env}`
  20. const baseConfig: UserConfigExport<'webpack5'> = {
  21. projectName: 'mini',
  22. date: '2025-7-27',
  23. designWidth: 750,
  24. deviceRatio: {
  25. 640: 2.34 / 2,
  26. 750: 1,
  27. 375: 2,
  28. 828: 1.81 / 2
  29. },
  30. sourceRoot: 'src',
  31. outputRoot: outputDir, // 使用动态生成的输出目录
  32. plugins: [
  33. "@tarojs/plugin-generator"
  34. ],
  35. defineConstants: {
  36. },
  37. copy: {
  38. patterns: [
  39. ],
  40. options: {
  41. }
  42. },
  43. framework: 'react',
  44. compiler: 'webpack5',
  45. cache: {
  46. enable: false // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
  47. },
  48. mini: {
  49. miniCssExtractPluginOption: {
  50. ignoreOrder: true,
  51. },
  52. postcss: {
  53. pxtransform: {
  54. enable: true,
  55. config: {
  56. }
  57. },
  58. cssModules: {
  59. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  60. config: {
  61. namingPattern: 'module', // 转换模式,取值为 global/module
  62. generateScopedName: '[name]__[local]___[hash:base64:5]'
  63. }
  64. }
  65. },
  66. webpackChain(chain) {
  67. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin)
  68. chain.merge({
  69. plugin: {
  70. install: {
  71. plugin: UnifiedWebpackPluginV5,
  72. args: [{
  73. // 这里可以传参数
  74. cssSelectorReplacement:{
  75. universal: ['view','text','button', 'input']
  76. },
  77. cssChildCombinatorReplaceValue: ['view', 'text', 'button', 'input']
  78. }]
  79. }
  80. }
  81. })
  82. }
  83. },
  84. h5: {
  85. publicPath: '/mini/',
  86. staticDirectory: 'static',
  87. output: {
  88. filename: 'js/[name].[hash:8].js',
  89. chunkFilename: 'js/[name].[chunkhash:8].js'
  90. },
  91. miniCssExtractPluginOption: {
  92. ignoreOrder: true,
  93. filename: 'css/[name].[hash].css',
  94. chunkFilename: 'css/[name].[chunkhash].css'
  95. },
  96. postcss: {
  97. pxtransform: {
  98. enable: true,
  99. config: {
  100. baseFontSize : 14,
  101. minRootSize: 14
  102. }
  103. },
  104. autoprefixer: {
  105. enable: true,
  106. config: {}
  107. },
  108. cssModules: {
  109. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  110. config: {
  111. namingPattern: 'module', // 转换模式,取值为 global/module
  112. generateScopedName: '[name]__[local]___[hash:base64:5]'
  113. }
  114. }
  115. },
  116. webpackChain(chain) {
  117. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin)
  118. },
  119. router: {
  120. basename: '/mini',
  121. },
  122. },
  123. rn: {
  124. appName: 'taroDemo',
  125. postcss: {
  126. cssModules: {
  127. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  128. }
  129. }
  130. }
  131. }
  132. process.env.BROWSERSLIST_ENV = process.env.NODE_ENV
  133. if (process.env.NODE_ENV === 'development') {
  134. // 本地开发构建配置(不混淆压缩)
  135. return merge({}, baseConfig, devConfig)
  136. }
  137. // 生产构建配置(默认开启压缩混淆等)
  138. return merge({}, baseConfig, prodConfig)
  139. })