import path from "path";
const WebpackObfuscator = require("webpack-obfuscator");

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) {
      // ✅ 避免 undefined.js，统一使用标准占位符
      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",
        },
      ]);

      if (process.env.NODE_ENV === "production") {
        // ✅ 保留原有混淆逻辑
        chain.plugin("obfuscator").use(WebpackObfuscator, [
          {
            rotateStringArray: true,
            stringArray: true,
            stringArrayThreshold: 0.5,
            compact: true,
            controlFlowFlattening: false,
            deadCodeInjection: false,
            debugProtection: false,
            disableConsoleOutput: true,
            selfDefending: false,
            simplify: true,
            target: "browser",
            disableOptimizations: true,
          },
          ["**/*.js", "!node_modules/**"],
        ]);
      }

      // ✅ markdown 支持
      chain.module
        .rule("markdown")
        .test(/\.md$/)
        .use("raw-loader")
        .loader("raw-loader")
        .end();

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

    esnextModules: ["taro-ui"],
    publicPath: "/", // ✅ 部署到根目录；如果是子路径，需要改成 /xxx/
    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: {
      proxy: {
        "/api/": {
          target: "http://localhost:8700",
          pathRewrite: { "^/api/": "/" },
          changeOrigin: true,
        },
      },
      allowedHosts: ["nomadro.com", "localhost", ".nomadro.com", "hackrobot.cn"],
    },

    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"));
};
