import path from 'path'
import { defineConfig, type UserConfigExport } from '@tarojs/cli'

export default defineConfig<'webpack5'>(async (merge, { command }) => {
  const baseConfig: UserConfigExport<'webpack5'> = {
    projectName: 'wangquan-miniapp',
    date: '2026-06-16',
    designWidth: 390,
    deviceRatio: {
      390: 2,
      640: 2.34,
      750: 1,
      828: 1.81
    },
    sourceRoot: 'src',
    outputRoot: 'dist',
    plugins: ['@tarojs/plugin-framework-react'],
    defineConstants: {},
    copy: {
      patterns: [{ from: 'src/custom-tab-bar', to: 'dist/custom-tab-bar' }],
      options: {}
    },
    framework: 'react',
    compiler: 'webpack5',
    cache: {
      enable: false
    },
    alias: {
      '@': path.resolve(__dirname, '..', 'src')
    },
    sass: {
      data: '@use "@/styles/tokens.scss" as *;'
    },
    mini: {
      postcss: {
        pxtransform: {
          enable: true,
          config: {}
        },
        cssModules: {
          enable: false,
          config: {
            namingPattern: 'module',
            generateScopedName: '[name]__[local]___[hash:base64:5]'
          }
        }
      }
    },
    h5: {}
  }

  if (command === 'build') {
    return merge({}, baseConfig, (await import('./prod')).default)
  }

  return merge({}, baseConfig, (await import('./dev')).default)
})
