Skip to content

Commit

Permalink
feat: add support for custom unenv resolve path (#7522)
Browse files Browse the repository at this point in the history
feat(wrangler): add support for custom unenv resolve path

Introduce `WRANGLER_UNENV_RESOLVE_PATHS` to
specify resolve paths for unenv.

Co-authored-by: Pete Bacon Darwin <[email protected]>
  • Loading branch information
vicb and petebacondarwin authored Dec 17, 2024
1 parent 01b5451 commit 6403e41
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
11 changes: 11 additions & 0 deletions .changeset/fluffy-eggs-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": minor
---

feat(wrangler): allow overriding the unenv preset.

By default wrangler uses the bundled unenv preset.

Setting `WRANGLER_UNENV_RESOLVE_PATHS` allow to use another version of the preset.
Those paths are used when resolving the unenv module identifiers to absolute paths.
This can be used to test a development version.
8 changes: 7 additions & 1 deletion packages/wrangler/src/deployment-bundle/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as esbuild from "esbuild";
import {
getBuildConditionsFromEnv,
getBuildPlatformFromEnv,
getUnenvResolvePathsFromEnv,
} from "../environment-variables/misc-variables";
import { UserError } from "../errors";
import { getFlag } from "../experimental-flags";
Expand Down Expand Up @@ -390,6 +391,8 @@ export async function bundleWorker(
},
};

const unenvResolvePaths = getUnenvResolvePathsFromEnv()?.split(",");

const buildOptions: esbuild.BuildOptions & { metafile: true } = {
// Don't use entryFile here as the file may have been changed when applying the middleware
entryPoints: [entry.file],
Expand Down Expand Up @@ -435,7 +438,10 @@ export async function bundleWorker(
plugins: [
aliasPlugin,
moduleCollector.plugin,
...getNodeJSCompatPlugins(nodejsCompatMode ?? null),
...getNodeJSCompatPlugins({
mode: nodejsCompatMode ?? null,
unenvResolvePaths,
}),
cloudflareInternalPlugin,
buildResultPlugin,
...(plugins || []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import type { Plugin, PluginBuild } from "esbuild";
const REQUIRED_NODE_BUILT_IN_NAMESPACE = "node-built-in-modules";
const REQUIRED_UNENV_ALIAS_NAMESPACE = "required-unenv-alias";

export const nodejsHybridPlugin: () => Plugin = () => {
/**
* ESBuild plugin to apply the unenv preset.
*
* @param unenvResolvePaths Root paths used to resolve absolute paths.
* @returns ESBuild plugin
*/
export function nodejsHybridPlugin(unenvResolvePaths?: string[]): Plugin {
// Get the resolved environment.
const { env } = defineEnv({
nodeCompat: true,
presets: [cloudflare],
resolve: true,
resolve: {
paths: unenvResolvePaths,
},
});
const { alias, inject, external } = env;
// Get the unresolved alias.
Expand All @@ -31,7 +39,7 @@ export const nodejsHybridPlugin: () => Plugin = () => {
handleNodeJSGlobals(build, inject);
},
};
};
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import type { NodeJSCompatMode } from "miniflare";
/**
* Returns the list of ESBuild plugins to use for a given compat mode.
*/
export function getNodeJSCompatPlugins(mode: NodeJSCompatMode): Plugin[] {
export function getNodeJSCompatPlugins({
mode,
unenvResolvePaths,
}: {
mode: NodeJSCompatMode;
unenvResolvePaths?: string[];
}): Plugin[] {
switch (mode) {
case "als":
return [asyncLocalStoragePlugin, nodejsCompatPlugin(mode)];
Expand All @@ -24,7 +30,7 @@ export function getNodeJSCompatPlugins(mode: NodeJSCompatMode): Plugin[] {
case "v1":
return [nodejsCompatPlugin(mode)];
case "v2":
return [nodejsHybridPlugin()];
return [nodejsHybridPlugin(unenvResolvePaths)];
case null:
return [nodejsCompatPlugin(mode)];
}
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/environment-variables/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type VariableNames =
| "WRANGLER_CI_MATCH_TAG"
| "WRANGLER_BUILD_CONDITIONS"
| "WRANGLER_BUILD_PLATFORM"
| "WRANGLER_UNENV_RESOLVE_PATHS"
| "WRANGLER_REGISTRY_PATH";

type DeprecatedNames =
Expand Down
13 changes: 13 additions & 0 deletions packages/wrangler/src/environment-variables/misc-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ export const getBuildPlatformFromEnv = getEnvironmentVariableFactory({
variableName: "WRANGLER_BUILD_PLATFORM",
});

/**
* `WRANGLER_UNENV_RESOLVE_PATHS` lists the paths used to resolve unenv.
*
* Note: multiple comma separated paths can be specified.
*
* By default wrangler uses the unenv preset version installed from the package.json.
*
* Setting root paths allow to use a different version of the preset.
*/
export const getUnenvResolvePathsFromEnv = getEnvironmentVariableFactory({
variableName: "WRANGLER_UNENV_RESOLVE_PATHS",
});

/**
* `WRANGLER_REGISTRY_PATH` specifies the file based dev registry folder
*/
Expand Down

0 comments on commit 6403e41

Please sign in to comment.