let proxy = {
  "/api": {
    target: process.env.VUE_APP_CORE_HOST_URL,
    changeOrigin: true,
    ws: true,
  },
};

const fs = require("fs");
if (fs.existsSync("./proxy.config.js")) {
  // 项目根目录下,创建 proxy.config.js,内容如下。把需要代理到本地服务器的api添加到下面。
  // exports.default = {
  //   "/api/test": {
  //     target: "http://localhost:8001",
  //     changeOrigin: true,
  //     ws: true,
  //   },
  // };
  const localProxy = require("./proxy.config.js").default;
  proxy = { ...localProxy, ...proxy };
  console.log(proxy);
}

// const mock = [{ source: "/api/mock/exam_question", dest: "/examQuestions" }];

// for (const m of mock) {
//   proxy[m.source] = {
//     target: "http://localhost:3000/",
//     changeOrigin: true,
//     pathRewrite: {
//       ".*": m.dest
//     }
//   };
// }

var webpack = require("webpack");

// const plugins = [];
// Ignore all locale files of moment.js
// TODO: use webpack stats to check if iview locale matters
// plugins.push();
// if (process.env.NODE_ENV === "production") {
//   plugins.push("transform-remove-console");
// }

module.exports = {
  lintOnSave: process.env.NODE_ENV !== "production" ? true : "error",
  publicPath: process.env.VUE_APP_PUBLIC_PATH,
  devServer: {
    proxy,
  },
  chainWebpack: (config) => {
    // iview Loader
    config.module
      .rule("vue")
      .test(/\.vue$/)
      .use("iview-loader")
      .loader("iview-loader")
      .options({
        prefix: true,
      })
      .end();
  },
  configureWebpack: {
    devtool: "source-map",
    plugins: [new webpack.IgnorePlugin(/^\.\/locale$/, /moment|iview$/)],
  },
  pwa: {
    workboxPluginMode: "GenerateSW",
    workboxOptions: {
      importWorkboxFrom: "local",
      // navigateFallback: "index.html",
      skipWaiting: true,
      clientsClaim: true,
      runtimeCaching: [
        {
          // 背景图如果要换,就改地址,减少网络消耗
          urlPattern: new RegExp(
            "^https://cdn.qmth.com.cn/ui/ecs-client-bg.jpg!/progressive/true"
          ),
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
          },
        },
        {
          // Match any same-origin request that contains 'api'.
          // 产品名称有可能每分钟都更新
          urlPattern: /\/api\/ecs_core\/org\/propertyNoSession\/OE_STUDENT_SYS_NAME\?domainName=/,
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
            cacheName: "sys-name-cache",
            expiration: {
              maxAgeSeconds: 60,
            },
          },
        },
        {
          // logo 地址会变
          urlPattern: new RegExp(
            "^https://ecs(-test)?-static.qmth.com.cn/org_logo/.*/.*"
          ),
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
          },
        },
        {
          // 作答文件的地址
          urlPattern: new RegExp(
            "^https://ecs(-test)?-static.qmth.com.cn/oe-answer-file/.*"
          ),
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
            cacheName: "oe-answer-file",
            expiration: {
              maxAgeSeconds: 4 * 60 * 60,
            },
          },
        },
        {
          urlPattern: /\/models\/.*\/.*\.json/,
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
          },
        },
        {
          // 客观分10分钟更新一次
          urlPattern: /\/api\/ecs_oe_student\/examScore\/queryObjectiveScoreList\?examStudentId=/,
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
            cacheName: "objective-score-list-cache",
            expiration: {
              maxAgeSeconds: 1 * 60,
            },
          },
        },
        {
          // APP是否下载1分钟更新一次
          urlPattern: /\/api\/ecs_core\/org\/property\/\d+\/APP_ENABLED/,
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
            cacheName: "app-alllow-download-cache",
            expiration: {
              maxAgeSeconds: 1 * 60,
            },
          },
        },
        {
          // APP下载地址10分钟更新一次
          urlPattern: /\/api\/ecs_core\/systemProperty\/APP_DOWNLOAD_URL/,
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
            cacheName: "app-download-url-cache",
            expiration: {
              maxAgeSeconds: 2 * 60,
            },
          },
        },
        {
          // 站内消息3分钟获取一次
          urlPattern: /\/api\/ecs_exam_work\/notice\/getUserNoticeList\?/,
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
            cacheName: "site-messages-list-cache",
            expiration: {
              maxAgeSeconds: 3 * 60,
            },
          },
        },
        {
          // 菜单3分钟获取一次
          urlPattern: /\/api\/ecs_core\/rolePrivilege\/getStudentClientMenu\?rootOrgId=/,
          handler: "cacheFirst",
          options: {
            cacheableResponse: {
              statuses: [0, 200],
            },
            cacheName: "menus-cache",
            expiration: {
              maxAgeSeconds: 3 * 60,
            },
          },
        },
      ],
    },
  },
};