|
1 | 1 | import { builtinModules } from "node:module"; |
2 | 2 | import nodePath from "node:path"; |
3 | 3 | import dedent from "ts-dedent"; |
4 | | -import { cloudflare, env, nodeless } from "unenv"; |
| 4 | +import { cloudflare, defineEnv } from "unenv"; |
5 | 5 | import { getBasePath } from "../../paths"; |
6 | 6 | import type { Plugin, PluginBuild } from "esbuild"; |
7 | 7 |
|
8 | 8 | const REQUIRED_NODE_BUILT_IN_NAMESPACE = "node-built-in-modules"; |
9 | 9 | const REQUIRED_UNENV_ALIAS_NAMESPACE = "required-unenv-alias"; |
10 | 10 |
|
11 | 11 | export const nodejsHybridPlugin: () => Plugin = () => { |
12 | | - const { alias, inject, external } = env(nodeless, cloudflare); |
| 12 | + // Get the resolved environment. |
| 13 | + const { env } = defineEnv({ |
| 14 | + nodeCompat: true, |
| 15 | + presets: [cloudflare], |
| 16 | + resolve: true, |
| 17 | + }); |
| 18 | + const { alias, inject, external } = env; |
| 19 | + // Get the unresolved alias. |
| 20 | + const unresolvedAlias = defineEnv({ |
| 21 | + nodeCompat: true, |
| 22 | + presets: [cloudflare], |
| 23 | + resolve: false, |
| 24 | + }).env.alias; |
13 | 25 | return { |
14 | 26 | name: "hybrid-nodejs_compat", |
15 | 27 | setup(build) { |
16 | 28 | errorOnServiceWorkerFormat(build); |
17 | 29 | handleRequireCallsToNodeJSBuiltins(build); |
18 | | - handleUnenvAliasedPackages(build, alias, external); |
| 30 | + handleUnenvAliasedPackages(build, unresolvedAlias, alias, external); |
19 | 31 | handleNodeJSGlobals(build, inject); |
20 | 32 | }, |
21 | 33 | }; |
@@ -87,45 +99,41 @@ function handleRequireCallsToNodeJSBuiltins(build: PluginBuild) { |
87 | 99 | ); |
88 | 100 | } |
89 | 101 |
|
| 102 | +/** |
| 103 | + * Handles aliased NPM packages. |
| 104 | + * |
| 105 | + * @param build ESBuild PluginBuild. |
| 106 | + * @param unresolvedAlias Unresolved aliases from the presets. |
| 107 | + * @param alias Aliases resolved to absolute paths. |
| 108 | + * @param external external modules. |
| 109 | + */ |
90 | 110 | function handleUnenvAliasedPackages( |
91 | 111 | build: PluginBuild, |
| 112 | + unresolvedAlias: Record<string, string>, |
92 | 113 | alias: Record<string, string>, |
93 | 114 | external: string[] |
94 | 115 | ) { |
95 | | - // esbuild expects alias paths to be absolute |
96 | | - const aliasAbsolute: Record<string, string> = {}; |
97 | | - for (const [module, unresolvedAlias] of Object.entries(alias)) { |
98 | | - try { |
99 | | - aliasAbsolute[module] = require |
100 | | - .resolve(unresolvedAlias) |
101 | | - .replace(/\.cjs$/, ".mjs"); |
102 | | - } catch (e) { |
103 | | - // this is an alias for package that is not installed in the current app => ignore |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - const UNENV_ALIAS_RE = new RegExp( |
108 | | - `^(${Object.keys(aliasAbsolute).join("|")})$` |
109 | | - ); |
| 116 | + const UNENV_ALIAS_RE = new RegExp(`^(${Object.keys(alias).join("|")})$`); |
110 | 117 |
|
111 | 118 | build.onResolve({ filter: UNENV_ALIAS_RE }, (args) => { |
112 | | - const unresolvedAlias = alias[args.path]; |
| 119 | + const unresolved = unresolvedAlias[args.path]; |
113 | 120 | // Convert `require()` calls for NPM packages to a virtual ES Module that can be imported avoiding the require calls. |
114 | 121 | // Note: Does not apply to Node.js packages that are handled in `handleRequireCallsToNodeJSBuiltins` |
115 | 122 | if ( |
116 | 123 | args.kind === "require-call" && |
117 | | - (unresolvedAlias.startsWith("unenv/runtime/npm/") || |
118 | | - unresolvedAlias.startsWith("unenv/runtime/mock/")) |
| 124 | + (unresolved.startsWith("unenv/runtime/npm/") || |
| 125 | + unresolved.startsWith("unenv/runtime/mock/")) |
119 | 126 | ) { |
120 | 127 | return { |
121 | 128 | path: args.path, |
122 | 129 | namespace: REQUIRED_UNENV_ALIAS_NAMESPACE, |
123 | 130 | }; |
124 | 131 | } |
| 132 | + |
125 | 133 | // Resolve the alias to its absolute path and potentially mark it as external |
126 | 134 | return { |
127 | | - path: aliasAbsolute[args.path], |
128 | | - external: external.includes(unresolvedAlias), |
| 135 | + path: alias[args.path], |
| 136 | + external: external.includes(unresolved), |
129 | 137 | }; |
130 | 138 | }); |
131 | 139 |
|
|
0 commit comments