const WebpackAliOSSPlugin = require('webpack-oss');
const time = WebpackAliOSSPlugin.getFormat('YYMMDD');
const path = require('path');

const BASE_URL = '/live/';
const globalEnv = require('./config/global.js');
const modeEnv = process.env.mode ? require(`./config/${process.env.mode}.js`) : {};
const env = { ...globalEnv, ...modeEnv };
const { X_CA_STAGE, DEF_OSS, sentryOptions } = env;
const BUILD_DIST = X_CA_STAGE || 'PROD';

export default {
  buildDir: 'nuxt-dist',
  env: {
    baseUrl: BASE_URL,
    ...env
  },
  router: {
    base: BASE_URL,
    middleware: ['device']
  },
  mode: 'universal',
  parallel: true,
  cache: true,
  terser: {
    parallel: true,
    cache: true,
    sourceMap: false,
    extractComments: {
      filename: 'LICENSES'
    }
  },
  render: {
    compressor: false,
    http2: {
      push: true
    }
  },
  /*
   ** Headers of the page
   */
  head: {
    title: process.env.npm_package_name || '',
    meta: [
      { charset: 'utf-8' },
      { 'http-equiv': 'Cache-control', content: 'no-store no-cache' },
      {
        name: 'viewport',
        content:
          'width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no'
      },
      { hid: 'description', name: 'description', content: '现场直播' }
    ],
    link: [{ rel: 'stylesheet', href: `${DEF_OSS}/static/css/reset.css` }],
    script: [
      { src: '//cdn.aodianyun.com/static/jquery/jquery-1.11.1.min.js' },
      { src: '//cdn.aodianyun.com/mps/v1/hlsplayer.js' },
      { src: '//cdn.aodianyun.com/dms/rop_client.js' },
      { src: '//res.wx.qq.com/open/js/jweixin-1.0.0.js' },
      { src: `${DEF_OSS}/static/js/web-animations.min.js` }
    ]
  },
  serverMiddleware: ['@/serverMiddleware/router'],
  /*
   ** Customize the progress-bar color
   */
  loading: { color: '#fff' },
  /*
   ** Global CSS
   */
  css: [],
  /*
   ** Plugins to load before mounting the App
   */
  plugins: ['@/plugins/main.js'],
  /*
   ** Nuxt.js dev-modules
   */
  buildModules: ['@nuxtjs/router'],
  /*
   ** Nuxt.js modules
   */
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/sentry',
    [
      '@nuxtjs/component-cache',
      {
        max: 10000,
        maxAge: 1000 * 60 * 5
      }
    ]
  ],
  sentry: {
    dsn: sentryOptions.dsn,
    config: {}
  },
  server: {
    port: 3000, // default: 3000
    host: '0.0.0.0' // default: localhost
  },
  /*
   ** Build configuration
   */
  build: {
    /*
     ** You can extend webpack config here
     */
    // publicPath: `/${BUILD_DIST.toLowerCase()}/`,
    publicPath: process.env.run_server
      ? `//static-${process.env.run_server}.guangdianyun.tv/live/${time}`
      : `/${BUILD_DIST.toLowerCase()}/`,
    extend(config, ctx) {
      // console.log(ctx);
    },
    analyze: false,
    maxChunkSize: 300000,
    plugins:
      process.env.NODE_ENV === 'production' && process.env.run_server
        ? [
            new WebpackAliOSSPlugin({
              accessKeyId: 'LTAIIgVna8MHelpI',
              accessKeySecret: 'fqQNjEGxww3ZZJ9GbUfNp2yzsWUcnl',
              region: 'oss-cn-hangzhou',
              bucket: `guangdianyun-static-${process.env.run_server}`,
              prefix: 'live',
              limit: 100, // 备份最近 3 个版本的 oss 文件
              format: time,
              exclude: [/.*\.html$/], // 或者 /.*\.html$/,排除.html文件的上传
              deleteAll: false, // 优先匹配 format 配置项
              output: path.resolve(__dirname, './nuxt-dist/dist/client'),
              local: true // 上传打包输出目录里的文件
            })
          ]
        : []
  }
};
