console.log("cycccccccc")
console.log(process.env.BUILD_DIR) 
console.log("cycccccccc")


//
var targetServer =  "https://xinsiketang.com"

console.log("hellolllllllooooo")
console.log(process.env.NODE_ENV)
// process.exit(0); // 0 表示正常退出，非0表示异常退出

var publicPath = 'https://smile-ebook-online.xinsiketang.com/reading/'
publicPath = "./"

console.log(publicPath)
// process.exit(0); // 0 表示正常退出，非0表示异常退出

console.log(process.env.NODE_ENV)
// process.exit(0);
if(process.env.NODE_ENV === 'production'){
  targetServer = "https://xinsiketang.com"
}else{
  targetServer = "https://dev.xinsiketang.com"
}
//  targetServer =  "https://xinsiketang.com"
console.log("targetServer",targetServer)


module.exports = {
  lintOnSave: false,
  publicPath: publicPath, // 设置公共路径为相对路径
  // publicPath: "https://smile-ebook-online.xinsiketang.com/reading/",
  outputDir:process.env.BUILD_DIR? process.env.BUILD_DIR :"dist",
  devServer: {
    // headers: {
    //   'Access-Control-Allow-Origin': '*',
    //   'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
    //   'Access-Control-Allow-Headers': 'Content-Type, Authorization',
    // },
    // allowedHosts: "all",
    proxy: {
      '/api/app_dev.php': {
        target: targetServer,
        changeOrigin: true,
        pathRewrite: {
          '^/api/app_dev.php': '/app_dev.php'
        },
        logLevel: 'debug', // 用于调试时查看代理请求详情
        onProxyRes(proxyRes, req, res) {
          let responseBody = '';
          proxyRes.on('data', (chunk) => {
            responseBody += chunk;
          });
          proxyRes.on('end', () => {
            // console.log('Proxy response body:', responseBody);
          });
          console.log('Proxy response:', res.statusCode);
        }
      },
      '/api': {
        target: targetServer,
        changeOrigin: true,
        // pathRewrite: {
        //   '^/api': '/api'
        // },
        logLevel: 'debug', // 用于调试时查看代理请求详情
        onProxyRes(proxyRes, req, res) {
          let responseBody = '';
          proxyRes.on('data', (chunk) => {
            responseBody += chunk;
          });
          proxyRes.on('end', () => {
            // console.log('Proxy response body:', responseBody);
          });
          console.log('Proxy response:', res.statusCode);
        }
      },
  
      '/baiduAi': {
        target: 'https://qianfan.baidubce.com',
        changeOrigin: true,
        pathRewrite: {
          '^/baiduAi': '/'
        },
        logLevel: 'debug', // 用于调试时查看代理请求详情
        onProxyRes(proxyRes, req, res) {
          let responseBody = '';
          proxyRes.on('data', (chunk) => {
            responseBody += chunk;
          });
          proxyRes.on('end', () => {
            // console.log('Proxy response body:', responseBody);
          });
          // console.log('Proxy response:', res.statusCode);
        }
      },
      '/remote':{
        target : "https://smile-ebook.oss-cn-qingdao.aliyuncs.com",
        changeOrigin : true,
           pathRewrite: {
          '^/remote': '/'
        },
        logLevel : 'debug',
        onProxyRes(proxyRes, req, res) {
          let responseBody = '';
          proxyRes.on('data', (chunk) => {
            responseBody += chunk;
          });
          proxyRes.on('end', () => {
            // console.log('Proxy response body:', responseBody);
          });
          console.log('Proxy response:', res.statusCode);
        }
      }

    }
  },

  configureWebpack:(config)=>{
    // config.devtool = 'source-map'; 
    // config.stats = 'errors-warings'
    // config.plugins.push(
    //   new webpack.ProgressPlugin({
    //     handler:() => {},
    //   })
    // )
  },
  chainWebpack: config => {
    // 细粒度的 Webpack 配置修改
    config.module
    .rule('js')
    .test(/\.m?jsx?$/)
    .exclude
    .clear()  // 清除默认的 exclude 条件
    .end()
    .include
    .add(/node_modules\/mind-elixir/)  // 包含 mind-elixir
    .add(/src\/plugin\/xml-digital-teaching\/lib/)  // 包含本地文件夹
    .end()
    .use('babel-loader')
    .loader('babel-loader')
    .tap(options => {
      // 如果需要，可以在此处修改 Babel 配置
      return options;
    });
    config.plugin('html').tap((args) => {
      args[0].title = 'Your Custom Title';
      return args;
    });
  }
}
