index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }]
  72. }
  73. }
  74. })
  75. }
  76. },
  77. h5: {
  78. publicPath: '/mini/',
  79. staticDirectory: 'static',
  80. output: {
  81. filename: 'js/[name].[hash:8].js',
  82. chunkFilename: 'js/[name].[chunkhash:8].js'
  83. },
  84. miniCssExtractPluginOption: {
  85. ignoreOrder: true,
  86. filename: 'css/[name].[hash].css',
  87. chunkFilename: 'css/[name].[chunkhash].css'
  88. },
  89. postcss: {
  90. autoprefixer: {
  91. enable: true,
  92. config: {}
  93. },
  94. cssModules: {
  95. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  96. config: {
  97. namingPattern: 'module', // 转换模式,取值为 global/module
  98. generateScopedName: '[name]__[local]___[hash:base64:5]'
  99. }
  100. }
  101. },
  102. webpackChain(chain) {
  103. chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin)
  104. },
  105. router: {
  106. basename: '/mini',
  107. },
  108. },
  109. rn: {
  110. appName: 'taroDemo',
  111. postcss: {
  112. cssModules: {
  113. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  114. }
  115. }
  116. }
  117. }
  118. process.env.BROWSERSLIST_ENV = process.env.NODE_ENV
  119. if (process.env.NODE_ENV === 'development') {
  120. // 本地开发构建配置(不混淆压缩)
  121. return merge({}, baseConfig, devConfig)
  122. }
  123. // 生产构建配置(默认开启压缩混淆等)
  124. return merge({}, baseConfig, prodConfig)
  125. })