Skip to content

Commit 9d2740a

Browse files
vicbpetebacondarwin
andauthoredJan 7, 2025··
test(wrangler): allow overriding the unenv preset (#7685)
Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>
1 parent 8b48ca6 commit 9d2740a

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed
 

‎.changeset/twelve-fireants-hunt.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
allow overriding the unenv preset.
6+
7+
By default wrangler uses the bundled unenv preset.
8+
9+
Setting `WRANGLER_UNENV_RESOLVE_PATHS` allow to use another version of the preset.
10+
Those paths are used when resolving the unenv module identifiers to absolute paths.
11+
This can be used to test a development version.

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as esbuild from "esbuild";
55
import {
66
getBuildConditionsFromEnv,
77
getBuildPlatformFromEnv,
8+
getUnenvResolvePathsFromEnv,
89
} from "../environment-variables/misc-variables";
910
import { UserError } from "../errors";
1011
import { getFlag } from "../experimental-flags";
@@ -390,6 +391,8 @@ export async function bundleWorker(
390391
},
391392
};
392393

394+
const unenvResolvePaths = getUnenvResolvePathsFromEnv()?.split(",");
395+
393396
const buildOptions: esbuild.BuildOptions & { metafile: true } = {
394397
// Don't use entryFile here as the file may have been changed when applying the middleware
395398
entryPoints: [entry.file],
@@ -435,7 +438,10 @@ export async function bundleWorker(
435438
plugins: [
436439
aliasPlugin,
437440
moduleCollector.plugin,
438-
...getNodeJSCompatPlugins(nodejsCompatMode ?? null),
441+
...getNodeJSCompatPlugins({
442+
mode: nodejsCompatMode ?? null,
443+
unenvResolvePaths,
444+
}),
439445
cloudflareInternalPlugin,
440446
buildResultPlugin,
441447
...(plugins || []),

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import type { Plugin, PluginBuild } from "esbuild";
88
const REQUIRED_NODE_BUILT_IN_NAMESPACE = "node-built-in-modules";
99
const REQUIRED_UNENV_ALIAS_NAMESPACE = "required-unenv-alias";
1010

11-
export const nodejsHybridPlugin: () => Plugin = () => {
11+
/**
12+
* ESBuild plugin to apply the unenv preset.
13+
*
14+
* @param unenvResolvePaths Root paths used to resolve absolute paths.
15+
* @returns ESBuild plugin
16+
*/
17+
export function nodejsHybridPlugin(unenvResolvePaths?: string[]): Plugin {
1218
// Get the resolved environment.
1319
const { env } = defineEnv({
1420
nodeCompat: true,
1521
presets: [cloudflare],
16-
resolve: true,
22+
resolve: {
23+
paths: unenvResolvePaths,
24+
},
1725
});
1826
const { alias, inject, external } = env;
1927
// Get the unresolved alias.
@@ -31,7 +39,7 @@ export const nodejsHybridPlugin: () => Plugin = () => {
3139
handleNodeJSGlobals(build, inject);
3240
},
3341
};
34-
};
42+
}
3543

3644
const NODEJS_MODULES_RE = new RegExp(`^(node:)?(${builtinModules.join("|")})$`);
3745

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import type { NodeJSCompatMode } from "miniflare";
1010
/**
1111
* Returns the list of ESBuild plugins to use for a given compat mode.
1212
*/
13-
export function getNodeJSCompatPlugins(mode: NodeJSCompatMode): Plugin[] {
13+
export function getNodeJSCompatPlugins({
14+
mode,
15+
unenvResolvePaths,
16+
}: {
17+
mode: NodeJSCompatMode;
18+
unenvResolvePaths?: string[];
19+
}): Plugin[] {
1420
switch (mode) {
1521
case "als":
1622
return [asyncLocalStoragePlugin, nodejsCompatPlugin(mode)];
@@ -24,7 +30,7 @@ export function getNodeJSCompatPlugins(mode: NodeJSCompatMode): Plugin[] {
2430
case "v1":
2531
return [nodejsCompatPlugin(mode)];
2632
case "v2":
27-
return [nodejsHybridPlugin()];
33+
return [nodejsHybridPlugin(unenvResolvePaths)];
2834
case null:
2935
return [nodejsCompatPlugin(mode)];
3036
}

‎packages/wrangler/src/environment-variables/factory.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type VariableNames =
2525
| "WRANGLER_CI_MATCH_TAG"
2626
| "WRANGLER_BUILD_CONDITIONS"
2727
| "WRANGLER_BUILD_PLATFORM"
28+
| "WRANGLER_UNENV_RESOLVE_PATHS"
2829
| "WRANGLER_REGISTRY_PATH";
2930

3031
type DeprecatedNames =

‎packages/wrangler/src/environment-variables/misc-variables.ts

+13
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ export const getBuildPlatformFromEnv = getEnvironmentVariableFactory({
126126
variableName: "WRANGLER_BUILD_PLATFORM",
127127
});
128128

129+
/**
130+
* `WRANGLER_UNENV_RESOLVE_PATHS` lists the paths used to resolve unenv.
131+
*
132+
* Note: multiple comma separated paths can be specified.
133+
*
134+
* By default wrangler uses the unenv preset version installed from the package.json.
135+
*
136+
* Setting root paths allow to use a different version of the preset.
137+
*/
138+
export const getUnenvResolvePathsFromEnv = getEnvironmentVariableFactory({
139+
variableName: "WRANGLER_UNENV_RESOLVE_PATHS",
140+
});
141+
129142
/**
130143
* `WRANGLER_REGISTRY_PATH` specifies the file based dev registry folder
131144
*/

0 commit comments

Comments
 (0)
Please sign in to comment.