From ca9410a4f92a61abc0d759a3abe29da05cedeaed Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 17 Dec 2024 08:10:33 +0100 Subject: [PATCH] chore(wrangler): update unenv dependency version (#7541) --- .changeset/chilled-needles-notice.md | 5 ++ .../nodejs-hybrid-app/tests/index.test.ts | 2 +- packages/wrangler/package.json | 2 +- .../wrangler/src/__tests__/deploy.test.ts | 4 +- .../esbuild-plugins/hybrid-nodejs-compat.ts | 54 +++++++++++-------- pnpm-lock.yaml | 42 +++++++++++++-- 6 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 .changeset/chilled-needles-notice.md diff --git a/.changeset/chilled-needles-notice.md b/.changeset/chilled-needles-notice.md new file mode 100644 index 000000000000..8fa075946124 --- /dev/null +++ b/.changeset/chilled-needles-notice.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +chore(wrangler): update unenv dependency version diff --git a/fixtures/nodejs-hybrid-app/tests/index.test.ts b/fixtures/nodejs-hybrid-app/tests/index.test.ts index fb46992c03c9..da7e2f26f0ec 100644 --- a/fixtures/nodejs-hybrid-app/tests/index.test.ts +++ b/fixtures/nodejs-hybrid-app/tests/index.test.ts @@ -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"] diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 4888458bbcb0..d14b5c8e0ed7 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -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" }, diff --git a/packages/wrangler/src/__tests__/deploy.test.ts b/packages/wrangler/src/__tests__/deploy.test.ts index 25baf80ae0a4..263aeec98eef 100644 --- a/packages/wrangler/src/__tests__/deploy.test.ts +++ b/packages/wrangler/src/__tests__/deploy.test.ts @@ -10069,7 +10069,7 @@ export default{ fs.writeFileSync( "index.js", ` - import path from 'path'; + import path from 'node:path'; console.log(path); export default {} ` @@ -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";` ); }); diff --git a/packages/wrangler/src/deployment-bundle/esbuild-plugins/hybrid-nodejs-compat.ts b/packages/wrangler/src/deployment-bundle/esbuild-plugins/hybrid-nodejs-compat.ts index e5a6ff9960f8..55a2fbcea2c7 100644 --- a/packages/wrangler/src/deployment-bundle/esbuild-plugins/hybrid-nodejs-compat.ts +++ b/packages/wrangler/src/deployment-bundle/esbuild-plugins/hybrid-nodejs-compat.ts @@ -1,7 +1,7 @@ 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"; @@ -9,13 +9,25 @@ 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); }, }; @@ -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, alias: Record, external: string[] ) { - // esbuild expects alias paths to be absolute - const aliasAbsolute: Record = {}; - 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), }; }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 780bc063f8a9..7cda8af2148f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1786,8 +1786,8 @@ importers: specifier: ^0.6.1 version: 0.6.1 unenv: - specifier: npm:unenv-nightly@2.0.0-20241204-140205-a5d5190 - version: unenv-nightly@2.0.0-20241204-140205-a5d5190 + specifier: npm:unenv-nightly@2.0.0-20241212-153011-af71c96 + version: unenv-nightly@2.0.0-20241212-153011-af71c96 workerd: specifier: 1.20241205.0 version: 1.20241205.0 @@ -4276,6 +4276,11 @@ packages: engines: {node: '>=0.4.0'} hasBin: true + acorn@8.14.0: + resolution: {integrity: sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA==} + engines: {node: '>=0.4.0'} + hasBin: true + agent-base@6.0.2: resolution: {integrity: sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==} engines: {node: '>= 6.0.0'} @@ -4907,6 +4912,9 @@ packages: engines: {node: ^14.13.0 || >=16.0.0} hasBin: true + confbox@0.1.8: + resolution: {integrity: sha512-RMtmw0iFkeR4YV+fUOSucriAQNb9g8zFR52MWCtl+cCZOFRNL6zeB395vPzFhEjjn4fMxXudmELnl/KF/WrK6w==} + config-chain@1.1.13: resolution: {integrity: sha512-qj+f8APARXHrM0hraqXYb2/bOVSV4PvJQlNZ/DVj0QrmNM2q2euizkeuVckQ57J+W0mRH6Hvi+k50M4Jul2VRQ==} @@ -7077,6 +7085,9 @@ packages: engines: {node: '>= 14.0.0'} hasBin: true + mlly@1.7.3: + resolution: {integrity: sha512-xUsx5n/mN0uQf4V548PKQ+YShA4/IW0KI1dZhrNrPCLG+xizETbHTkOa1f8/xut9JRPp8kQuMnz0oqwkTiLo/A==} + mock-socket@9.3.1: resolution: {integrity: sha512-qxBgB7Qa2sEQgHFjj0dSigq7fX4k6Saisd5Nelwp2q8mlbAFh5dHV9JTTlF8viYJLSSWgMCZFUom8PJcMNBoJw==} engines: {node: '>= 8'} @@ -7646,6 +7657,9 @@ packages: pkg-types@1.0.3: resolution: {integrity: sha512-nN7pYi0AQqJnoLPC9eHFQ8AcyaixBUOwvqc5TDnIKCMEE6I0y8P7OKA7fPexsXGCGxQDl/cmrLAp26LhcwxZ4A==} + pkg-types@1.2.1: + resolution: {integrity: sha512-sQoqa8alT3nHjGuTjuKgOnvjo4cljkufdtLMnO2LBP/wRwuDlo1tkaEdMxCRhyGRPacv/ztlZgDPm2b7FAmEvw==} + plur@5.1.0: resolution: {integrity: sha512-VP/72JeXqak2KiOzjgKtQen5y3IZHn+9GOuLDafPv0eXa47xq0At93XahYBs26MsifCQ4enGKwbjBTKgb9QJXg==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} @@ -8925,8 +8939,8 @@ packages: unenv-nightly@2.0.0-20241111-080453-894aa31: resolution: {integrity: sha512-0W39QQOQ9VE8kVVUpGwEG+pZcsCXk5wqNG6rDPE6Gr+fiA69LR0qERM61hW5KCOkC1/ArCFrfCGjwHyyv/bI0Q==} - unenv-nightly@2.0.0-20241204-140205-a5d5190: - resolution: {integrity: sha512-jpmAytLeiiW01pl5bhVn9wYJ4vtiLdhGe10oXlJBuQEX8mxjxO8BlEXGHU4vr4yEikjFP1wsomTHt/CLU8kUwg==} + unenv-nightly@2.0.0-20241212-153011-af71c96: + resolution: {integrity: sha512-Yugb9yPs/EZsPOY+IHloqVVEcZeJ0uwwViTedsZjOtVeYO8I29B1rzU/p84FMT0R1Ht3bHsKkNV/rzrjSd07QA==} unicorn-magic@0.1.0: resolution: {integrity: sha512-lRfVq8fE8gz6QMBuDM6a+LO3IAzTi05H6gCVaUpir2E1Rwpo4ZUog45KpNXKC/Mn3Yb9UDuHumeFTo9iV/D9FQ==} @@ -11978,6 +11992,8 @@ snapshots: transitivePeerDependencies: - supports-color + acorn@8.14.0: {} + agent-base@6.0.2(supports-color@9.2.2): dependencies: debug: 4.3.7(supports-color@9.2.2) @@ -12767,6 +12783,8 @@ snapshots: tree-kill: 1.2.2 yargs: 17.7.2 + confbox@0.1.8: {} + config-chain@1.1.13: dependencies: ini: 1.3.8 @@ -15237,6 +15255,13 @@ snapshots: yargs-parser: 20.2.9 yargs-unparser: 2.0.0 + mlly@1.7.3: + dependencies: + acorn: 8.14.0 + pathe: 1.1.2 + pkg-types: 1.2.1 + ufo: 1.5.4 + mock-socket@9.3.1: {} mrmime@2.0.0: {} @@ -15838,6 +15863,12 @@ snapshots: mlly: 1.4.2 pathe: 1.1.2 + pkg-types@1.2.1: + dependencies: + confbox: 0.1.8 + mlly: 1.7.3 + pathe: 1.1.2 + plur@5.1.0: dependencies: irregular-plurals: 3.5.0 @@ -17213,9 +17244,10 @@ snapshots: pathe: 1.1.2 ufo: 1.5.4 - unenv-nightly@2.0.0-20241204-140205-a5d5190: + unenv-nightly@2.0.0-20241212-153011-af71c96: dependencies: defu: 6.1.4 + mlly: 1.7.3 ohash: 1.1.4 pathe: 1.1.2 ufo: 1.5.4