Skip to content

Commit 28b1dc7

Browse files
authored
Fix requiring unenv aliased packages (#8021)
1 parent ca3cbc4 commit 28b1dc7

File tree

10 files changed

+126
-19
lines changed

10 files changed

+126
-19
lines changed

.changeset/unlucky-actors-swim.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: prevent \_\_cf_cjs name collision in the hybrid Nodejs compat plugin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fetch = require("cross-fetch");
2+
3+
export const onRequest = () => {
4+
const supportsDefaultExports = typeof fetch === "function";
5+
const supportsNamedExports = typeof fetch.Headers === "function";
6+
7+
return new Response(
8+
supportsDefaultExports && supportsNamedExports ? "OK!" : "KO!"
9+
);
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "pages-functions-unenv-alias",
3+
"private": true,
4+
"sideEffects": false,
5+
"scripts": {
6+
"check:type": "tsc",
7+
"dev:functions-app": "wrangler pages dev --port 8792",
8+
"test:ci": "vitest run",
9+
"test:watch": "vitest",
10+
"type:tests": "tsc -p ./tests/tsconfig.json"
11+
},
12+
"devDependencies": {
13+
"@cloudflare/workers-tsconfig": "workspace:^",
14+
"typescript": "catalog:default",
15+
"undici": "catalog:default",
16+
"vitest": "catalog:default",
17+
"wrangler": "workspace:*"
18+
},
19+
"engines": {
20+
"node": ">=16.13"
21+
},
22+
"volta": {
23+
"extends": "../../package.json"
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { resolve } from "node:path";
2+
import { fetch } from "undici";
3+
import { describe, it } from "vitest";
4+
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";
5+
6+
describe("Pages functions with unenv aliased packages", () => {
7+
it("should run dev server when requiring an unenv aliased package", async ({
8+
expect,
9+
onTestFinished,
10+
}) => {
11+
const { ip, port, stop } = await runWranglerPagesDev(
12+
resolve(__dirname, ".."),
13+
"./functions",
14+
["--port=0", "--inspector-port=0"]
15+
);
16+
onTestFinished(stop);
17+
const response = await fetch(`http://${ip}:${port}/`);
18+
const body = await response.text();
19+
expect(body).toEqual(`OK!`);
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"]
5+
},
6+
"include": ["**/*.ts", "../../../node-types.d.ts"]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"esModuleInterop": true,
5+
"module": "CommonJS",
6+
"lib": ["ES2020"],
7+
"types": ["node", "@cloudflare/workers-types"],
8+
"moduleResolution": "node",
9+
"noEmit": true,
10+
"skipLibCheck": true,
11+
"checkJs": true
12+
},
13+
"include": [
14+
"apps/workerjs-directory/_worker.js/index.js",
15+
"apps/workerjs-file/_worker.js",
16+
"functions/[[path]].ts",
17+
"tests",
18+
"../../node-types.d.ts"
19+
]
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineProject, mergeConfig } from "vitest/config";
2+
import configShared from "../../vitest.shared";
3+
4+
export default mergeConfig(
5+
configShared,
6+
defineProject({
7+
test: {},
8+
})
9+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "pages-functions-unenv-alias"
2+
compatibility_date = "2024-12-30"
3+
compatibility_flags = ["nodejs_compat"]

packages/wrangler/src/deployment-bundle/esbuild-plugins/hybrid-nodejs-compat.ts

+6-17
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,18 @@ function handleUnenvAliasedPackages(
143143
};
144144
});
145145

146-
build.initialOptions.banner = { js: "", ...build.initialOptions.banner };
147-
build.initialOptions.banner.js += dedent`
148-
function __cf_cjs(esm) {
149-
const cjs = 'default' in esm ? esm.default : {};
150-
for (const [k, v] of Object.entries(esm)) {
151-
if (k !== 'default') {
152-
Object.defineProperty(cjs, k, {
153-
enumerable: true,
154-
value: v,
155-
});
156-
}
157-
}
158-
return cjs;
159-
}
160-
`;
161-
162146
build.onLoad(
163147
{ filter: /.*/, namespace: REQUIRED_UNENV_ALIAS_NAMESPACE },
164148
({ path }) => {
165149
return {
166150
contents: dedent`
167151
import * as esm from '${path}';
168-
module.exports = __cf_cjs(esm);
152+
module.exports = Object.entries(esm)
153+
.filter(([k,]) => k !== 'default')
154+
.reduce((cjs, [k, value]) =>
155+
Object.defineProperty(cjs, k, { value, enumerable: true }),
156+
"default" in esm ? esm.default : {}
157+
);
169158
`,
170159
loader: "js",
171160
};

pnpm-lock.yaml

+20-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)