npm.io
0.1.0 • Published 3 years ago

esbuild-plugin-esm-cjs-shim

Licence
MIT
Version
0.1.0
Deps
0
Size
7 kB
Vulns
0
Weekly
0

esbuild-plugin-esm-cjs-shim

Shims ESM and CJS features, handy if you are building hybrid NPM modules.

Features

Shims when building ESM:

  • __filename
  • __dirname
  • require (require.resolve)

Shims when building CJS:

  • import.meta.url

Getting Started

npm install esbuild-plugin-esm-cjs-shim

Load plugin

const { shimPlugin } = require("esbuild-plugin-esm-cjs-shim)";

or if ESM:

import { shimPlugin } = from "esbuild-plugin-esm-cjs-shim";

Add following to your esbuild-conf:

{
    entryPoints: ["./commonjs.entry.js"],
    bundle: true,
    write: false,
+    plugins: [shimPlugin()],
+    format: "esm", // or "cjs" depending on what you need to build
+    platform: "node",
};
Known issues
  • All shims are included at this point.
Credits

Idea from tsup, the primary reason for this lib is to create a separate esbuild-plugin.

Noteworthy libs