const pkg = require('./package')
require('dotenv').config()

// 系统环境变量 cross-env NODE_ENV=production 指定为 production 环境
let envFilename = '.env.development'
if (process.env.NODE_ENV) {
  envFilename = '.env.' + process.env.NODE_ENV
}
// 静态服务文件地址
let staticURL = 'http://static.patzn.com'
console.log('process.env.STATIC_URL==', process.env.STATIC_URL)
if (process.env.STATIC_URL) {
  // 取各个配置文件中的 STATIC_URL
  staticURL = process.env.STATIC_URL
}

module.exports = {
  mode: 'universal',
  server: {
    port: 8085,
    host: '0.0.0.0'
  },
  /*
  ** Headers of the page
  */
  head: {
    title: '实验室管理系统',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: pkg.description }
    ],
    script: [
      { src: staticURL + '/js/jquery-3.3.1.min.js' },
      {
        src:
          staticURL +
          '/thirdparty/ztree/zTree_v3-3.5.29/js/jquery.ztree.all.min.js'
      }
    ],
    link: [
      {
        rel: 'icon',
        type: 'image/x-icon',
        href: staticURL + '/favicon.ico'
      },
      {
        rel: 'stylesheet',
        href: staticURL + '/thirdparty/iview/3.4.2/iview.css'
      },
      {
        rel: 'stylesheet',
        href: staticURL + '/thirdparty/element-ui/2.6.2/index.css'
      },
      {
        rel: 'stylesheet',
        href: staticURL + '/thirdparty/bootstrap/4.3.1/bootstrap.min.css'
      },
      {
        rel: 'stylesheet',
        href:
          staticURL +
          '/thirdparty/ztree/zTree_v3-3.5.29/css/metroStyle/metroStyle.css'
      },
      {
        rel: 'stylesheet',
        href: staticURL + '/thirdparty/ztree/menuStyle.css'
      },
      {
        rel: 'stylesheet',
        href: staticURL + '/css/animate.css'
      },
      {
        rel: 'stylesheet',
        href: staticURL + '/icon/iconfont.css'
      },
      {
        rel: 'stylesheet',
        href: staticURL + '/theme/my-theme/main.css'
      }
      // { rel: 'stylesheet', href: '', name: 'theme' }
    ]
  },

  /*
  ** Customize the progress-bar color
  */
  // loading: { color: '#00a0e9' },

  /*
  ** Global CSS
  */
  css: [{ src: 'assets/css/el-common.css' }],
  /*
  ** Plugins to load before mounting the App; ssr选项设置为false，这样服务端渲染时就不会渲染这个组件了
  */
  plugins: [
    { src: '~/plugins/iview.js', ssr: true },
    { src: '~/plugins/element-ui.js', ssr: true },
    { src: '~/plugins/common.js', ssr: true },
    // { src: '~/plugins/drag.js', ssr: true },
    { src: '~/plugins/dialogDrag.js', ssr: true },
    { src: '~/plugins/global.js', ssr: true },
    { src: '~/plugins/editor.js', ssr: false },
    { src: '~/plugins/gallery.js', ssr: false },
    { src: '~/plugins/routers.js', ssr: false }
  ],

  /*
  ** Nuxt.js sub
  */
  modules: [
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    '@nuxtjs/router',
    ['@nuxtjs/dotenv', { path: './env', filename: envFilename }],
    'cookie-universal-nuxt'
  ],
  /*
  ** Axios sub configuration
  */
  axios: {
    // See https://github.com/nuxt-community/axios-module#options
  },

  /*
   * less
   */
  loader: [
    {
      test: /\.less$/,
      loaders: 'style-loader!css-loader!less-loader'
    }
  ],

  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          // https://github.com/webpack-contrib/eslint-loader#formatter-default-eslint-stylish-formatter
          options: {
            // 忽略警告
            quiet: true
          },
          exclude: /(node_modules)/
        })
      }
      // 打包编译压缩，移除 console 日志
      if (!ctx.isDev) {
        const UglifyJSWebpackPlugin = require('uglifyjs-webpack-plugin')
        config.plugins = config.plugins.filter(
          plugin => plugin.constructor.name !== 'UglifyJsPlugin'
        )
        config.plugins.push(
          new UglifyJSWebpackPlugin({
            cache: true,
            uglifyOptions: {
              compress: { drop_console: true }
            }
          })
        )
      }
    },
    vendor: [
      '~/plugins/iview.js',
      '~/plugins/element-ui.js',
      '~/plugins/vue-inject.js',
      'vue-i18n'
    ]
  }
}
