const path = require("path");
const LodashModuleReplacementPlugin = require("lodash-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin"); // 代码压缩

module.exports = {
  publicPath: "/bims",
  productionSourceMap: false,
  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: [path.resolve(__dirname, "src/assets/style/common.less")],
    },
  },
  css: {
    loaderOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
  chainWebpack: (config) => {
    if (process.env.NODE_ENV === "production") {
      // lodash 按需加载
      config.plugin("loadshReplace").use(new LodashModuleReplacementPlugin());
    }
  },
  configureWebpack: (config) => {
    const plugins = [];
    if (process.env.NODE_ENV === "production") {
      plugins.push(
        new CompressionPlugin({
          filename: "[path][base].gz",
          algorithm: "gzip",
          test: new RegExp(".(" + ["js", "css", "json"].join("|") + ")$"),
          threshold: 8192,
          include: /\/src/,
          exclude: /node_modules/,
          minRatio: 0.8,
          deleteOriginalAssets: false, // 此处源文件保留吧，以防不测
        })
      );
    }
    config.plugins = [...config.plugins, ...plugins];
  },

  devServer: {
    disableHostCheck: true,
    useLocalIp: true,
    port: 8888,
    hot: true,
    proxy: {
      "^/api_bims": {
        target: "http://47.99.75.3:8070",
        pathRewrite: {
          "^/api_bims": "/",
        },
        changeOrigin: true,
        secure: false,
      },
    },
  },
};
