Skip to content

Commit

Permalink
Mark node:* imports as external when using the nodejs_compat comp…
Browse files Browse the repository at this point in the history
…atibility flag
  • Loading branch information
GregBrimble committed Jan 12, 2023
1 parent ab63043 commit 8f12780
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/late-bulldogs-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

feat: Add support for the `nodejs_compat` Compatibility Flag when bundling a Worker with Wrangler

This new Compatibility Flag is incompatible with the legacy `--node-compat` CLI argument and `node_compat` configuration option. If you want to use the new runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or your config file.
12 changes: 12 additions & 0 deletions packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ Add "node_compat = true" to your wrangler.toml file to enable Node compatibility
}
}

const nodejsCompatPlugin: esbuild.Plugin = {
name: "nodejs_compat Plugin",
setup(pluginBuild) {
pluginBuild.onResolve({ filter: /node:.*/ }, () => {
return { external: true };
});
},
};

/**
* Generate a bundle for the worker identified by the arguments passed in.
*/
Expand All @@ -102,6 +111,7 @@ export async function bundleWorker(
tsconfig?: string;
minify?: boolean;
legacyNodeCompat?: boolean;
nodejsCompat?: boolean;
define: Config["define"];
checkFetch: boolean;
services?: Config["services"];
Expand Down Expand Up @@ -130,6 +140,7 @@ export async function bundleWorker(
tsconfig,
minify,
legacyNodeCompat,
nodejsCompat,
checkFetch,
local,
assets,
Expand Down Expand Up @@ -354,6 +365,7 @@ export async function bundleWorker(
...(legacyNodeCompat
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
: []),
...(nodejsCompat ? [nodejsCompatPlugin] : []),
...(plugins || []),
],
...(jsxFactory && { jsxFactory }),
Expand Down
13 changes: 13 additions & 0 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export async function startDev(args: StartDevOptions) {
const {
entry,
legacyNodeCompat,
nodejsCompat,
upstreamProtocol,
zoneId,
host,
Expand Down Expand Up @@ -461,6 +462,7 @@ export async function startDev(args: StartDevOptions) {
legacyEnv={isLegacyEnv(configParam)}
minify={args.minify ?? configParam.minify}
legacyNodeCompat={legacyNodeCompat}
nodejsCompat={nodejsCompat}
build={configParam.build || {}}
define={{ ...configParam.define, ...cliDefines }}
initialMode={
Expand Down Expand Up @@ -559,6 +561,7 @@ export async function startApiDev(args: StartDevOptions) {
const {
entry,
legacyNodeCompat,
nodejsCompat,
upstreamProtocol,
zoneId,
host,
Expand Down Expand Up @@ -598,6 +601,7 @@ export async function startApiDev(args: StartDevOptions) {
legacyEnv: isLegacyEnv(configParam),
minify: args.minify ?? configParam.minify,
legacyNodeCompat,
nodejsCompat,
build: configParam.build || {},
define: { ...config.define, ...cliDefines },
initialMode: args.local ? "local" : "remote",
Expand Down Expand Up @@ -786,6 +790,14 @@ async function validateDevServerSettings(
"Enabling Node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
);
}
const compatibilityFlags =
args.compatibilityFlags ?? config.compatibility_flags;
const nodejsCompat = compatibilityFlags.includes("nodejs_compat");
if (legacyNodeCompat && nodejsCompat) {
throw new Error(
"The `nodejs_compat` compatibility flag cannot be used in conjunction with the legacy `--node-compat` flag. If you want to use the Workers runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or `node_compat = true` from your config file."
);
}

if (args.experimentalEnableLocalPersistence) {
logger.warn(
Expand All @@ -807,6 +819,7 @@ async function validateDevServerSettings(
entry,
upstreamProtocol,
legacyNodeCompat,
nodejsCompat,
getLocalPort,
getInspectorPort,
zoneId,
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export type DevProps = {
usageModel: "bundled" | "unbound" | undefined;
minify: boolean | undefined;
legacyNodeCompat: boolean | undefined;
nodejsCompat: boolean | undefined;
build: Config["build"];
env: string | undefined;
legacyEnv: boolean;
Expand Down Expand Up @@ -293,6 +294,7 @@ function DevSession(props: DevSessionProps) {
tsconfig: props.tsconfig,
minify: props.minify,
legacyNodeCompat: props.legacyNodeCompat,
nodejsCompat: props.nodejsCompat,
betaD1Shims,
define: props.define,
noBundle: props.noBundle,
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/dev/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export async function startDevServer(
tsconfig: props.tsconfig,
minify: props.minify,
legacyNodeCompat: props.legacyNodeCompat,
nodejsCompat: props.nodejsCompat,
define: props.define,
noBundle: props.noBundle,
assets: props.assetsConfig,
Expand Down Expand Up @@ -210,6 +211,7 @@ async function runEsbuild({
tsconfig,
minify,
legacyNodeCompat,
nodejsCompat,
define,
noBundle,
workerDefinitions,
Expand All @@ -232,6 +234,7 @@ async function runEsbuild({
tsconfig: string | undefined;
minify: boolean | undefined;
legacyNodeCompat: boolean | undefined;
nodejsCompat: boolean | undefined;
noBundle: boolean;
workerDefinitions: WorkerRegistry;
firstPartyWorkerDevFacade: boolean | undefined;
Expand Down Expand Up @@ -264,6 +267,7 @@ async function runEsbuild({
tsconfig,
minify,
legacyNodeCompat,
nodejsCompat,
define,
checkFetch: true,
assets: assets && {
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/dev/use-esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function useEsbuild({
tsconfig,
minify,
legacyNodeCompat,
nodejsCompat,
betaD1Shims,
define,
noBundle,
Expand All @@ -55,6 +56,7 @@ export function useEsbuild({
tsconfig: string | undefined;
minify: boolean | undefined;
legacyNodeCompat: boolean | undefined;
nodejsCompat: boolean | undefined;
betaD1Shims?: string[];
noBundle: boolean;
workerDefinitions: WorkerRegistry;
Expand Down Expand Up @@ -122,6 +124,7 @@ export function useEsbuild({
tsconfig,
minify,
legacyNodeCompat,
nodejsCompat,
betaD1Shims,
define,
checkFetch: true,
Expand Down Expand Up @@ -188,6 +191,7 @@ export function useEsbuild({
noBundle,
minify,
legacyNodeCompat,
nodejsCompat,
define,
assets,
services,
Expand Down
9 changes: 9 additions & 0 deletions packages/wrangler/src/dev/validate-dev-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ export function validateDevProps(props: DevProps) {
"You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
);
}

if (
props.compatibilityFlags?.includes("nodejs_compat") &&
props.legacyNodeCompat
) {
throw new Error(
"You cannot use the `nodejs_compat` compatibility flag in conjunction with the legacy `--node-compat` flag. If you want to use the new runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or your config file."
);
}
}
24 changes: 24 additions & 0 deletions packages/wrangler/src/pages/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ export function Options(yargs: Argv) {
type: "boolean",
hidden: true,
},
"compatibility-date": {
describe: "Date to use for compatibility checks",
type: "string",
requiresArg: true,
},
"compatibility-flags": {
describe: "Flags to use for compatibility checks",
alias: "compatibility-flag",
type: "string",
requiresArg: true,
array: true,
},
bindings: {
type: "string",
describe:
Expand All @@ -104,6 +116,7 @@ export const Handler = async ({
plugin,
buildOutputDirectory,
nodeCompat: legacyNodeCompat,
compatibilityFlags,
bindings,
}: PagesBuildArgs) => {
if (!isInPagesCI) {
Expand All @@ -117,6 +130,13 @@ export const Handler = async ({
);
}

const nodejsCompat = compatibilityFlags?.includes("nodejs_compat");
if (legacyNodeCompat && nodejsCompat) {
throw new Error(
"The `nodejs_compat` compatibility flag cannot be used in conjunction with the legacy `--node-compat` flag. If you want to use the Workers runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command."
);
}

let d1Databases: string[] | undefined = undefined;
if (bindings) {
try {
Expand All @@ -140,6 +160,7 @@ export const Handler = async ({
plugin,
buildOutputDirectory,
legacyNodeCompat,
nodejsCompat,
routesOutputPath,
local: false,
d1Databases,
Expand Down Expand Up @@ -174,6 +195,7 @@ export async function buildFunctions({
buildOutputDirectory,
routesOutputPath,
legacyNodeCompat,
nodejsCompat,
local,
d1Databases,
}: Partial<
Expand All @@ -189,6 +211,7 @@ export async function buildFunctions({
>
> & {
legacyNodeCompat?: boolean;
nodejsCompat?: boolean;
functionsDirectory: string;
onEnd?: () => void;
outfile: Required<PagesBuildArgs>["outfile"];
Expand Down Expand Up @@ -264,6 +287,7 @@ export async function buildFunctions({
onEnd,
buildOutputDirectory,
legacyNodeCompat,
nodejsCompat,
})
);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/wrangler/src/pages/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ export const Handler = async ({

let scriptPath = "";

const nodejsCompat = compatibilityFlags?.includes("nodejs_compat");

if (usingWorkerScript) {
scriptPath = workerScriptPath;
let runBuild = async () => {
Expand All @@ -275,6 +277,7 @@ export const Handler = async ({
workerScriptPath,
outfile: scriptPath,
directory: directory ?? ".",
nodejsCompat,
local: true,
sourcemap: true,
watch: true,
Expand Down Expand Up @@ -303,6 +306,13 @@ export const Handler = async ({
);
}

if (legacyNodeCompat && nodejsCompat) {
throw new FatalError(
"The `nodejs_compat` compatibility flag cannot be used in conjunction with the legacy `--node-compat` flag. If you want to use the Workers runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or `node_compat = true` from your config file.",
1
);
}

logger.log(`Compiling worker to "${scriptPath}"...`);
const onEnd = () => scriptReadyResolve();
try {
Expand All @@ -315,6 +325,7 @@ export const Handler = async ({
onEnd,
buildOutputDirectory: directory,
legacyNodeCompat,
nodejsCompat,
local: true,
});
await metrics.sendMetricsEvent("build pages functions");
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/pages/functions/buildPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function buildPlugin({
sourcemap,
watch,
legacyNodeCompat,
// We don't currently have a mechanism for Plugins 'requiring' a specific compat date/flag,
// but if someone wants to publish a Plugin which does require this new `nodejs_compat` flag
// and they document that on their README.md, we should let them.
nodejsCompat: true,
define: {},
betaD1Shims: (betaD1Shims || []).map(
(binding) => `${D1_BETA_PREFIX}${binding}`
Expand Down
6 changes: 6 additions & 0 deletions packages/wrangler/src/pages/functions/buildWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type Options = {
onEnd?: () => void;
buildOutputDirectory?: string;
legacyNodeCompat?: boolean;
nodejsCompat?: boolean;
functionsDirectory: string;
local: boolean;
betaD1Shims?: string[];
Expand All @@ -33,6 +34,7 @@ export function buildWorker({
onEnd = () => {},
buildOutputDirectory,
legacyNodeCompat,
nodejsCompat,
functionsDirectory,
local,
betaD1Shims,
Expand All @@ -50,6 +52,7 @@ export function buildWorker({
sourcemap,
watch,
legacyNodeCompat,
nodejsCompat,
loader: {
".txt": "text",
".html": "text",
Expand Down Expand Up @@ -160,6 +163,7 @@ export type RawOptions = {
onEnd?: () => void;
buildOutputDirectory?: string;
legacyNodeCompat?: boolean;
nodejsCompat?: boolean;
local: boolean;
betaD1Shims?: string[];
};
Expand All @@ -181,6 +185,7 @@ export function buildRawWorker({
plugins = [],
onEnd = () => {},
legacyNodeCompat,
nodejsCompat,
local,
betaD1Shims,
}: RawOptions) {
Expand All @@ -196,6 +201,7 @@ export function buildRawWorker({
sourcemap,
watch,
legacyNodeCompat,
nodejsCompat,
loader: {
".txt": "text",
".html": "text",
Expand Down
Loading

0 comments on commit 8f12780

Please sign in to comment.