const path = require('path');

const { DefinePlugin } = require('webpack');

function resolve(dir) {
  return path.join(__dirname, dir);
}

const { defineConfig } = require('@vue/cli-service');
const isDev = process.env.NODE_ENV !== 'production';
console.log('isdev', isDev);
process.env.IDE_ENV = 'web';
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    host: '0.0.0.0',
    // host: 'localhost',
    proxy: {
      // 低代码组件服务地址
      '/phoenix': {
        target: 'http://192.168.193.1:15186',
        // target: "http://192.168.193.134:2012",
      },
      '/ecas': {
        target: 'http://192.168.193.1:15186',
        // target: "http://192.168.193.134:2012",
      },
      '/dev': {
        target: 'http://192.168.193.1:15186',
        // target: 'http://192.168.193.134:2012',
        pathRewrite: { '^/dev': '' },
        changeOrigin: true,
      },
      '/sdp': {
        target: 'http://192.168.193.134:9078',
        changeOrigin: true,
      },
      '/sdp-demo': {
        target: 'http://192.168.193.134:9078',
        changeOrigin: true,
      },
    },
    port: 9527,
    client: {
      overlay: false,
    },
  },
  css: {
    loaderOptions: {
      sass: {
        additionalData: `@import './src/global.scss';`,
      },
    },
  },
  publicPath: process.env.NODE_ENV === 'development' ? '/' : '././',
  lintOnSave: false,
  // 自定义webpack配置
  configureWebpack: {
    resolve: {
      extensions: ['.js', '.vue', '.json', '.ts', '.scss', '.css'],
      alias: {
        _c: resolve('src/components'),
      },
    },
  },
  chainWebpack: (config) => {
    // // 添加login 模块

    // 配置sourcemap
    const needSourcemap = isDev ? 'eval-source-map' : false;

    config.devtool(needSourcemap);
    // 设置根目录别名
    config.resolve.alias.set('@$', resolve('src'));

    // webpack5 不在支持node环境api，需要设置回退
    config.resolve.set('fallback', {
      // path 设置false仍会报错，此处引入path-browserify, 同时，devDependency 中也要引入
      path: require.resolve('path-browserify'),
      assert: false,
      constants: false,
      crypto: false,
    });

    config.plugin('define').use(DefinePlugin, [
      {
        'process.env.IDE_ENV': JSON.stringify(process.env.IDE_ENV),
        'process.env.COMPATIBLE_MODE': JSON.stringify('none'),
        'process.env.INTEGRATED_EDOC': JSON.stringify('none'),
        DEBUG: process.env.NODE_ENV === 'development',
        __DEV__: true,
      },
    ]);
  },
  pluginOptions: {
    mock: {
      // 默认mock配置文件路径为项目中mock/api.js
      // 开始mock，默认为true
      power: true,
    },
  },
});
