Skip to content

Commit d8fb032

Browse files
authored
feat(wrangler): use unenv bulitin dependency resolution (#7625)
1 parent 94f650e commit d8fb032

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feat(wrangler): use unenv bulitin dependency resolution

packages/wrangler/src/__tests__/deploy.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10420,7 +10420,7 @@ export default{
1042010420
fs.writeFileSync(
1042110421
"index.js",
1042210422
`
10423-
import path from 'path';
10423+
import path from 'node:path';
1042410424
console.log(path);
1042510425
export default {}
1042610426
`
@@ -10441,7 +10441,7 @@ export default{
1044110441
}
1044210442
`);
1044310443
expect(fs.readFileSync("dist/index.js", { encoding: "utf-8" })).toContain(
10444-
`import path from "path";`
10444+
`import path from "node:path";`
1044510445
);
1044610446
});
1044710447

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
import { builtinModules } from "node:module";
22
import nodePath from "node:path";
33
import dedent from "ts-dedent";
4-
import { cloudflare, env, nodeless } from "unenv";
4+
import { cloudflare, defineEnv } from "unenv";
55
import { getBasePath } from "../../paths";
66
import type { Plugin, PluginBuild } from "esbuild";
77

88
const REQUIRED_NODE_BUILT_IN_NAMESPACE = "node-built-in-modules";
99
const REQUIRED_UNENV_ALIAS_NAMESPACE = "required-unenv-alias";
1010

1111
export const nodejsHybridPlugin: () => Plugin = () => {
12-
const { alias, inject, external } = env(nodeless, cloudflare);
12+
// Get the resolved environment.
13+
const { env } = defineEnv({
14+
nodeCompat: true,
15+
presets: [cloudflare],
16+
resolve: true,
17+
});
18+
const { alias, inject, external } = env;
19+
// Get the unresolved alias.
20+
const unresolvedAlias = defineEnv({
21+
nodeCompat: true,
22+
presets: [cloudflare],
23+
resolve: false,
24+
}).env.alias;
1325
return {
1426
name: "hybrid-nodejs_compat",
1527
setup(build) {
1628
errorOnServiceWorkerFormat(build);
1729
handleRequireCallsToNodeJSBuiltins(build);
18-
handleUnenvAliasedPackages(build, alias, external);
30+
handleUnenvAliasedPackages(build, unresolvedAlias, alias, external);
1931
handleNodeJSGlobals(build, inject);
2032
},
2133
};
@@ -87,45 +99,41 @@ function handleRequireCallsToNodeJSBuiltins(build: PluginBuild) {
8799
);
88100
}
89101

102+
/**
103+
* Handles aliased NPM packages.
104+
*
105+
* @param build ESBuild PluginBuild.
106+
* @param unresolvedAlias Unresolved aliases from the presets.
107+
* @param alias Aliases resolved to absolute paths.
108+
* @param external external modules.
109+
*/
90110
function handleUnenvAliasedPackages(
91111
build: PluginBuild,
112+
unresolvedAlias: Record<string, string>,
92113
alias: Record<string, string>,
93114
external: string[]
94115
) {
95-
// esbuild expects alias paths to be absolute
96-
const aliasAbsolute: Record<string, string> = {};
97-
for (const [module, unresolvedAlias] of Object.entries(alias)) {
98-
try {
99-
aliasAbsolute[module] = require
100-
.resolve(unresolvedAlias)
101-
.replace(/\.cjs$/, ".mjs");
102-
} catch (e) {
103-
// this is an alias for package that is not installed in the current app => ignore
104-
}
105-
}
106-
107-
const UNENV_ALIAS_RE = new RegExp(
108-
`^(${Object.keys(aliasAbsolute).join("|")})$`
109-
);
116+
const UNENV_ALIAS_RE = new RegExp(`^(${Object.keys(alias).join("|")})$`);
110117

111118
build.onResolve({ filter: UNENV_ALIAS_RE }, (args) => {
112-
const unresolvedAlias = alias[args.path];
119+
const unresolved = unresolvedAlias[args.path];
113120
// Convert `require()` calls for NPM packages to a virtual ES Module that can be imported avoiding the require calls.
114121
// Note: Does not apply to Node.js packages that are handled in `handleRequireCallsToNodeJSBuiltins`
115122
if (
116123
args.kind === "require-call" &&
117-
(unresolvedAlias.startsWith("unenv/runtime/npm/") ||
118-
unresolvedAlias.startsWith("unenv/runtime/mock/"))
124+
(unresolved.startsWith("unenv/runtime/npm/") ||
125+
unresolved.startsWith("unenv/runtime/mock/"))
119126
) {
120127
return {
121128
path: args.path,
122129
namespace: REQUIRED_UNENV_ALIAS_NAMESPACE,
123130
};
124131
}
132+
125133
// Resolve the alias to its absolute path and potentially mark it as external
126134
return {
127-
path: aliasAbsolute[args.path],
128-
external: external.includes(unresolvedAlias),
135+
path: alias[args.path],
136+
external: external.includes(unresolved),
129137
};
130138
});
131139

0 commit comments

Comments
 (0)