Skip to content

Commit 57fc660

Browse files
authored
allow resolving unenv aliases to packages (#11083)
1 parent a5eb513 commit 57fc660

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

.changeset/petite-rules-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
allow resolving unenv aliases to packages

packages/vite-plugin-cloudflare/src/plugins/nodejs-compat.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,24 @@ export class NodeJsCompat {
403403
): { unresolved: string; resolved: string } | undefined {
404404
const alias = this.#env.alias[source];
405405

406-
// These aliases must be resolved from the context of this plugin since the alias will refer to one of the
407-
// `@cloudflare/unenv-preset` or the `unenv` packages, which are direct dependencies of this package,
408-
// and not the user's project.
409406
// We exclude `externals` as these should be externalized rather than optimized.
410407
if (alias && !this.externals.has(alias)) {
411-
return {
412-
unresolved: alias,
413-
resolved: resolvePathSync(alias, { url: import.meta.url }),
414-
};
408+
try {
409+
// These aliases must be resolved from the context of this plugin since the alias can refer to one of
410+
// the `@cloudflare/unenv-preset` or the `unenv` packages, which are direct dependencies of this package,
411+
// and not the user's project.
412+
// When the alias is a package (i.e. the `debug` npm package), `resolvePathSync` will throw
413+
return {
414+
unresolved: alias,
415+
resolved: resolvePathSync(alias, { url: import.meta.url }),
416+
};
417+
} catch {
418+
// Returns the raw alias when it can not be resolved.
419+
return {
420+
unresolved: source,
421+
resolved: alias,
422+
};
423+
}
415424
}
416425

417426
if (this.entries.has(source)) {

0 commit comments

Comments
 (0)