/** * @file Plugins - create-require * @module mkbuild/plugins/create-require/plugin */ import { type Plugin } from 'esbuild'; /** * Returns a plugin that defines the `require` function in ESM bundles. * * When outputting ESM, any `require` calls will be replaced with esbuild's * `__require` shim, since [`require` is not defined in ESM environments][1]. * * The shim first checks if `require` is polyfilled. If it is, the shim passes * the call to `require.apply`. If `require` isn't defined, an error will be * thrown: `Dynamic require of "' + x + '" is not supported`. * * This is particularly problematic when bundling, especially when targeting * node ([`platform === 'node'`][2]), since [built-in modules][3] are marked * as [`external`][4] by esbuild automatically. `require`ing these modules, as * well as other externals, will generate a runtime error. This is described * in [`evanw/esbuild#1921`][5]. * * To circumvent this issue, output files containing the shim will have a * snippet insert that defines `require` using [`module.createRequire`][6]. * * [1]: https://nodejs.org/api/esm.html#no-require-exports-or-moduleexports * [2]: https://esbuild.github.io/api/#platform * [3]: https://nodejs.org/api/esm.html#builtin-modules * [4]: https://esbuild.github.io/api/#external * [5]: https://github.com/evanw/esbuild/issues/1921 * [6]: https://nodejs.org/api/module.html#modulecreaterequirefilename * * @return {Plugin} `create-require` plugin */ declare const plugin: () => Plugin; export { plugin as default };