const path = require('path')

// 拼接路径
function resolve (dir) {
  return path.join(__dirname, dir)
}

// 基础路径 注意发布之前要先修改这里
let baseUrl = ''
// let baseUrl = '/ses-enrol-v1/enrol/'
// 演示项目自动构建使用
if (process.env.VUE_APP_TRAVIS === 'TRUE') baseUrl = '/ses-enrol-v1/enrol/'

module.exports = {
  baseUrl: baseUrl, // 根据你的实际情况更改这里
  lintOnSave: true,
  // 是否为生产环境构建生成 source map？
  productionSourceMap: true,
  parallel: require('os').cpus().length > 1,
  devServer: {
    publicPath: baseUrl, // 和 baseUrl 保持一致
    proxy:{
      // http://127.0.0.1:8091/
      '/ses-enrol-v1/':{
        target: 'http://127.0.0.1:8091/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      '/enrol-upload/':{
        target: 'http://127.0.0.1:8091/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      '/std/':{
        target: 'http://127.0.0.1:8091/ses-enrol-v1/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      '/pl/':{
        target: 'http://127.0.0.1:8091/ses-enrol-v1/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      '/pay/':{
        target: 'http://127.0.0.1:8091/ses-enrol-v1/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      '/by/':{
        target: 'http://127.0.0.1:8091/ses-enrol-v1/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      '/mk/':{
        target: 'http://127.0.0.1:8091/ses-enrol-v1/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      '/kj/':{
        target: 'http://127.0.0.1:8091/ses-enrol-v1/',
        changeOrigin: true, 
        onProxyRes(proxyRes, req, res) {
          var cookies = proxyRes.headers['set-cookie']
          if (cookies == null || cookies.length == 0) {
              delete proxyRes.headers['set-cookie']
              return
          }
          for (var i = 0,n = cookies.length; i < n; i++) {
              if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
                  cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
              }
          }

          proxyRes.headers['set-cookie'] = cookies;
        }
      },
      // '/sima/':{
      //   target: 'http://ses.hmsoft.cn:83/ses-upload/',
      //   changeOrigin: true, 
      //   onProxyRes(proxyRes, req, res) {
      //     var cookies = proxyRes.headers['set-cookie']
      //     if (cookies == null || cookies.length == 0) {
      //         delete proxyRes.headers['set-cookie']
      //         return
      //     }
      //     for (var i = 0,n = cookies.length; i < n; i++) {
      //         if(cookies[i].match(/^JSESSIONID=[^;]+;[\s\S]*Path=\/[^;]+/)){
      //             cookies[i] = cookies[i].replace(/Path=\/[^;]+/,'Path=/');
      //         }
      //     }

      //     proxyRes.headers['set-cookie'] = cookies;
      //   }
      // },
    }      
  },
  // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
  chainWebpack: config => {
    // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
    config.resolve
      .symlinks(true)
    // markdown
    config.module
      .rule('md')
      .test(/\.md$/)
      .use('text-loader')
      .loader('text-loader')
      .end()
    // i18n
    config.module
      .rule('i18n')
      .resourceQuery(/blockType=i18n/)
      .use('i18n')
      .loader('@kazupon/vue-i18n-loader')
      .end()
    // svg
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .include
      .add(resolve('src/assets/svg-icons/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'd2-[name]'
      })
      .end()
    // image exclude
    const imagesRule = config.module.rule('images')
    imagesRule
      .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
      .exclude
      .add(resolve('src/assets/svg-icons/icons'))
      .end()
    // 重新设置 alias
    config.resolve.alias
      .set('@', resolve('src'))
    // babel-polyfill 加入 entry
    const entry = config.entry('app')
    entry
      .add('babel-polyfill')
      .end()
  }
}
