const HtmlWebpackPlugin = require('html-webpack-plugin')
const AWSDevParams = require('./AWSDevParams');
const AWSReleasePath = AWSDevParams.AWSReleasePath;
const AWSPortalUrl = AWSDevParams.AWSPortalUrl;
//开发时通过用户名及密码获取sessionId
const devUserInfo =AWSDevParams.devUserInfo;
const AWSproxyPath = "/AWSDEVURL/r/"; //防止跨域的地址

const moduleTemplateInAWS = AWSDevParams.moduleTemplateInAWS;//平台的模板路径
const outputDir = AWSDevParams.outputDir;
const publicPath = AWSDevParams.publicPath;//决定生成在平台的js或css的相对路径,与build配置的路径要对应

//判断是否开发
const isproduction = process.env.NODE_ENV === 'production';
//开发时路径和build到平台路径
let AWSJSAndCSSPath = isproduction ? "../" : AWSproxyPath.replace("r/","");
let AWSJSAndCSSImport = AWSDevParams.AWSJSAndCSSImport == null ? [] : AWSDevParams.AWSJSAndCSSImport;
let AWSJSAndCSSImportArray = [];
for(let k of AWSJSAndCSSImport){
  if(k.type == "css"){
    AWSJSAndCSSImportArray.push("<link type='text/css' rel='stylesheet' href='"+AWSJSAndCSSPath + k.path +"'/>");
  }else if(k.type == "js"){
    AWSJSAndCSSImportArray.push("<script type='text/javascript' src='"+AWSJSAndCSSPath + k.path + "'></script>");
  }
}

const getAWSFileRelativePath = ()=>{
  let index = "./";
  if(isproduction){
    index = publicPath;
  }
  return index;
}
const getIndexPath = ()=>{
  let index = "index.html";
  if(isproduction){
    index = AWSReleasePath+moduleTemplateInAWS;
  }
  return index;
}
module.exports = {
  // 基本路径 build后文件路径../apps/_bpm.platform/test2 ，开发运行时文件路径./
  publicPath: getAWSFileRelativePath(),

  //生成入口的html文件位置
  indexPath: getIndexPath(),

  outputDir : AWSReleasePath+outputDir,

  // eslint-loader 是否在保存的时候检查
  lintOnSave: true,

  productionSourceMap: false, //打包不使用源码（false后在平台无法调试）
  // use the full build with in-browser compiler?
  // https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
  // compiler: false,
  // webpack配置
  // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  chainWebpack: (config) => {
    config.plugin('html')
        .tap(args => {
          let buildVar = args[0];
          buildVar.AWSJSAndCSSImport = AWSJSAndCSSImportArray.join("\n");
          buildVar.AWSJSAndCSSPath = AWSJSAndCSSPath;
          buildVar.devUserInfo = devUserInfo;
          buildVar.isproduction = isproduction;
          buildVar.axiosBaseUrl = isproduction ? "./" : AWSproxyPath;
          buildVar.settingParam = isproduction ? "<#settingParam>" : "{}";
          return args;
        });
  },

  configureWebpack: config => {
    config.entry.app=["babel-polyfill","./src/main.js"];
    if (isproduction) {
      // 为生产环境修改配置...
      config.mode = 'production'
    } else {
      // 为开发环境修改配置...
      config.mode = 'development'
    }
  },

  devServer: {
    proxy: {
      //配置跨域
      "/AWSDEVURL": {
        target: AWSPortalUrl,
        ws: true,
        changOrigin: true, //允许跨域
        pathRewrite: {
          "^/AWSDEVURL": "" //请求的时候使用这个URL就可以
        }
      }
    }
  },
  // css相关配置
  css: {
    loaderOptions: {}
  },

  // 是否启用dll
  // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#dll-mode
  // dll: false,
  // PWA 插件相关配置
  // see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
  pwa: {},

  // webpack-dev-server 相关配置
  //  devServer: {
  //    open: process.platform === 'darwin',
  //    disableHostCheck: true,
  //    host: 'www.test.com',//如果是真机测试，就使用这个IP
  //    port: 1234,
  //    https: false,
  //    hotOnly: false,
  //   before: app => {}
  //  },
  
  // 第三方插件配置
  pluginOptions: {
  },
  transpileDependencies:  ["*"]
}
