const { merge } = require('webpack-merge')
const path = require('path')
const moment = require('moment')
const { defineConfig } = require('@vue/cli-service')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const tsImportPluginFactory = require('ts-import-plugin')

const convertPath = (dir) => path.resolve(__dirname, dir)
const outputDir = convertPath('app/dist') // 打包输出目录

module.exports = defineConfig({
  transpileDependencies: true,
  productionSourceMap: false, // 打包取消.map
  publicPath: './',
  outputDir,
  pages: {
    indexPath: {
      entry: process.env.VUE_APP_ROOT + 'main.ts',
      template: process.env.VUE_APP_ROOT + 'index.html',
      filename: 'index.html',
      title: process.env.VUE_APP_TITLE,
      meta: {
        revised: moment(new Date()).format('YYYYMMDDHHmmss')  // 打包生成版本号
      }
    }
  },
  devServer: {
    client: {
      overlay: false
    }
  },
  css: {
    extract: true, // 是否使用css分离插件 ExtractTextPlugin
    sourceMap: false,
    loaderOptions: {
      postcss: {
        postcssOptions: {
          config: process.env.VUE_APP_ROOT, // 自定义 postcss.config.js 文件路径，https://webpack.docschina.org/loaders/postcss-loader/#postcss-options
        }
      }
    }
  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@public', convertPath('public/'))
      .set('@pc', convertPath('src/packages/pc/'))
      .set('@mobile', convertPath('src/packages/mobile/'))

    config.module
      .rule('ts')
      .use('ts-loader')
      .tap((options) => {
        options = merge(options, {
          transpileOnly: true,
          getCustomTransformers: () => ({
            before: [
              // vant 按需引入
              tsImportPluginFactory({
                libraryName: 'vant',
                libraryDirectory: 'es',
                style: true
              })
            ],
            compilerOptions: {
              module: 'es2015'
            }
          })
        })
        return options;
      })

    config.plugin('compressionPlugin')
      .use(new CompressionPlugin({
        test: /\.js$|\.html$|\.css/, // 匹配文件名
        threshold: 10240, // 对超过10k的数据压缩
      }))

    config.plugin('fork-ts-checker')
      .use(new ForkTsCheckerWebpackPlugin({
        typescript: {
          memoryLimit: 4096,
        },
      }))
  },
  configureWebpack: (config) => {
    config.devtool = 'source-map'
  }
})
