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

chore(wrangler): update unenv dependency version #7541

Merged
merged 2 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
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:unenv-nightly@2.0.0-20241204-140205-a5d5190",
"unenv": "npm:unenv-nightly@2.0.0-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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not required - the output is the same with or without the prefix.
Added only for consistency with l10093 below

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";`
Copy link
Contributor Author

@vicb vicb Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is required because unenv module resolution will always append the node: prefix

);
});

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/") ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using resolveAlias from pathe/utils (pathe is safe against windows paths when comparing and also handles multiple recursive aliases a => b => c)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure to understand how it would help here?
"unenv/runtime/npm/" comes from the preset alias.

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.

Loading