1
1
import { builtinModules } from "node:module" ;
2
2
import nodePath from "node:path" ;
3
3
import dedent from "ts-dedent" ;
4
- import { cloudflare , defineEnv } from "unenv" ;
4
+ import { cloudflare , env , nodeless } from "unenv" ;
5
5
import { getBasePath } from "../../paths" ;
6
6
import type { Plugin , PluginBuild } from "esbuild" ;
7
7
@@ -11,31 +11,17 @@ const REQUIRED_UNENV_ALIAS_NAMESPACE = "required-unenv-alias";
11
11
/**
12
12
* ESBuild plugin to apply the unenv preset.
13
13
*
14
- * @param unenvResolvePaths Root paths used to resolve absolute paths.
14
+ * @param _unenvResolvePaths Root paths used to resolve absolute paths.
15
15
* @returns ESBuild plugin
16
16
*/
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 ) ;
33
19
return {
34
20
name : "hybrid-nodejs_compat" ,
35
21
setup ( build ) {
36
22
errorOnServiceWorkerFormat ( build ) ;
37
23
handleRequireCallsToNodeJSBuiltins ( build ) ;
38
- handleUnenvAliasedPackages ( build , unresolvedAlias , alias , external ) ;
24
+ handleUnenvAliasedPackages ( build , alias , external ) ;
39
25
handleNodeJSGlobals ( build , inject ) ;
40
26
} ,
41
27
} ;
@@ -111,26 +97,38 @@ function handleRequireCallsToNodeJSBuiltins(build: PluginBuild) {
111
97
* Handles aliased NPM packages.
112
98
*
113
99
* @param build ESBuild PluginBuild.
114
- * @param unresolvedAlias Unresolved aliases from the presets.
115
100
* @param alias Aliases resolved to absolute paths.
116
101
* @param external external modules.
117
102
*/
118
103
function handleUnenvAliasedPackages (
119
104
build : PluginBuild ,
120
- unresolvedAlias : Record < string , string > ,
121
105
alias : Record < string , string > ,
122
106
external : string [ ]
123
107
) {
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
+ ) ;
125
123
126
124
build . onResolve ( { filter : UNENV_ALIAS_RE } , ( args ) => {
127
- const unresolved = unresolvedAlias [ args . path ] ;
125
+ const unresolvedAlias = alias [ args . path ] ;
128
126
// Convert `require()` calls for NPM packages to a virtual ES Module that can be imported avoiding the require calls.
129
127
// Note: Does not apply to Node.js packages that are handled in `handleRequireCallsToNodeJSBuiltins`
130
128
if (
131
129
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/" ) )
134
132
) {
135
133
return {
136
134
path : args . path ,
@@ -140,8 +138,8 @@ function handleUnenvAliasedPackages(
140
138
141
139
// Resolve the alias to its absolute path and potentially mark it as external
142
140
return {
143
- path : alias [ args . path ] ,
144
- external : external . includes ( unresolved ) ,
141
+ path : aliasAbsolute [ args . path ] ,
142
+ external : external . includes ( unresolvedAlias ) ,
145
143
} ;
146
144
} ) ;
147
145
0 commit comments