const path = require('path')
const childProcess = require('child_process')

const versions
  = childProcess.execSync('git rev-parse --abbrev-ref HEAD', {
    encoding: 'utf8',
  }) != 'HEAD\n'
    ? childProcess.execSync('git rev-parse --abbrev-ref HEAD', {
      encoding: 'utf8',
    })
    : childProcess.execSync('git describe --tags --abbrev=0', {
      encoding: 'utf8',
    })
const CURRENT_GITHASH = childProcess.execSync('git rev-parse --short HEAD', {
  encoding: 'utf8',
})
const CURRENT_VERSION = `Version: ${JSON.stringify(process.env.CODE_BRANCH || versions)} ${CURRENT_GITHASH} ${new Date().toLocaleString()}`.replace(
  /\"|\\n/g,
  '',
)

const config = {
  appid: 'wx64fe67f111d52457', // 测试/体验环境
  projectName: 'SpiderSteward',
  date: '2022-4-6',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2,
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  defineConstants: {
    CURRENT_VERSION: JSON.stringify(CURRENT_VERSION),
    CURRENT_GITHASH: JSON.stringify(CURRENT_GITHASH),
    CURRENT_ENV: JSON.stringify(process.env.NODE_ENV),
  },
  copy: {
    patterns: [],
    options: {},
  },
  framework: 'react',
  mini: {
    postcss: {
      pxtransform: {
        enable: true,
        config: {
          onePxTransform: false,
        },
      },
      url: {
        enable: true,
        config: {
          limit: 1024, // 设定转换尺寸上限
        },
      },
      cssModules: {
        enable: true, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
  },
  h5: {
    router: {
      mode: 'hash',
    },
    // 设置代理服务器转发
    //   devServer: {
    //     proxy: {
    //       '/mp/': {
    //         target: Domain,
    //         pathRewrite: {
    //           '^/mp/': '' // 所以带有/api/请求的链接一律替换为空并追加域名请求
    //         },
    //         changeOrigin: true
    //       }
    //     }
    //   }
    // },
    output: {
      filename: 'js/[name].[hash].js',
      chunkFilename: 'js/[name].[chunkhash].js',
    },
    imageUrlLoaderOption: {
      limit: 5000,
      name: 'static/images/[name].[hash].[ext]',
    },
    miniCssExtractPluginOption: {
      filename: 'css/[name].[hash].css',
      chunkFilename: 'css/[name].[chunkhash].css',
    },
    devServer: {
      https: true,
    },
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {},
      },
      cssModules: {
        enable: true, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
  },
  compiler: {
    type: 'webpack5',
    // 依赖预编译配置
    prebundle: {
      enable: true,
    },
  },
  // 持久化缓存配置
  cache: {
    enable: true,
  },
  plugins: [['@tarojs/plugin-framework-react', { reactMode: 'concurrent' }]],
}

module.exports = function(merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  if (process.env.NODE_ENV === 'pre') {
    return merge({}, config, require('./pre'))
  }
  return merge({}, config, require('./prod'))
}
