Skip to content

Commit ca9410a

Browse files
authored
chore(wrangler): update unenv dependency version (#7541)
1 parent 77bd9a1 commit ca9410a

File tree

6 files changed

+77
-32
lines changed

6 files changed

+77
-32
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+
chore(wrangler): update unenv dependency version

fixtures/nodejs-hybrid-app/tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe("nodejs compat", () => {
6464
}
6565
});
6666

67-
test("import unenv aliased packages", async ({ expect }) => {
67+
test("require unenv aliased packages", async ({ expect }) => {
6868
const { ip, port, stop } = await runWranglerDev(
6969
resolve(__dirname, "../src"),
7070
["--port=0", "--inspector-port=0"]

packages/wrangler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"resolve": "^1.22.8",
8484
"selfsigned": "^2.0.1",
8585
"source-map": "^0.6.1",
86-
"unenv": "npm:[email protected]20241204-140205-a5d5190",
86+
"unenv": "npm:[email protected]20241212-153011-af71c96",
8787
"workerd": "1.20241205.0",
8888
"xxhash-wasm": "^1.0.1"
8989
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10069,7 +10069,7 @@ export default{
1006910069
fs.writeFileSync(
1007010070
"index.js",
1007110071
`
10072-
import path from 'path';
10072+
import path from 'node:path';
1007310073
console.log(path);
1007410074
export default {}
1007510075
`
@@ -10090,7 +10090,7 @@ export default{
1009010090
}
1009110091
`);
1009210092
expect(fs.readFileSync("dist/index.js", { encoding: "utf-8" })).toContain(
10093-
`import path from "path";`
10093+
`import path from "node:path";`
1009410094
);
1009510095
});
1009610096

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

pnpm-lock.yaml

Lines changed: 37 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)