Skip to content

Commit 8c4b8a3

Browse files
Keep Node.js ESM packages working when consumers rebundle them as CommonJS (#15870)
1 parent 8280086 commit 8c4b8a3

8 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎.changeset/clever-dodos-require.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@cloudflare/autoconfig": patch
3+
"@cloudflare/codemods": patch
4+
"@cloudflare/deploy-helpers": patch
5+
"@cloudflare/workers-auth": patch
6+
"@cloudflare/workers-utils": patch
7+
---
8+
9+
Keep Node.js ESM packages working when consumers rebundle them as CommonJS
10+
11+
Node.js-targeted ESM bundles now provide a real `require` implementation for bundled CommonJS dependencies. This avoids downstream patches for dynamic require calls and keeps the packages usable when a consumer rebundles them to CommonJS.

‎packages/autoconfig/tsup.config.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export default defineConfig(() => [
77
keepNames: true,
88
entry: ["src/index.ts"],
99
platform: "node",
10+
// Provide require for bundled CommonJS dependencies. The __filename
11+
// fallback keeps the output working when it is rebundled to CommonJS.
12+
banner: {
13+
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url || (typeof __filename === "string" ? __filename : "/"));',
14+
},
1015
format: "esm",
1116
dts: true,
1217
outDir: "dist",

‎packages/codemods/tsup.config.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export default defineConfig(() => [
1313
tsconfig: "tsconfig.json",
1414
metafile: true,
1515
sourcemap: process.env.SOURCEMAPS !== "false",
16+
// Provide require for bundled CommonJS dependencies. The __filename
17+
// fallback keeps the output working when it is rebundled to CommonJS.
1618
banner: {
17-
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);',
19+
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url || (typeof __filename === "string" ? __filename : "/"));',
1820
},
1921
noExternal: [/.*/],
2022
},

‎packages/containers-shared/tsup.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig(() => [
1313
// Bundled ws uses CommonJS imports for Node builtins. Miniflare bundles
1414
// this package into CJS, where import.meta.url is unavailable.
1515
banner: {
16-
js: 'import { createRequire } from "node:module"; const require = createRequire(typeof __filename === "string" ? __filename : import.meta.url);',
16+
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url || (typeof __filename === "string" ? __filename : "/"));',
1717
},
1818
format: "esm",
1919
dts: true,

‎packages/deploy-helpers/tsup.config.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export default defineConfig(() => [
1616
"startup-profile": "src/startup-profile.ts",
1717
},
1818
platform: "node",
19+
// Provide require for bundled CommonJS dependencies. The __filename
20+
// fallback keeps the output working when it is rebundled to CommonJS.
21+
banner: {
22+
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url || (typeof __filename === "string" ? __filename : "/"));',
23+
},
1924
format: "esm",
2025
dts: true,
2126
outDir: "dist",

‎packages/shared-ast-primitives/tsup.config.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export default defineConfig(() => [
66
keepNames: true,
77
entry: ["src/index.ts"],
88
platform: "node",
9+
// Provide require for bundled CommonJS dependencies. The __filename
10+
// fallback keeps the output working when it is rebundled to CommonJS.
11+
banner: {
12+
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url || (typeof __filename === "string" ? __filename : "/"));',
13+
},
914
format: "esm",
1015
dts: true,
1116
outDir: "dist",

‎packages/workers-auth/tsup.config.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export default defineConfig(() => [
1111
"src/test-helpers/index.ts",
1212
],
1313
platform: "node",
14+
// Provide require for bundled CommonJS dependencies. The __filename
15+
// fallback keeps the output working when it is rebundled to CommonJS.
16+
banner: {
17+
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url || (typeof __filename === "string" ? __filename : "/"));',
18+
},
1419
format: "esm",
1520
dts: true,
1621
outDir: "dist",

‎packages/workers-utils/tsup.config.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@ import { defineConfig } from "tsup";
22
import { EXTERNAL_DEPENDENCIES } from "./scripts/deps";
33

44
export default defineConfig(() => [
5+
{
6+
treeshake: true,
7+
keepNames: true,
8+
// Keep the browser export free of the Node-only banner used below.
9+
entry: ["src/browser.ts"],
10+
platform: "node",
11+
format: "esm",
12+
dts: true,
13+
outDir: "dist",
14+
tsconfig: "tsconfig.json",
15+
metafile: true,
16+
sourcemap: process.env.SOURCEMAPS !== "false",
17+
define: {
18+
"process.env.NODE_ENV": `'${"production"}'`,
19+
},
20+
external: ["@cloudflare/*", "vitest", ...EXTERNAL_DEPENDENCIES],
21+
},
522
{
623
treeshake: true,
724
keepNames: true,
825
entry: [
926
"src/index.ts",
10-
"src/browser.ts",
1127
"src/prometheus-metrics.ts",
1228
"src/test-helpers/index.ts",
1329
// Leaf entry points let consumers bundle individual utilities without
@@ -22,6 +38,11 @@ export default defineConfig(() => [
2238
"src/zod-format.ts",
2339
],
2440
platform: "node",
41+
// Provide require for bundled CommonJS dependencies. The __filename
42+
// fallback keeps the output working when it is rebundled to CommonJS.
43+
banner: {
44+
js: 'import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url || (typeof __filename === "string" ? __filename : "/"));',
45+
},
2546
format: "esm",
2647
dts: true,
2748
outDir: "dist",

0 commit comments

Comments
 (0)