esbuild-plugin-transform-hook
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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')
      }
    }
  ],
  // optional first-level pattern, defaults to /.$/
  pattern: /^(?!.*\.html$)/, 
})
const config: BuildOptions = {
  entryPoints: ['index.ts'],
  outdir: 'target/cjs',
  plugins: [plugin],
  format: 'cjs',
}

await build(config)

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i esbuild-plugin-transform-hook

Weekly Downloads

272

Version

0.2.0

License

MIT

Unpacked Size

11.9 kB

Total Files

7

Last publish

Collaborators

  • antongolub