Skip to content

Commit

Permalink
chore(wrangler): update unenv dependency version (#7541)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored Dec 17, 2024
1 parent 77bd9a1 commit ca9410a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-needles-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore(wrangler): update unenv dependency version
2 changes: 1 addition & 1 deletion fixtures/nodejs-hybrid-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("nodejs compat", () => {
}
});

test("import unenv aliased packages", async ({ expect }) => {
test("require unenv aliased packages", async ({ expect }) => {
const { ip, port, stop } = await runWranglerDev(
resolve(__dirname, "../src"),
["--port=0", "--inspector-port=0"]
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"resolve": "^1.22.8",
"selfsigned": "^2.0.1",
"source-map": "^0.6.1",
"unenv": "npm:[email protected]20241204-140205-a5d5190",
"unenv": "npm:[email protected]20241212-153011-af71c96",
"workerd": "1.20241205.0",
"xxhash-wasm": "^1.0.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10069,7 +10069,7 @@ export default{
fs.writeFileSync(
"index.js",
`
import path from 'path';
import path from 'node:path';
console.log(path);
export default {}
`
Expand All @@ -10090,7 +10090,7 @@ export default{
}
`);
expect(fs.readFileSync("dist/index.js", { encoding: "utf-8" })).toContain(
`import path from "path";`
`import path from "node:path";`
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { builtinModules } from "node:module";
import nodePath from "node:path";
import dedent from "ts-dedent";
import { cloudflare, env, nodeless } from "unenv";
import { cloudflare, defineEnv } from "unenv";
import { getBasePath } from "../../paths";
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 = () => {
const { alias, inject, external } = env(nodeless, cloudflare);
// Get the resolved environment.
const { env } = defineEnv({
nodeCompat: true,
presets: [cloudflare],
resolve: true,
});
const { alias, inject, external } = env;
// Get the unresolved alias.
const unresolvedAlias = defineEnv({
nodeCompat: true,
presets: [cloudflare],
resolve: false,
}).env.alias;
return {
name: "hybrid-nodejs_compat",
setup(build) {
errorOnServiceWorkerFormat(build);
handleRequireCallsToNodeJSBuiltins(build);
handleUnenvAliasedPackages(build, alias, external);
handleUnenvAliasedPackages(build, unresolvedAlias, alias, external);
handleNodeJSGlobals(build, inject);
},
};
Expand Down Expand Up @@ -87,45 +99,41 @@ function handleRequireCallsToNodeJSBuiltins(build: PluginBuild) {
);
}

/**
* Handles aliased NPM packages.
*
* @param build ESBuild PluginBuild.
* @param unresolvedAlias Unresolved aliases from the presets.
* @param alias Aliases resolved to absolute paths.
* @param external external modules.
*/
function handleUnenvAliasedPackages(
build: PluginBuild,
unresolvedAlias: Record<string, string>,
alias: Record<string, string>,
external: string[]
) {
// esbuild expects alias paths to be absolute
const aliasAbsolute: Record<string, string> = {};
for (const [module, unresolvedAlias] of Object.entries(alias)) {
try {
aliasAbsolute[module] = require
.resolve(unresolvedAlias)
.replace(/\.cjs$/, ".mjs");
} catch (e) {
// this is an alias for package that is not installed in the current app => ignore
}
}

const UNENV_ALIAS_RE = new RegExp(
`^(${Object.keys(aliasAbsolute).join("|")})$`
);
const UNENV_ALIAS_RE = new RegExp(`^(${Object.keys(alias).join("|")})$`);

build.onResolve({ filter: UNENV_ALIAS_RE }, (args) => {
const unresolvedAlias = alias[args.path];
const unresolved = unresolvedAlias[args.path];
// Convert `require()` calls for NPM packages to a virtual ES Module that can be imported avoiding the require calls.
// Note: Does not apply to Node.js packages that are handled in `handleRequireCallsToNodeJSBuiltins`
if (
args.kind === "require-call" &&
(unresolvedAlias.startsWith("unenv/runtime/npm/") ||
unresolvedAlias.startsWith("unenv/runtime/mock/"))
(unresolved.startsWith("unenv/runtime/npm/") ||
unresolved.startsWith("unenv/runtime/mock/"))
) {
return {
path: args.path,
namespace: REQUIRED_UNENV_ALIAS_NAMESPACE,
};
}

// Resolve the alias to its absolute path and potentially mark it as external
return {
path: aliasAbsolute[args.path],
external: external.includes(unresolvedAlias),
path: alias[args.path],
external: external.includes(unresolved),
};
});

Expand Down
42 changes: 37 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ca9410a

Please sign in to comment.