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

// const stu = [
//   "/api/ecs_oe",
//   "/api/ecs_oe_student/",
//   "/api/sys_param",
//   "/api/exam_record",
//   "/api/exam_control",
//   "/api/exam_question",
//   "/api/exam_score",
//   "/api/practice_course",
//   "/api/practice_detail",
//   "/api/practice_record",
//   "/api/exam_captures",
//   "/api/face_capture",
//   "/api/face_verify",
//   "/api/offline_exam"
// ];

// for (const s of stu) {
//   proxy[s] = {
//     target: "http://ecs-dev.qmth.com.cn:8003", // 陈恳
//     changeOrigin: true
//   };
// }

// 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: "/oe-web/",
  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,
            },
          },
        },
      ],
    },
  },
};
