esbuild-plugin-hybrid-export

Esbuild plugin to simplify hybrid (aka dual) packages forging

MIT 12 个版本
安装
npm install esbuild-plugin-hybrid-export
yarn add esbuild-plugin-hybrid-export
pnpm add esbuild-plugin-hybrid-export
bun add esbuild-plugin-hybrid-export
README

esbuild-plugin-hybrid-export

Esbuild plugin to simplify hybrid (aka dual) packages forging

lcov npm

Status

PoC

Problem

Hybrid package is a quite specific approach when, for some reason, you may need to support both modern (esm) and legacy (cjs) architectures. Node.js brings a portion of internal magic for this:

⚠️ When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static analysis as a convenience for better ecosystem compatibility. https://nodejs.org/api/esm.html#import-statements

Unfortunately, this mechanism does not provide absolute reliability. That's why, the general approach for this case is double bundling, when size is not critical. Otherwise, a little optimization fixes it easily:

index.cjs (generated)

const foo = 'bar'
module.exports = {
  foo
}

index.mjs

const {foo} = require('./index.cjs')
export {foo}

This plugin just handles the mentioned routine.

Usage

import { build, BuildOptions } from 'esbuild'
import { hybridExportPlugin } from 'esbuild-plugin-hybrid-export'

const plugin = hybridExportPlugin({
  to: 'target/esm',
  toExt: '.mjs'
})
const config: BuildOptions = {
  entryPoints: ['index.ts'],
  outdir: 'target/cjs',
  plugins: [plugin],
  format: 'cjs'
}

await build(config)

Refs

License

MIT

版本列表
0.3.4 2026-04-13
0.3.2 2026-04-05
0.3.1 2024-11-27
0.3.0 2024-11-27
0.2.5 2024-08-27
0.2.4 2024-05-26
0.2.3 2024-05-05
0.2.2 2024-05-05
0.2.1 2024-05-03
0.2.0 2024-04-27
0.1.0 2024-04-27
0.0.0 2024-04-01