import path from "path";

const isDev = process.env.NODE_ENV === "development";

const config = {
  projectName: "cityh5",
  date: "2023-12-2",
  designWidth: 750,
  deviceRatio: { 640: 2.34 / 2, 750: 1, 828: 1.81 / 2 },
  sourceRoot: "src",
  outputRoot: "dist",
  plugins: [],
  defineConstants: {},
  copy: { patterns: [], options: {} },
  framework: "react",
  compiler: "webpack5",

  cache: {
    enable: true,
    type: "filesystem",
  },

  h5: {
    webpackChain(chain) {
      if (isDev) {
        chain.output.filename("static/js/[name].js");
        chain.output.chunkFilename("static/js/[name].js");
        chain.devtool("eval-cheap-module-source-map");
      } else {
        chain.output.filename("static/js/[name].[contenthash:8].js");
        chain.output.chunkFilename("static/js/[name].[contenthash:8].js");

        chain.plugin("MiniCssExtractPlugin").use(require("mini-css-extract-plugin"), [
          {
            filename: "static/css/[name].[contenthash:8].css",
            chunkFilename: "static/css/[name].[contenthash:8].css",
          },
        ]);
      }

      chain.module
        .rule("markdown")
        .test(/\.md$/)
        .use("md-chunk-loader")
        .loader(path.resolve(__dirname, "..", "src", "loaders", "md-chunk-loader.js"))
        .end();

      chain.module
        .rule("ico")
        .test(/\.ico$/)
        .use("file-loader")
        .loader("file-loader")
        .options({ name: "static/images/[name].[ext]" });
    },

    esnextModules: ["taro-ui"],
    publicPath: "/",
    staticDirectory: "static",

    postcss: {
      autoprefixer: { enable: true, config: {} },
      cssModules: {
        enable: false,
        config: {
          namingPattern: "module",
          generateScopedName: "[name]__[local]___[hash:base64:5]",
        },
      },
    },

    router: {
      mode: "browser",
      customRoutes: {
        "/pages/index/index": "/",
        "/pages/book/index": "/book",
      },
    },

    devServer: {
      hot: true,
      proxy: {
        "/api/": {
          target: "http://localhost:8700",
          pathRewrite: { "^/api/": "/" },
          changeOrigin: true,
        },
      },
      allowedHosts: ["nomadro.com", "localhost", ".nomadro.com", "hackrobot.cn", ".ngrok-free.app"],
    },

    htmlPluginOption: {
      favicon: path.resolve(__dirname, "..", "src", "images", "dgnomad.png"),
      meta: {
        "cache-control": "no-cache, no-store, must-revalidate",
        pragma: "no-cache",
        expires: "0",
      },
    },

    target: ["web", "browserslist:> 0.25%, not dead"],
  },
};

module.exports = function (merge) {
  if (process.env.NODE_ENV === "development") {
    return merge({}, config, require("./dev"));
  }
  return merge({}, config, require("./prod"));
};
