Skip to content

Commit e9bb8d3

Browse files
vicbjamesopstad
andauthored
fix require("debug") in nodejs_compat mode (#10149)
* fix `require("debug")` in nodejs_compat mode * fixup! review feedback --------- Co-authored-by: James Opstad <[email protected]>
1 parent c307b08 commit e9bb8d3

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

.changeset/nasty-foxes-find.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix `require("debug")` in nodejs_compat mode

packages/wrangler/e2e/unenv-preset/preset.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ describe.each(testConfigs)(
7070
main: join(__dirname, "/worker/index.ts"),
7171
compatibility_date: compatibilityDate,
7272
compatibility_flags: ["nodejs_compat", ...compatibilityFlags],
73+
// Enable `enabled` logs for the `debug` package
7374
vars: {
74-
DEBUG: "example",
75+
DEBUG: "enabled",
7576
},
7677
}),
7778
});

packages/wrangler/e2e/unenv-preset/worker/index.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const TESTS: Record<string, () => void> = {
1212
testTimers,
1313
testNet,
1414
testTls,
15-
testDebug,
15+
testImportDebug,
16+
testRequireDebug,
1617
testHttp,
1718
testHttps,
1819
};
@@ -212,7 +213,7 @@ export async function testTls() {
212213
assert.strictEqual(typeof tls.convertALPNProtocols, "function");
213214
}
214215

215-
export async function testDebug() {
216+
export async function testImportDebug() {
216217
// @ts-expect-error "debug" is an unenv alias, not installed locally
217218
const debug = (await import("debug")).default;
218219
const logs: string[] = [];
@@ -221,13 +222,35 @@ export async function testDebug() {
221222
debug.log = (...args: string[]) =>
222223
logs.push(args.map((arg) => arg.toString()).join(" "));
223224

224-
const exampleLog = debug("example");
225-
const testLog = exampleLog.extend("test");
225+
// This should log because as `DEBUG` is set to "enabled".
226+
const enabledLog = debug("enabled");
227+
enabledLog("This should be logged");
226228

227-
exampleLog("This is an example log");
228-
testLog("This is a test log");
229+
// This should not log as `DEBUG` does not contain "enabled:disabled"
230+
const disabledLog = enabledLog.extend("disabled");
231+
disabledLog("This should not be logged");
229232

230-
assert.deepEqual(logs, ["example This is an example log +0ms"]);
233+
assert.deepEqual(logs, ["enabled This should be logged +0ms"]);
234+
}
235+
236+
export async function testRequireDebug() {
237+
// eslint-disable-next-line @typescript-eslint/no-require-imports
238+
const debug = require("debug");
239+
const logs: string[] = [];
240+
241+
// Append all logs to the array instead of logging to console
242+
debug.log = (...args: string[]) =>
243+
logs.push(args.map((arg) => arg.toString()).join(" "));
244+
245+
// This should log because as `DEBUG` is set to "enabled".
246+
const enabledLog = debug("enabled");
247+
enabledLog("This should be logged");
248+
249+
// This should not log as `DEBUG` does not contain "enabled:disabled"
250+
const disabledLog = enabledLog.extend("disabled");
251+
disabledLog("This should not be logged");
252+
253+
assert.deepEqual(logs, ["enabled This should be logged +0ms"]);
231254
}
232255

233256
export async function testHttp() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ function handleUnenvAliasedPackages(
143143
if (
144144
args.kind === "require-call" &&
145145
(unresolvedAlias.startsWith("unenv/npm/") ||
146+
unresolvedAlias.startsWith("@cloudflare/unenv-preset/npm/") ||
146147
unresolvedAlias.startsWith("unenv/mock/"))
147148
) {
148149
return {

0 commit comments

Comments
 (0)