bundlers/esbuild-impl/preprocess-plugin
This module defines a custom esbuild plugin that preprocesses JavaScript and TypeScript files for any build target.
@remarks We cannot use . instead of (dot) in the above description because the file itself is preprocessed with the same rule.
initCjs, initEsm and the shims they call are stringified with String(fn) and injected into every built bundle as an esbuild banner, so they must be reachable from one another by BARE identifier. An unbundled build (bundle: false, one output per source) gets no banner: each emitted module gets a preamble holding only the shims its own code needs, and a module that needs none gets nothing. That is why they all live in this module: esbuild compiles a cross-module call to (0, import_module.fn)(…), which would name a binding the emitted bundle does not have. Same-module references stay bare, and makePreprocessBanner serializes the shims alongside the init function so they are in scope there. A shim that is not serialized silently degrades to whatever the consumer’s global scope holds under that name — which is how __name used to end up holding window.name.
A caller that needs more in the banner (the Obsidian plugin builder patches require) hands in PreprocessPluginOptions.additionalBannerInitFunctions. Each one is serialized as its OWN immediately invoked function after the shared scope, so it must be self-contained: it cannot see the shims, and calling one from another module would hit exactly the cross-module trap above.
Interfaces
Section titled “Interfaces”| Interface | Description |
|---|---|
| MakePreprocessBannerOptions | Options for makePreprocessBanner. |
| PreprocessPluginOptions | Options for preprocessPlugin. |
Functions
Section titled “Functions”| Function | Description |
|---|---|
| ensureBrowserProcess | Ensures the bundle runs against a process global that browser-targeting packages recognize. |
| ensureDisposeSymbols | Makes Symbol.dispose and Symbol.asyncDispose exist, falling back to the registry symbols esbuild’s using helpers look up. |
| keepName | Returns its argument unchanged. |
| makePreprocessBanner | Builds the banner injected at the top of every emitted bundle. String(fn) captures only the function’s own body, so every shim the init function calls has to be serialized next to it and wrapped in one shared scope; otherwise the call it makes resolves against the consumer’s global scope at runtime. See this file’s @file remarks. |
| preprocessPlugin | Creates an esbuild plugin that preprocesses JavaScript and TypeScript files. This plugin performs the following tasks: - Replaces instances of import(dot)meta(dot)url with a CommonJS-compatible __filename alternative. - Modifies the sourceMappingURL comment so a template literal ending a file does not swallow it. - Adds a basic process object to the global scope if process is referenced but not defined. - Fills in Symbol.dispose / Symbol.asyncDispose on engines that lack them, so using works there. |