scripts/build/esbuild-plugins/replace-module.mjs/setupOnResolveListener
function setupOnResolveListener(build, { concepts, replacements }) {
  if (replacements.size === 0) {
    return;
  }

  // `build.resolve()` will call `onResolve` listener
  // Use a `Set` to avoid infinite loop
  const seenModules = new Set();
  build.onResolve(concepts, async (args) => {
    if (!(args.kind === "require-call" || args.kind === "import-statement")) {
      return;
    }

    const key = JSON.stringify(args);
    if (seenModules.has(key)) {
      return;
    }
    seenModules.add(key);

    const resolveResult = await build.resolve(args.path, {
      importer: args.importer,
      namespace: args.namespace,
      resolveDir: args.resolveDir,
      kind: args.kind,
      pluginData: args.pluginData,
    });

    // `build.resolve()` seems not respecting `browser` field in `package.json`,
    // `resolveResult` maybe not correct, return `undefined` to let esbuild process the file.
    return replacements.get(resolveResult.path);
  });
}