esbuild-plugin-transform-hook

v0.1.1
Esbuild plugin to apply custom transformation hooks

esbuild-plugin-transform-hook

Esbuild plugin to apply custom transformation hooks

lcov npm

Status

PoC

Problem

Esbuild and its plugins mostly focus on sources processing, but sometimes additional modifications is also necessary for dependencies or bundles:

These features will be provided sooner or later, but for now we need a workaround to apply custom transforms.

Usage

import { build, BuildOptions } from 'esbuild'
import { transformHookPlugin } from 'esbuild-plugin-transform-hook'

const plugin = transformHookPlugin({
  hooks: [
    {
      on: 'load', // or 'end'
      pattern: /\.ts$/,
      transform: (source) => {
        return source.replace(/console\.log/g, 'console.error')
      },
      rename: (path) => {
        return path.replace(/\.ts$/, '.js')
      }
    }
  ]
})
const config: BuildOptions = {
  entryPoints: ['index.ts'],
  outdir: 'target/cjs',
  plugins: [plugin],
  format: 'cjs'
}

await build(config)

License

MIT

npm i esbuild-plugin-transform-hook

Metadata

  • MIT
  • Whatever
  • Anton Golub
  • released 5/26/2024

Downloads

Maintainers