-
Notifications
You must be signed in to change notification settings - Fork 787
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
chore(wrangler): update unenv dependency version |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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";` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is required because unenv module resolution will always append the |
||
); | ||
}); | ||
|
||
|
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], | ||
vicb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolve: true, | ||
}); | ||
const { alias, inject, external } = env; | ||
// Get the unresolved alias. | ||
const unresolvedAlias = defineEnv({ | ||
nodeCompat: true, | ||
presets: [cloudflare], | ||
resolve: false, | ||
}).env.alias; | ||
vicb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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<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/") || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure to understand how it would help here? |
||
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), | ||
}; | ||
}); | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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