scripts/build/esbuild-plugins/replace-module.mjs/checkPathReplaced
function processReplacements(replacements) {
  const onResolveReplacements = new Map();
  const onLoadReplacements = new Map();
  const onLoadProcessors = new Map();

  const checkPathReplaced = (module) => {
    if (!onResolveReplacements.has(module)) {
      return;
    }

    throw new Error(`module: '${module}' already replaced with another path.`);
  };

  const checkTextReplaced = (module) => {
    if (!onLoadReplacements.has(module)) {
      return;
    }

    throw new Error(
      `module: '${module}' already replaced with another content.`
    );
  };

  for (const replacement of replacements) {
    const { module } = replacement;

    if (typeof module !== "string") {
      throw new TypeError("'module' option is required.");
    }

    if (
      Reflect.has(replacement, "external") ||
      Reflect.has(replacement, "path")
    ) {
      if (module === "*") {
        throw new Error("Can not replace all modules with the same path.");
      }

      checkPathReplaced(module);

      onResolveReplacements.set(
        module,
        Reflect.has(replacement, "external")
          ? { external: true, path: replacement.external }
          : { path: replacement.path }
      );

      continue;
    }

    if (Reflect.has(replacement, "text")) {
      if (module === "*") {
        throw new Error("Can not replace all modules with the same content.");
      }

      checkPathReplaced(module);
      checkTextReplaced(module);

      onLoadReplacements.set(module, {
        contents: replacement.text,
        loader: replacement.loader,
      });

      continue;
    }

    checkPathReplaced(module);
    checkTextReplaced(module);

    if (!onLoadProcessors.has(module)) {
      onLoadProcessors.set(module, []);
    }

    const processFunctions = onLoadProcessors.get(module);

    if (Reflect.has(replacement, "process")) {
      const { process } = replacement;
      if (typeof process !== "function") {
        throw new TypeError("'process' option should be a function.");
      }

      processFunctions.push(process);
      continue;
    }

    if (
      Reflect.has(replacement, "find") &&
      Reflect.has(replacement, "replacement")
    ) {
      processFunctions.push((text) =>
        text.replaceAll(replacement.find, replacement.replacement)
      );
      continue;
    }

    console.log(replacement);
    throw new Error("Unexpected replacement option.");
  }

  return { onResolveReplacements, onLoadReplacements, onLoadProcessors };
}