function umd(moduleName) {
  return {
    name: 'umd',
    setup(build) {
      // We _must_ disable the write option so that `result.outputFiles` is set.
      // Node API defaults to false.
      const originalWrite = build.initialOptions.write ?? true;
      build.initialOptions.write = false;

      if (build.initialOptions.globalName) {
        throw new Error('Using the umd plugin requires not setting `globalName`');
      }
      build.initialOptions.globalName = 'umdExports';

      if (build.initialOptions.format) {
        throw new Error('Using the umd plugin requires not setting `format`');
      }
      build.initialOptions.format = 'iife';

      build.onEnd(async (result) => {
        if (result.outputFiles?.length !== 1) {
          throw new Error('unexpected number of output files');
        }

        const umdCode = generateUMD(result.outputFiles[0].text, moduleName);
        // @ts-expect-error build-viewer needs to extract the umd bundle as a string.
        result.outputFiles[0].textUmd = umdCode;
        if (originalWrite) {
          await fs.promises.writeFile(result.outputFiles[0].path, umdCode);
        }
      });
    },
  };
}
@param {string} moduleName
@return {esbuild.Plugin}