Skip to content

Commit

Permalink
Fix requiring unenv aliased packages (#8021)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xD34DC0DE authored Feb 14, 2025
1 parent ca3cbc4 commit 28b1dc7
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-actors-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: prevent \_\_cf_cjs name collision in the hybrid Nodejs compat plugin
10 changes: 10 additions & 0 deletions fixtures/pages-functions-unenv-alias/functions/[[path]].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require("cross-fetch");

export const onRequest = () => {
const supportsDefaultExports = typeof fetch === "function";
const supportsNamedExports = typeof fetch.Headers === "function";

return new Response(
supportsDefaultExports && supportsNamedExports ? "OK!" : "KO!"
);
};
25 changes: 25 additions & 0 deletions fixtures/pages-functions-unenv-alias/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "pages-functions-unenv-alias",
"private": true,
"sideEffects": false,
"scripts": {
"check:type": "tsc",
"dev:functions-app": "wrangler pages dev --port 8792",
"test:ci": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:^",
"typescript": "catalog:default",
"undici": "catalog:default",
"vitest": "catalog:default",
"wrangler": "workspace:*"
},
"engines": {
"node": ">=16.13"
},
"volta": {
"extends": "../../package.json"
}
}
21 changes: 21 additions & 0 deletions fixtures/pages-functions-unenv-alias/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { resolve } from "node:path";
import { fetch } from "undici";
import { describe, it } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe("Pages functions with unenv aliased packages", () => {
it("should run dev server when requiring an unenv aliased package", async ({
expect,
onTestFinished,
}) => {
const { ip, port, stop } = await runWranglerPagesDev(
resolve(__dirname, ".."),
"./functions",
["--port=0", "--inspector-port=0"]
);
onTestFinished(stop);
const response = await fetch(`http://${ip}:${port}/`);
const body = await response.text();
expect(body).toEqual(`OK!`);
});
});
7 changes: 7 additions & 0 deletions fixtures/pages-functions-unenv-alias/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
20 changes: 20 additions & 0 deletions fixtures/pages-functions-unenv-alias/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ES2020",
"esModuleInterop": true,
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["node", "@cloudflare/workers-types"],
"moduleResolution": "node",
"noEmit": true,
"skipLibCheck": true,
"checkJs": true
},
"include": [
"apps/workerjs-directory/_worker.js/index.js",
"apps/workerjs-file/_worker.js",
"functions/[[path]].ts",
"tests",
"../../node-types.d.ts"
]
}
9 changes: 9 additions & 0 deletions fixtures/pages-functions-unenv-alias/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineProject, mergeConfig } from "vitest/config";
import configShared from "../../vitest.shared";

export default mergeConfig(
configShared,
defineProject({
test: {},
})
);
3 changes: 3 additions & 0 deletions fixtures/pages-functions-unenv-alias/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "pages-functions-unenv-alias"
compatibility_date = "2024-12-30"
compatibility_flags = ["nodejs_compat"]
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,18 @@ function handleUnenvAliasedPackages(
};
});

build.initialOptions.banner = { js: "", ...build.initialOptions.banner };
build.initialOptions.banner.js += dedent`
function __cf_cjs(esm) {
const cjs = 'default' in esm ? esm.default : {};
for (const [k, v] of Object.entries(esm)) {
if (k !== 'default') {
Object.defineProperty(cjs, k, {
enumerable: true,
value: v,
});
}
}
return cjs;
}
`;

build.onLoad(
{ filter: /.*/, namespace: REQUIRED_UNENV_ALIAS_NAMESPACE },
({ path }) => {
return {
contents: dedent`
import * as esm from '${path}';
module.exports = __cf_cjs(esm);
module.exports = Object.entries(esm)
.filter(([k,]) => k !== 'default')
.reduce((cjs, [k, value]) =>
Object.defineProperty(cjs, k, { value, enumerable: true }),
"default" in esm ? esm.default : {}
);
`,
loader: "js",
};
Expand Down
22 changes: 20 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 28b1dc7

Please sign in to comment.