Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for custom unenv resolve path #7522

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading