const { defineConfig } = require('@vue/cli-service')
const webpack = require('webpack')
const path = require('path')
const fs = require('fs')
const global = require('./src/config/global')
const resolve = dir => {
  return path.join(__dirname, dir)
}
//const BASE_URL = process.env.NODE_ENV === 'production' ? '/' : '/'  //从 Vue CLI 3.3 起已弃用，请使用publicPath。
const PUBLIC_PATH = process.env.NODE_ENV === 'production' ? './' : '/'

console.log('当前的环境是',process.env.VUE_APP_TITLE,process.env.NODE_ENV,process.env.VUE_APP_ENV)
console.log(global)
let deployAddress = process.env.npm_config_deployAddress;
if(deployAddress){
  fs.writeFile('./public/config/deploy.param.js', `
    module.exports = {
      DEPLOYMENT_AREA: '${deployAddress}'
    }
  `,  function(err) {
    if (err) {
      return console.error(err);
    }
  });
}

//后端统一网关地址
const SERVER_URL = global.SERVER_URL
module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: PUBLIC_PATH,
  // 如果你不需要使用eslint，把lintOnSave设为false即可
  lintOnSave: false,
  css:{
    loaderOptions:{
      less:{
        javascriptEnabled:true
      }
    }
  },
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
      .set('_c', resolve('src/components'))
  },
  // 设为false打包时不生成.map文件
  productionSourceMap: false,
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        jQuery:'jquery',
        $:'jquery',
      })
    ],
  },
  assetsDir: 'assets',
  outputDir: 'static',

   // 这里写你调用接口的基础路径，来解决跨域，如果设置了代理，那你本地开发环境的axios的baseUrl要写为 '' ，即空字符串
  //一般情况下此前端项目请求都是cas的
  devServer: {
    port: 91, // 端口号
    host: '0.0.0.0',
    https: false, // https:{type:Boolean}
    open: false, //配置自动启动浏览器
    // proxy: 'http://localhost:4000' // 配置跨域处理,只有一个代理
    proxy: {
      '/bcDigitalAuth/': {
        target: global.GETWAY_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/bcDigitalAuth': '/bcDigitalAuth'
        }
      },
      '/api/': {
        target: SERVER_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/api'
        }
      },
      '/oauth': {
        target: SERVER_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/bochu-print': '/',
          '^/': '/'
        }
      },
    },
  }
})
