Registry / devops / esbuild-plugin-transform-hook

esbuild-plugin-transform-hook

JSON →
library0.2.3jsnpmunverified

Esbuild plugin (v0.2.3, 2024) that allows applying custom transformation hooks to build outputs or dependencies, not just sources. Solves gaps in esbuild's native support for polyfill injection, dynamic banners, and importable helpers. Compared to alternatives (e.g., onEnd hooks or manual post-processing), it provides a declarative hook-based API with pattern matching and rename support. Requires esbuild >=0.19.0. Ships TypeScript types. Note: still in PoC status; API may change.

npm install esbuild-plugin-transform-hook
INSTALL
IMPORT
SIG · ESBUILD-PLUGIN-TRA
E
esbuild-plugin-transform-hook
devopsjavascriptv0.2.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transformHookPlugin
✓ import { transformHookPlugin } from 'esbuild-plugin-transform-hook'
✗ const { transformHookPlugin } = require('esbuild-plugin-transform-hook')
ESM-only; CJS require() will not work if package is ESM-only.
default
✓ import transformHookPlugin from 'esbuild-plugin-transform-hook'
✗ const transformHookPlugin = require('esbuild-plugin-transform-hook')
Default export also available; prefer named import for clarity.
TransformHookOptions
✓ import type { TransformHookOptions } from 'esbuild-plugin-transform-hook'
✗ import { TransformHookOptions } from 'esbuild-plugin-transform-hook'
This is a TypeScript type; use `import type` to avoid runtime issues.

Shows how to use the plugin to replace console.log with console.error in TypeScript files during the load phase.

import { build } from 'esbuild' import { transformHookPlugin } from 'esbuild-plugin-transform-hook' const plugin = transformHookPlugin({ hooks: [ { on: 'load', pattern: /\.ts$/, transform: (source) => { return source.replace(/console\.log/g, 'console.error') }, rename: (path) => { return path.replace(/\.ts$/, '.js') } } ], pattern: /^(?!.*\.html$)/, }) await build({ entryPoints: ['index.ts'], outdir: 'dist', plugins: [plugin], format: 'esm', }) console.log('Build complete')
Debug
Known issues
gotchaThe plugin is marked as PoC (Proof of Concept). API may change in future versions without major semver bump.
fix
Pin to exact version in package.json and test upgrades carefully.
affects: >=0.0.0
breakingRequires esbuild >=0.19.0. Older esbuild versions (pre-0.19) are not supported and will cause runtime errors.
fix
Upgrade esbuild to >=0.19.0.
affects: >=0.2.0
deprecatedThe 'on' property currently only accepts 'load' or 'end'. Other values may be silently ignored or cause errors in future versions.
fix
Stick to 'load' or 'end' until documentation states otherwise.
affects: <=0.2.3
Errors
Common errors & fixes
Error: Cannot find module 'esbuild-plugin-transform-hook'
Package not installed or used with require() in an ESM-only environment.
fix
Install with `npm install esbuild-plugin-transform-hook` and use `import` instead of `require`.
TypeError: transformHookPlugin is not a function
Importing the wrong export (e.g., default vs named) or using a bundler that doesn't support ESM.
fix
Use `import { transformHookPlugin } from 'esbuild-plugin-transform-hook'` and ensure your project is configured for ESM.
error: [plugin] Error: esbuild version must be >=0.19.0
Installed esbuild version is too old (<0.19.0).
fix
Run `npm install esbuild@latest` to upgrade esbuild to >=0.19.0.
Upgrade
Version history
0.2.3latest on npm
Audit
Dependencies
esbuildrequiredpeer dependency: plugin works as an esbuild plugin and requires esbuild >=0.19.0
Agent activity
4 hits · last 30 days
node
4
Resources
esbuild-plugin-transform-hook — npm install esbuild-plugin-transform-hook · libregistry