11import { builtinModules } from "node:module" ;
22import nodePath from "node:path" ;
33import dedent from "ts-dedent" ;
4- import { cloudflare , defineEnv } from "unenv" ;
4+ import { cloudflare , env , nodeless } from "unenv" ;
55import { getBasePath } from "../../paths" ;
66import type { Plugin , PluginBuild } from "esbuild" ;
77
@@ -11,31 +11,17 @@ const REQUIRED_UNENV_ALIAS_NAMESPACE = "required-unenv-alias";
1111/**
1212 * ESBuild plugin to apply the unenv preset.
1313 *
14- * @param unenvResolvePaths Root paths used to resolve absolute paths.
14+ * @param _unenvResolvePaths Root paths used to resolve absolute paths.
1515 * @returns ESBuild plugin
1616 */
17- export function nodejsHybridPlugin ( unenvResolvePaths ?: string [ ] ) : Plugin {
18- // Get the resolved environment.
19- const { env } = defineEnv ( {
20- nodeCompat : true ,
21- presets : [ cloudflare ] ,
22- resolve : {
23- paths : unenvResolvePaths ,
24- } ,
25- } ) ;
26- const { alias, inject, external } = env ;
27- // Get the unresolved alias.
28- const unresolvedAlias = defineEnv ( {
29- nodeCompat : true ,
30- presets : [ cloudflare ] ,
31- resolve : false ,
32- } ) . env . alias ;
17+ export function nodejsHybridPlugin ( _unenvResolvePaths ?: string [ ] ) : Plugin {
18+ const { alias, inject, external } = env ( nodeless , cloudflare ) ;
3319 return {
3420 name : "hybrid-nodejs_compat" ,
3521 setup ( build ) {
3622 errorOnServiceWorkerFormat ( build ) ;
3723 handleRequireCallsToNodeJSBuiltins ( build ) ;
38- handleUnenvAliasedPackages ( build , unresolvedAlias , alias , external ) ;
24+ handleUnenvAliasedPackages ( build , alias , external ) ;
3925 handleNodeJSGlobals ( build , inject ) ;
4026 } ,
4127 } ;
@@ -111,26 +97,38 @@ function handleRequireCallsToNodeJSBuiltins(build: PluginBuild) {
11197 * Handles aliased NPM packages.
11298 *
11399 * @param build ESBuild PluginBuild.
114- * @param unresolvedAlias Unresolved aliases from the presets.
115100 * @param alias Aliases resolved to absolute paths.
116101 * @param external external modules.
117102 */
118103function handleUnenvAliasedPackages (
119104 build : PluginBuild ,
120- unresolvedAlias : Record < string , string > ,
121105 alias : Record < string , string > ,
122106 external : string [ ]
123107) {
124- const UNENV_ALIAS_RE = new RegExp ( `^(${ Object . keys ( alias ) . join ( "|" ) } )$` ) ;
108+ // esbuild expects alias paths to be absolute
109+ const aliasAbsolute : Record < string , string > = { } ;
110+ for ( const [ module , unresolvedAlias ] of Object . entries ( alias ) ) {
111+ try {
112+ aliasAbsolute [ module ] = require
113+ . resolve ( unresolvedAlias )
114+ . replace ( / \. c j s $ / , ".mjs" ) ;
115+ } catch ( e ) {
116+ // this is an alias for package that is not installed in the current app => ignore
117+ }
118+ }
119+
120+ const UNENV_ALIAS_RE = new RegExp (
121+ `^(${ Object . keys ( aliasAbsolute ) . join ( "|" ) } )$`
122+ ) ;
125123
126124 build . onResolve ( { filter : UNENV_ALIAS_RE } , ( args ) => {
127- const unresolved = unresolvedAlias [ args . path ] ;
125+ const unresolvedAlias = alias [ args . path ] ;
128126 // Convert `require()` calls for NPM packages to a virtual ES Module that can be imported avoiding the require calls.
129127 // Note: Does not apply to Node.js packages that are handled in `handleRequireCallsToNodeJSBuiltins`
130128 if (
131129 args . kind === "require-call" &&
132- ( unresolved . startsWith ( "unenv/runtime/npm/" ) ||
133- unresolved . startsWith ( "unenv/runtime/mock/" ) )
130+ ( unresolvedAlias . startsWith ( "unenv/runtime/npm/" ) ||
131+ unresolvedAlias . startsWith ( "unenv/runtime/mock/" ) )
134132 ) {
135133 return {
136134 path : args . path ,
@@ -140,8 +138,8 @@ function handleUnenvAliasedPackages(
140138
141139 // Resolve the alias to its absolute path and potentially mark it as external
142140 return {
143- path : alias [ args . path ] ,
144- external : external . includes ( unresolved ) ,
141+ path : aliasAbsolute [ args . path ] ,
142+ external : external . includes ( unresolvedAlias ) ,
145143 } ;
146144 } ) ;
147145
0 commit comments