index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: true // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
  47. },
  48. mini: {
  49. postcss: {
  50. pxtransform: {
  51. enable: true,
  52. config: {
  53. }
  54. },
  55. cssModules: {
  56. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  57. config: {
  58. namingPattern: 'module', // 转换模式,取值为 global/module
  59. generateScopedName: '[name]__[local]___[hash:base64:5]'
  60. }
  61. }
  62. },
  63. webpackChain(chain) {
  64. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin)
  65. chain.merge({
  66. plugin: {
  67. install: {
  68. plugin: UnifiedWebpackPluginV5,
  69. args: [{
  70. // 这里可以传参数
  71. cssSelectorReplacement:{
  72. universal: ['view','text','button', 'input']
  73. },
  74. cssChildCombinatorReplaceValue: ['view', 'text', 'button', 'input']
  75. }]
  76. }
  77. }
  78. })
  79. }
  80. },
  81. h5: {
  82. publicPath: '/mini/',
  83. staticDirectory: 'static',
  84. output: {
  85. filename: 'js/[name].[hash:8].js',
  86. chunkFilename: 'js/[name].[chunkhash:8].js'
  87. },
  88. miniCssExtractPluginOption: {
  89. ignoreOrder: true,
  90. filename: 'css/[name].[hash].css',
  91. chunkFilename: 'css/[name].[chunkhash].css'
  92. },
  93. postcss: {
  94. autoprefixer: {
  95. enable: true,
  96. config: {}
  97. },
  98. cssModules: {
  99. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  100. config: {
  101. namingPattern: 'module', // 转换模式,取值为 global/module
  102. generateScopedName: '[name]__[local]___[hash:base64:5]'
  103. }
  104. }
  105. },
  106. webpackChain(chain) {
  107. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin)
  108. },
  109. router: {
  110. basename: '/mini',
  111. },
  112. },
  113. rn: {
  114. appName: 'taroDemo',
  115. postcss: {
  116. cssModules: {
  117. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  118. }
  119. }
  120. }
  121. }
  122. process.env.BROWSERSLIST_ENV = process.env.NODE_ENV
  123. if (process.env.NODE_ENV === 'development') {
  124. // 本地开发构建配置(不混淆压缩)
  125. return merge({}, baseConfig, devConfig)
  126. }
  127. // 生产构建配置(默认开启压缩混淆等)
  128. return merge({}, baseConfig, prodConfig)
  129. })