Skip to content

Commit d078800

Browse files
nodejs compat functions build (#3133)
1 parent 5079f47 commit d078800

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

.changeset/metal-buckets-fly.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix pages building not taking into account the nodejs_compat flag (and improve the related error message)

packages/wrangler/src/__tests__/pages/functions-build.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ export default {
448448
"Build failed with 1 error:
449449
hello.js:2:36: ERROR: Could not resolve \\"node:async_hooks\\""
450450
`);
451+
expect(std.err).toContain(
452+
'The package "node:async_hooks" wasn\'t found on the file system but is built into node.'
453+
);
454+
expect(std.err).toContain(
455+
'Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility.'
456+
);
451457
});
452458

453459
it("should compile a _worker.js/ directory", async () => {

packages/wrangler/src/__tests__/publish.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7224,7 +7224,7 @@ export default{
72247224
expect(
72257225
esbuild.formatMessagesSync(err?.errors ?? [], { kind: "error" }).join()
72267226
).toMatch(
7227-
/The package "path" wasn't found on the file system but is built into node\.\s+Add "node_compat = true" to your wrangler\.toml file to enable Node compatibility\./
7227+
/The package "path" wasn't found on the file system but is built into node\.\s+Add "node_compat = true" to your wrangler\.toml file to enable Node.js compatibility\./
72287228
);
72297229
});
72307230

packages/wrangler/src/bundle.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,25 @@ export function isBuildFailure(err: unknown): err is esbuild.BuildFailure {
7777
* Rewrites esbuild BuildFailures for failing to resolve Node built-in modules
7878
* to suggest enabling Node compat as opposed to `platform: "node"`.
7979
*/
80-
export function rewriteNodeCompatBuildFailure(err: esbuild.BuildFailure) {
80+
export function rewriteNodeCompatBuildFailure(
81+
err: esbuild.BuildFailure,
82+
forPages = false
83+
) {
8184
for (const error of err.errors) {
8285
const match = nodeBuiltinResolveErrorText.exec(error.text);
8386
if (match !== null) {
87+
const issue = `The package "${match[1]}" wasn't found on the file system but is built into node.`;
88+
89+
const instructionForUser = `${
90+
forPages
91+
? 'Add the "nodejs_compat" compatibility flag to your Pages project'
92+
: 'Add "node_compat = true" to your wrangler.toml file'
93+
} to enable Node.js compatibility.`;
94+
8495
error.notes = [
8596
{
8697
location: null,
87-
text: `The package "${match[1]}" wasn't found on the file system but is built into node.
88-
Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.`,
98+
text: `${issue}\n${instructionForUser}`,
8999
},
90100
];
91101
}
@@ -148,6 +158,7 @@ export async function bundleWorker(
148158
// TODO: Rip these out https://github.com/cloudflare/workers-sdk/issues/2153
149159
disableModuleCollection?: boolean;
150160
isOutfile?: boolean;
161+
forPages?: boolean;
151162
}
152163
): Promise<BundleResult> {
153164
const {
@@ -179,6 +190,7 @@ export async function bundleWorker(
179190
plugins,
180191
disableModuleCollection,
181192
isOutfile,
193+
forPages,
182194
} = options;
183195

184196
// We create a temporary directory for any oneoff files we
@@ -412,7 +424,7 @@ export async function bundleWorker(
412424
result = await esbuild.build(buildOptions);
413425
} catch (e) {
414426
if (!legacyNodeCompat && isBuildFailure(e))
415-
rewriteNodeCompatBuildFailure(e);
427+
rewriteNodeCompatBuildFailure(e, forPages);
416428
throw e;
417429
}
418430

packages/wrangler/src/pages/build.ts

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ export const Handler = async (args: PagesBuildArgs) => {
233233
sourcemap,
234234
watch,
235235
betaD1Shims: d1Databases,
236+
nodejsCompat,
236237
});
237238
}
238239
} else {

packages/wrangler/src/pages/functions/buildPlugin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export function buildPlugin({
108108
targetConsumer: local ? "dev" : "publish",
109109
local,
110110
experimentalLocal: false,
111+
forPages: true,
111112
}
112113
);
113114
}

packages/wrangler/src/pages/functions/buildWorker.ts

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function buildWorker({
153153
targetConsumer: local ? "dev" : "publish",
154154
local,
155155
experimentalLocal: false,
156+
forPages: true,
156157
}
157158
);
158159
}
@@ -227,6 +228,7 @@ export function buildRawWorker({
227228
targetConsumer: local ? "dev" : "publish",
228229
local,
229230
experimentalLocal: false,
231+
forPages: true,
230232
}
231233
);
232234
}

0 commit comments

Comments
 (0)