Skip to content

Commit ea20f57

Browse files
authored
fix: inject a require function to the esm build (#21)
Surprisingly, esbuild apparently outputs ESM bundles with `require` calls still in them (which is invalid). Until esbuild sorts this out, we have no option but to inject a `createRequire` call and hope for the best.
1 parent 9bd1f9f commit ea20f57

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

tsup-require-shim.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {createRequire} from 'node:module';
2+
3+
if (!('require' in globalThis)) {
4+
globalThis.require = createRequire(import.meta.url);
5+
}

tsup.config.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import { defineConfig } from "tsup";
1+
import { defineConfig, type Options } from "tsup";
22

3-
export default defineConfig({
3+
const defaultConfig: Options = {
44
entryPoints: ["src/main.ts"],
55
outDir: "dist",
6-
format: ["esm", "cjs"],
76
tsconfig: "./tsconfig.json",
8-
target: "es2018",
7+
target: "es2022",
98
minify: false,
109
minifySyntax: true,
1110
minifyWhitespace: false,
1211
minifyIdentifiers: true,
1312
clean: true,
1413
dts: true,
15-
});
14+
};
15+
16+
export default defineConfig([
17+
{
18+
...defaultConfig,
19+
format: ["esm"],
20+
inject: ['tsup-require-shim.ts']
21+
},
22+
{
23+
...defaultConfig,
24+
format: ["cjs"]
25+
}
26+
]);

0 commit comments

Comments
 (0)