const { version } = require('./package.json');
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 { DEF_S_AODIANYUN, DEF_OSS, X_CA_STAGE, sentryOptions } = env;
const BUILD_DIST = X_CA_STAGE || 'PROD';

const flag = process.env.NODE_ENV === 'production' && !env.private
const gtagConfig = flag
  ? {
      'google-gtag': {
        id: env.GTAG_ID,
        config: {
          anonymize_ip: true, // anonymize IP
          send_page_view: false // might be necessary to avoid duplicated page track on page reload
        },
        // linker:{
        //   domains:[] // 跨域追踪，追踪两个相关但不同网域的页面
        // },
        debug: true, // enable to track in dev mode
        disableAutoPageTrack: false, // disable if you don't want to track each page route with router.afterEach(...).
        additionalAccounts: []
      }
    }
  : {}
  
export default {
  buildDir: 'nuxt-dist',
  env: {
    baseUrl: BASE_URL,
    ...env
  },
  router: {
    base: BASE_URL,
    middleware: ['device']
  },
  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: `${DEF_S_AODIANYUN}/static/jquery/jquery-1.11.1.min.js` },
      { src: `${DEF_OSS}/common/js/define.js` },
      { src: `${DEF_S_AODIANYUN}/mps/v1/hlsplayer.js` },
      { src: `${DEF_S_AODIANYUN}/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/google-gtag',
    [
      '@nuxtjs/component-cache',
      {
        max: 10000,
        maxAge: 1000 * 60 * 5
      }
    ]
  ],
  ...gtagConfig,
  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/${version}`
      : `/${BUILD_DIST.toLowerCase()}/`,
    extend(config, { isClient }) {
      // 为 客户端打包 进行扩展配置
      // if (isClient) {
      //   config.devtool = 'eval-source-map';
      // }
    },
    analyze: false,
    maxChunkSize: 300000,
    plugins: []
  }
};
