Skip to content

Commit a66babd

Browse files
committed
refactor(wrangler): Explicitely pick node compat plugins for each mode
1 parent 04660b3 commit a66babd

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

packages/wrangler/src/deployment-bundle/bundle.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as fs from "node:fs";
22
import * as path from "node:path";
3-
import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
4-
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
53
import chalk from "chalk";
64
import * as esbuild from "esbuild";
75
import {
@@ -18,12 +16,9 @@ import {
1816
} from "./build-failures";
1917
import { dedupeModulesByName } from "./dedupe-modules";
2018
import { getEntryPointFromMetafile } from "./entry-point-from-metafile";
21-
import { asyncLocalStoragePlugin } from "./esbuild-plugins/als-external";
2219
import { cloudflareInternalPlugin } from "./esbuild-plugins/cloudflare-internal";
2320
import { configProviderPlugin } from "./esbuild-plugins/config-provider";
24-
import { nodejsHybridPlugin } from "./esbuild-plugins/hybrid-nodejs-compat";
25-
import { nodejsCompatPlugin } from "./esbuild-plugins/nodejs-compat";
26-
import { standardURLPlugin } from "./esbuild-plugins/standard-url";
21+
import { getNodeJSCompatPlugins } from "./esbuild-plugins/nodejs-plugins";
2722
import { writeAdditionalModules } from "./find-additional-modules";
2823
import { noopModuleCollector } from "./module-collection";
2924
import type { Config } from "../config";
@@ -440,20 +435,7 @@ export async function bundleWorker(
440435
plugins: [
441436
aliasPlugin,
442437
moduleCollector.plugin,
443-
...(nodejsCompatMode === "als" ? [asyncLocalStoragePlugin] : []),
444-
...(nodejsCompatMode === "legacy"
445-
? [
446-
NodeGlobalsPolyfills({ buffer: true }),
447-
standardURLPlugin(),
448-
NodeModulesPolyfills(),
449-
]
450-
: []),
451-
// Runtime Node.js compatibility (will warn if not using nodejs compat flag and are trying to import from a Node.js builtin).
452-
...(nodejsCompatMode === "v1" || nodejsCompatMode !== "v2"
453-
? [nodejsCompatPlugin(nodejsCompatMode === "v1")]
454-
: []),
455-
// Hybrid Node.js compatibility
456-
...(nodejsCompatMode === "v2" ? [nodejsHybridPlugin()] : []),
438+
...getNodeJSCompatPlugins(nodejsCompatMode ?? null),
457439
cloudflareInternalPlugin,
458440
buildResultPlugin,
459441
...(plugins || []),

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import chalk from "chalk";
33
import { logger } from "../../logger";
44
import { dedent } from "../../utils/dedent";
55
import type { Plugin } from "esbuild";
6+
import type { NodeJSCompatMode } from "miniflare";
67

78
/**
8-
* An esbuild plugin that will mark any `node:...` imports as external.
9+
* An esbuild plugin that will:
10+
* - mark any `node:...` imports as external
11+
* - warn if there are node imports (if not in v1 mode)
12+
*
13+
* Applies to: null, als, legacy and v1 modes.
914
*/
10-
export const nodejsCompatPlugin: (silenceWarnings: boolean) => Plugin = (
11-
silenceWarnings
12-
) => ({
15+
export const nodejsCompatPlugin = (mode: NodeJSCompatMode): Plugin => ({
1316
name: "nodejs_compat-imports",
1417
setup(pluginBuild) {
1518
// Infinite loop detection
@@ -87,7 +90,7 @@ export const nodejsCompatPlugin: (silenceWarnings: boolean) => Plugin = (
8790
// Wait until the build finishes to log warnings, so that all files which import a package
8891
// can be collated
8992
pluginBuild.onEnd(() => {
90-
if (!silenceWarnings) {
93+
if (mode !== "v1") {
9194
warnedPackaged.forEach((importers: string[], path: string) => {
9295
logger.warn(
9396
dedent`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
2+
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
3+
import { asyncLocalStoragePlugin } from "./als-external";
4+
import { nodejsHybridPlugin } from "./hybrid-nodejs-compat";
5+
import { nodejsCompatPlugin } from "./nodejs-compat";
6+
import { standardURLPlugin } from "./standard-url";
7+
import type { Plugin } from "esbuild";
8+
import type { NodeJSCompatMode } from "miniflare";
9+
10+
/**
11+
* Returns the list of ESBuild plugins to use for a given compat mode.
12+
*/
13+
export function getNodeJSCompatPlugins(mode: NodeJSCompatMode): Plugin[] {
14+
switch (mode) {
15+
case "als":
16+
return [asyncLocalStoragePlugin, nodejsCompatPlugin(mode)];
17+
case "legacy":
18+
return [
19+
NodeGlobalsPolyfills({ buffer: true }),
20+
standardURLPlugin(),
21+
NodeModulesPolyfills(),
22+
nodejsCompatPlugin(mode),
23+
];
24+
case "v1":
25+
return [nodejsCompatPlugin(mode)];
26+
case "v2":
27+
return [nodejsHybridPlugin()];
28+
case null:
29+
return [nodejsCompatPlugin(mode)];
30+
}
31+
}

0 commit comments

Comments
 (0)