@jsenv/plugin-supervisor 1.6.11 → 1.6.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,319 +1,6 @@
|
|
|
1
1
|
import { applyBabelPlugins, parseHtml, visitHtmlNodes, analyzeScriptNode, getHtmlNodeAttribute, getHtmlNodeText, injectJsenvScript, stringifyHtmlAst, getHtmlNodePosition, getUrlForContentInsideHtml, setHtmlNodeText, setHtmlNodeAttributes } from "@jsenv/ast";
|
|
2
|
-
import process$1 from "node:process";
|
|
3
|
-
import os from "node:os";
|
|
4
|
-
import tty from "node:tty";
|
|
5
2
|
import { createRequire } from "node:module";
|
|
6
|
-
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
7
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
8
3
|
|
|
9
|
-
// From: https://github.com/sindresorhus/has-flag/blob/main/index.js
|
|
10
|
-
/// function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) {
|
|
11
|
-
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process$1.argv) {
|
|
12
|
-
const prefix = flag.startsWith('-') ? '' : flag.length === 1 ? '-' : '--';
|
|
13
|
-
const position = argv.indexOf(prefix + flag);
|
|
14
|
-
const terminatorPosition = argv.indexOf('--');
|
|
15
|
-
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
|
16
|
-
}
|
|
17
|
-
const {
|
|
18
|
-
env
|
|
19
|
-
} = process$1;
|
|
20
|
-
let flagForceColor;
|
|
21
|
-
if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false') || hasFlag('color=never')) {
|
|
22
|
-
flagForceColor = 0;
|
|
23
|
-
} else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) {
|
|
24
|
-
flagForceColor = 1;
|
|
25
|
-
}
|
|
26
|
-
function envForceColor() {
|
|
27
|
-
if (!('FORCE_COLOR' in env)) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
if (env.FORCE_COLOR === 'true') {
|
|
31
|
-
return 1;
|
|
32
|
-
}
|
|
33
|
-
if (env.FORCE_COLOR === 'false') {
|
|
34
|
-
return 0;
|
|
35
|
-
}
|
|
36
|
-
if (env.FORCE_COLOR.length === 0) {
|
|
37
|
-
return 1;
|
|
38
|
-
}
|
|
39
|
-
const level = Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
|
|
40
|
-
if (![0, 1, 2, 3].includes(level)) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
return level;
|
|
44
|
-
}
|
|
45
|
-
function translateLevel(level) {
|
|
46
|
-
if (level === 0) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
level,
|
|
51
|
-
hasBasic: true,
|
|
52
|
-
has256: level >= 2,
|
|
53
|
-
has16m: level >= 3
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function _supportsColor(haveStream, {
|
|
57
|
-
streamIsTTY,
|
|
58
|
-
sniffFlags = true
|
|
59
|
-
} = {}) {
|
|
60
|
-
const noFlagForceColor = envForceColor();
|
|
61
|
-
if (noFlagForceColor !== undefined) {
|
|
62
|
-
flagForceColor = noFlagForceColor;
|
|
63
|
-
}
|
|
64
|
-
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
65
|
-
if (forceColor === 0) {
|
|
66
|
-
return 0;
|
|
67
|
-
}
|
|
68
|
-
if (sniffFlags) {
|
|
69
|
-
if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) {
|
|
70
|
-
return 3;
|
|
71
|
-
}
|
|
72
|
-
if (hasFlag('color=256')) {
|
|
73
|
-
return 2;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Check for Azure DevOps pipelines.
|
|
78
|
-
// Has to be above the `!streamIsTTY` check.
|
|
79
|
-
if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
|
|
80
|
-
return 1;
|
|
81
|
-
}
|
|
82
|
-
if (haveStream && !streamIsTTY && forceColor === undefined) {
|
|
83
|
-
return 0;
|
|
84
|
-
}
|
|
85
|
-
const min = forceColor || 0;
|
|
86
|
-
if (env.TERM === 'dumb') {
|
|
87
|
-
return min;
|
|
88
|
-
}
|
|
89
|
-
if (process$1.platform === 'win32') {
|
|
90
|
-
// Windows 10 build 10586 is the first Windows release that supports 256 colors.
|
|
91
|
-
// Windows 10 build 14931 is the first release that supports 16m/TrueColor.
|
|
92
|
-
const osRelease = os.release().split('.');
|
|
93
|
-
if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
|
|
94
|
-
return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
|
95
|
-
}
|
|
96
|
-
return 1;
|
|
97
|
-
}
|
|
98
|
-
if ('CI' in env) {
|
|
99
|
-
if (['GITHUB_ACTIONS', 'GITEA_ACTIONS', 'CIRCLECI'].some(key => key in env)) {
|
|
100
|
-
return 3;
|
|
101
|
-
}
|
|
102
|
-
if (['TRAVIS', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
|
|
103
|
-
return 1;
|
|
104
|
-
}
|
|
105
|
-
return min;
|
|
106
|
-
}
|
|
107
|
-
if ('TEAMCITY_VERSION' in env) {
|
|
108
|
-
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
|
109
|
-
}
|
|
110
|
-
if (env.COLORTERM === 'truecolor') {
|
|
111
|
-
return 3;
|
|
112
|
-
}
|
|
113
|
-
if (env.TERM === 'xterm-kitty') {
|
|
114
|
-
return 3;
|
|
115
|
-
}
|
|
116
|
-
if ('TERM_PROGRAM' in env) {
|
|
117
|
-
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
|
|
118
|
-
switch (env.TERM_PROGRAM) {
|
|
119
|
-
case 'iTerm.app':
|
|
120
|
-
{
|
|
121
|
-
return version >= 3 ? 3 : 2;
|
|
122
|
-
}
|
|
123
|
-
case 'Apple_Terminal':
|
|
124
|
-
{
|
|
125
|
-
return 2;
|
|
126
|
-
}
|
|
127
|
-
// No default
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (/-256(color)?$/i.test(env.TERM)) {
|
|
131
|
-
return 2;
|
|
132
|
-
}
|
|
133
|
-
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
|
|
134
|
-
return 1;
|
|
135
|
-
}
|
|
136
|
-
if ('COLORTERM' in env) {
|
|
137
|
-
return 1;
|
|
138
|
-
}
|
|
139
|
-
return min;
|
|
140
|
-
}
|
|
141
|
-
function createSupportsColor(stream, options = {}) {
|
|
142
|
-
const level = _supportsColor(stream, {
|
|
143
|
-
streamIsTTY: stream && stream.isTTY,
|
|
144
|
-
...options
|
|
145
|
-
});
|
|
146
|
-
return translateLevel(level);
|
|
147
|
-
}
|
|
148
|
-
({
|
|
149
|
-
stdout: createSupportsColor({
|
|
150
|
-
isTTY: tty.isatty(1)
|
|
151
|
-
}),
|
|
152
|
-
stderr: createSupportsColor({
|
|
153
|
-
isTTY: tty.isatty(2)
|
|
154
|
-
})
|
|
155
|
-
});
|
|
156
|
-
function isUnicodeSupported() {
|
|
157
|
-
const {
|
|
158
|
-
env
|
|
159
|
-
} = process$1;
|
|
160
|
-
const {
|
|
161
|
-
TERM,
|
|
162
|
-
TERM_PROGRAM
|
|
163
|
-
} = env;
|
|
164
|
-
if (process$1.platform !== 'win32') {
|
|
165
|
-
return TERM !== 'linux'; // Linux console (kernel)
|
|
166
|
-
}
|
|
167
|
-
return Boolean(env.WT_SESSION) // Windows Terminal
|
|
168
|
-
|| Boolean(env.TERMINUS_SUBLIME) // Terminus (<0.2.27)
|
|
169
|
-
|| env.ConEmuTask === '{cmd::Cmder}' // ConEmu and cmder
|
|
170
|
-
|| TERM_PROGRAM === 'Terminus-Sublime' || TERM_PROGRAM === 'vscode' || TERM === 'xterm-256color' || TERM === 'alacritty' || TERM === 'rxvt-unicode' || TERM === 'rxvt-unicode-256color' || env.TERMINAL_EMULATOR === 'JetBrains-JediTerm';
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/* globals WorkerGlobalScope, DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, ServiceWorkerGlobalScope */
|
|
174
|
-
|
|
175
|
-
const isBrowser = globalThis.window?.document !== undefined;
|
|
176
|
-
globalThis.process?.versions?.node !== undefined;
|
|
177
|
-
globalThis.process?.versions?.bun !== undefined;
|
|
178
|
-
globalThis.Deno?.version?.deno !== undefined;
|
|
179
|
-
globalThis.process?.versions?.electron !== undefined;
|
|
180
|
-
globalThis.navigator?.userAgent?.includes('jsdom') === true;
|
|
181
|
-
typeof WorkerGlobalScope !== 'undefined' && globalThis instanceof WorkerGlobalScope;
|
|
182
|
-
typeof DedicatedWorkerGlobalScope !== 'undefined' && globalThis instanceof DedicatedWorkerGlobalScope;
|
|
183
|
-
typeof SharedWorkerGlobalScope !== 'undefined' && globalThis instanceof SharedWorkerGlobalScope;
|
|
184
|
-
typeof ServiceWorkerGlobalScope !== 'undefined' && globalThis instanceof ServiceWorkerGlobalScope;
|
|
185
|
-
|
|
186
|
-
// Note: I'm intentionally not DRYing up the other variables to keep them "lazy".
|
|
187
|
-
const platform = globalThis.navigator?.userAgentData?.platform;
|
|
188
|
-
platform === 'macOS' || globalThis.navigator?.platform === 'MacIntel' // Even on Apple silicon Macs.
|
|
189
|
-
|| globalThis.navigator?.userAgent?.includes(' Mac ') === true || globalThis.process?.platform === 'darwin';
|
|
190
|
-
platform === 'Windows' || globalThis.navigator?.platform === 'Win32' || globalThis.process?.platform === 'win32';
|
|
191
|
-
platform === 'Linux' || globalThis.navigator?.platform?.startsWith('Linux') === true || globalThis.navigator?.userAgent?.includes(' Linux ') === true || globalThis.process?.platform === 'linux';
|
|
192
|
-
platform === 'Android' || globalThis.navigator?.platform === 'Android' || globalThis.navigator?.userAgent?.includes(' Android ') === true || globalThis.process?.platform === 'android';
|
|
193
|
-
!isBrowser && process$1.env.TERM_PROGRAM === 'Apple_Terminal';
|
|
194
|
-
!isBrowser && process$1.platform === 'win32';
|
|
195
|
-
isBrowser ? () => {
|
|
196
|
-
throw new Error('`process.cwd()` only works in Node.js, not the browser.');
|
|
197
|
-
} : process$1.cwd;
|
|
198
|
-
|
|
199
|
-
// https://github.com/Marak/colors.js/blob/master/lib/styles.js
|
|
200
|
-
// https://stackoverflow.com/a/75985833/2634179
|
|
201
|
-
const RESET = "\x1b[0m";
|
|
202
|
-
const createAnsi = ({
|
|
203
|
-
supported
|
|
204
|
-
}) => {
|
|
205
|
-
const ANSI = {
|
|
206
|
-
supported,
|
|
207
|
-
RED: "\x1b[31m",
|
|
208
|
-
GREEN: "\x1b[32m",
|
|
209
|
-
YELLOW: "\x1b[33m",
|
|
210
|
-
BLUE: "\x1b[34m",
|
|
211
|
-
MAGENTA: "\x1b[35m",
|
|
212
|
-
CYAN: "\x1b[36m",
|
|
213
|
-
GREY: "\x1b[90m",
|
|
214
|
-
color: (text, color) => {
|
|
215
|
-
if (!ANSI.supported) {
|
|
216
|
-
return text;
|
|
217
|
-
}
|
|
218
|
-
if (!color) {
|
|
219
|
-
return text;
|
|
220
|
-
}
|
|
221
|
-
if (typeof text === "string" && text.trim() === "") {
|
|
222
|
-
// cannot set color of blank chars
|
|
223
|
-
return text;
|
|
224
|
-
}
|
|
225
|
-
return "".concat(color).concat(text).concat(RESET);
|
|
226
|
-
},
|
|
227
|
-
BOLD: "\x1b[1m",
|
|
228
|
-
UNDERLINE: "\x1b[4m",
|
|
229
|
-
STRIKE: "\x1b[9m",
|
|
230
|
-
effect: (text, effect) => {
|
|
231
|
-
if (!ANSI.supported) {
|
|
232
|
-
return text;
|
|
233
|
-
}
|
|
234
|
-
if (!effect) {
|
|
235
|
-
return text;
|
|
236
|
-
}
|
|
237
|
-
// cannot add effect to empty string
|
|
238
|
-
if (text === "") {
|
|
239
|
-
return text;
|
|
240
|
-
}
|
|
241
|
-
return "".concat(effect).concat(text).concat(RESET);
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
return ANSI;
|
|
245
|
-
};
|
|
246
|
-
const processSupportsBasicColor = createSupportsColor(process.stdout).hasBasic;
|
|
247
|
-
const ANSI = createAnsi({
|
|
248
|
-
supported: process.env.FORCE_COLOR === "1" || processSupportsBasicColor ||
|
|
249
|
-
// GitHub workflow does support ANSI but "supports-color" returns false
|
|
250
|
-
// because stream.isTTY returns false, see https://github.com/actions/runner/issues/241
|
|
251
|
-
process.env.GITHUB_WORKFLOW
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
// see also https://github.com/sindresorhus/figures
|
|
255
|
-
|
|
256
|
-
const createUnicode = ({
|
|
257
|
-
supported,
|
|
258
|
-
ANSI
|
|
259
|
-
}) => {
|
|
260
|
-
const UNICODE = {
|
|
261
|
-
supported,
|
|
262
|
-
get COMMAND_RAW() {
|
|
263
|
-
return UNICODE.supported ? "\u276F" : ">";
|
|
264
|
-
},
|
|
265
|
-
get OK_RAW() {
|
|
266
|
-
return UNICODE.supported ? "\u2714" : "\u221A";
|
|
267
|
-
},
|
|
268
|
-
get FAILURE_RAW() {
|
|
269
|
-
return UNICODE.supported ? "\u2716" : "\xD7";
|
|
270
|
-
},
|
|
271
|
-
get DEBUG_RAW() {
|
|
272
|
-
return UNICODE.supported ? "\u25C6" : "\u2666";
|
|
273
|
-
},
|
|
274
|
-
get INFO_RAW() {
|
|
275
|
-
return UNICODE.supported ? "\u2139" : "i";
|
|
276
|
-
},
|
|
277
|
-
get WARNING_RAW() {
|
|
278
|
-
return UNICODE.supported ? "\u26A0" : "\u203C";
|
|
279
|
-
},
|
|
280
|
-
get CIRCLE_CROSS_RAW() {
|
|
281
|
-
return UNICODE.supported ? "\u24E7" : "(\xD7)";
|
|
282
|
-
},
|
|
283
|
-
get CIRCLE_DOTTED_RAW() {
|
|
284
|
-
return UNICODE.supported ? "\u25CC" : "*";
|
|
285
|
-
},
|
|
286
|
-
get COMMAND() {
|
|
287
|
-
return ANSI.color(UNICODE.COMMAND_RAW, ANSI.GREY); // ANSI_MAGENTA)
|
|
288
|
-
},
|
|
289
|
-
get OK() {
|
|
290
|
-
return ANSI.color(UNICODE.OK_RAW, ANSI.GREEN);
|
|
291
|
-
},
|
|
292
|
-
get FAILURE() {
|
|
293
|
-
return ANSI.color(UNICODE.FAILURE_RAW, ANSI.RED);
|
|
294
|
-
},
|
|
295
|
-
get DEBUG() {
|
|
296
|
-
return ANSI.color(UNICODE.DEBUG_RAW, ANSI.GREY);
|
|
297
|
-
},
|
|
298
|
-
get INFO() {
|
|
299
|
-
return ANSI.color(UNICODE.INFO_RAW, ANSI.BLUE);
|
|
300
|
-
},
|
|
301
|
-
get WARNING() {
|
|
302
|
-
return ANSI.color(UNICODE.WARNING_RAW, ANSI.YELLOW);
|
|
303
|
-
},
|
|
304
|
-
get CIRCLE_CROSS() {
|
|
305
|
-
return ANSI.color(UNICODE.CIRCLE_CROSS_RAW, ANSI.RED);
|
|
306
|
-
},
|
|
307
|
-
get ELLIPSIS() {
|
|
308
|
-
return UNICODE.supported ? "\u2026" : "...";
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
return UNICODE;
|
|
312
|
-
};
|
|
313
|
-
createUnicode({
|
|
314
|
-
supported: process.env.FORCE_UNICODE === "1" || isUnicodeSupported(),
|
|
315
|
-
ANSI
|
|
316
|
-
});
|
|
317
4
|
const formatDefault = v => v;
|
|
318
5
|
const generateContentFrame = ({
|
|
319
6
|
content,
|
|
@@ -1217,856 +904,6 @@ const generateCodeToSuperviseScriptWithSrc = ({
|
|
|
1217
904
|
return "window.__supervisor__.superviseScript(".concat(srcEncoded, ");");
|
|
1218
905
|
};
|
|
1219
906
|
|
|
1220
|
-
// https://nodejs.org/api/packages.html#resolving-user-conditions
|
|
1221
|
-
const readCustomConditionsFromProcessArgs = () => {
|
|
1222
|
-
const packageConditions = [];
|
|
1223
|
-
for (const arg of process.execArgv) {
|
|
1224
|
-
if (arg.includes("-C=")) {
|
|
1225
|
-
const packageCondition = arg.slice(0, "-C=".length);
|
|
1226
|
-
packageConditions.push(packageCondition);
|
|
1227
|
-
}
|
|
1228
|
-
if (arg.includes("--conditions=")) {
|
|
1229
|
-
const packageCondition = arg.slice("--conditions=".length);
|
|
1230
|
-
packageConditions.push(packageCondition);
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
return packageConditions;
|
|
1234
|
-
};
|
|
1235
|
-
|
|
1236
|
-
const asDirectoryUrl = url => {
|
|
1237
|
-
const {
|
|
1238
|
-
pathname
|
|
1239
|
-
} = new URL(url);
|
|
1240
|
-
if (pathname.endsWith("/")) {
|
|
1241
|
-
return url;
|
|
1242
|
-
}
|
|
1243
|
-
return new URL("./", url).href;
|
|
1244
|
-
};
|
|
1245
|
-
const getParentUrl = url => {
|
|
1246
|
-
if (url.startsWith("file://")) {
|
|
1247
|
-
// With node.js new URL('../', 'file:///C:/').href
|
|
1248
|
-
// returns "file:///C:/" instead of "file:///"
|
|
1249
|
-
const resource = url.slice("file://".length);
|
|
1250
|
-
const slashLastIndex = resource.lastIndexOf("/");
|
|
1251
|
-
if (slashLastIndex === -1) {
|
|
1252
|
-
return url;
|
|
1253
|
-
}
|
|
1254
|
-
const lastCharIndex = resource.length - 1;
|
|
1255
|
-
if (slashLastIndex === lastCharIndex) {
|
|
1256
|
-
const slashBeforeLastIndex = resource.lastIndexOf("/", slashLastIndex - 1);
|
|
1257
|
-
if (slashBeforeLastIndex === -1) {
|
|
1258
|
-
return url;
|
|
1259
|
-
}
|
|
1260
|
-
return "file://".concat(resource.slice(0, slashBeforeLastIndex + 1));
|
|
1261
|
-
}
|
|
1262
|
-
return "file://".concat(resource.slice(0, slashLastIndex + 1));
|
|
1263
|
-
}
|
|
1264
|
-
return new URL(url.endsWith("/") ? "../" : "./", url).href;
|
|
1265
|
-
};
|
|
1266
|
-
const isValidUrl = url => {
|
|
1267
|
-
try {
|
|
1268
|
-
// eslint-disable-next-line no-new
|
|
1269
|
-
new URL(url);
|
|
1270
|
-
return true;
|
|
1271
|
-
} catch (_unused) {
|
|
1272
|
-
return false;
|
|
1273
|
-
}
|
|
1274
|
-
};
|
|
1275
|
-
|
|
1276
|
-
const defaultLookupPackageScope = url => {
|
|
1277
|
-
let scopeUrl = asDirectoryUrl(url);
|
|
1278
|
-
while (scopeUrl !== "file:///") {
|
|
1279
|
-
if (scopeUrl.endsWith("node_modules/")) {
|
|
1280
|
-
return null;
|
|
1281
|
-
}
|
|
1282
|
-
const packageJsonUrlObject = new URL("package.json", scopeUrl);
|
|
1283
|
-
if (existsSync(packageJsonUrlObject)) {
|
|
1284
|
-
return scopeUrl;
|
|
1285
|
-
}
|
|
1286
|
-
scopeUrl = getParentUrl(scopeUrl);
|
|
1287
|
-
}
|
|
1288
|
-
return null;
|
|
1289
|
-
};
|
|
1290
|
-
|
|
1291
|
-
const defaultReadPackageJson = packageUrl => {
|
|
1292
|
-
const packageJsonUrl = new URL("package.json", packageUrl);
|
|
1293
|
-
const buffer = readFileSync(packageJsonUrl);
|
|
1294
|
-
const string = String(buffer);
|
|
1295
|
-
try {
|
|
1296
|
-
return JSON.parse(string);
|
|
1297
|
-
} catch (_unused) {
|
|
1298
|
-
throw new Error("Invalid package configuration");
|
|
1299
|
-
}
|
|
1300
|
-
};
|
|
1301
|
-
|
|
1302
|
-
// https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/tools/node_modules/eslint/node_modules/%40babel/core/lib/vendor/import-meta-resolve.js#L2473
|
|
1303
|
-
const createInvalidModuleSpecifierError = (reason, specifier, {
|
|
1304
|
-
parentUrl
|
|
1305
|
-
}) => {
|
|
1306
|
-
const error = new Error("Invalid module \"".concat(specifier, "\" ").concat(reason, " imported from ").concat(fileURLToPath(parentUrl)));
|
|
1307
|
-
error.code = "INVALID_MODULE_SPECIFIER";
|
|
1308
|
-
return error;
|
|
1309
|
-
};
|
|
1310
|
-
const createInvalidPackageTargetError = (reason, target, {
|
|
1311
|
-
parentUrl,
|
|
1312
|
-
packageDirectoryUrl,
|
|
1313
|
-
key,
|
|
1314
|
-
isImport
|
|
1315
|
-
}) => {
|
|
1316
|
-
let message;
|
|
1317
|
-
if (key === ".") {
|
|
1318
|
-
message = "Invalid \"exports\" main target defined in ".concat(fileURLToPath(packageDirectoryUrl), "package.json imported from ").concat(fileURLToPath(parentUrl), "; ").concat(reason);
|
|
1319
|
-
} else {
|
|
1320
|
-
message = "Invalid \"".concat(isImport ? "imports" : "exports", "\" target ").concat(JSON.stringify(target), " defined for \"").concat(key, "\" in ").concat(fileURLToPath(packageDirectoryUrl), "package.json imported from ").concat(fileURLToPath(parentUrl), "; ").concat(reason);
|
|
1321
|
-
}
|
|
1322
|
-
const error = new Error(message);
|
|
1323
|
-
error.code = "INVALID_PACKAGE_TARGET";
|
|
1324
|
-
return error;
|
|
1325
|
-
};
|
|
1326
|
-
const createPackagePathNotExportedError = (subpath, {
|
|
1327
|
-
parentUrl,
|
|
1328
|
-
packageDirectoryUrl
|
|
1329
|
-
}) => {
|
|
1330
|
-
let message;
|
|
1331
|
-
if (subpath === ".") {
|
|
1332
|
-
message = "No \"exports\" main defined in ".concat(fileURLToPath(packageDirectoryUrl), "package.json imported from ").concat(fileURLToPath(parentUrl));
|
|
1333
|
-
} else {
|
|
1334
|
-
message = "Package subpath \"".concat(subpath, "\" is not defined by \"exports\" in ").concat(fileURLToPath(packageDirectoryUrl), "package.json imported from ").concat(fileURLToPath(parentUrl));
|
|
1335
|
-
}
|
|
1336
|
-
const error = new Error(message);
|
|
1337
|
-
error.code = "PACKAGE_PATH_NOT_EXPORTED";
|
|
1338
|
-
return error;
|
|
1339
|
-
};
|
|
1340
|
-
const createModuleNotFoundError = (specifier, {
|
|
1341
|
-
parentUrl
|
|
1342
|
-
}) => {
|
|
1343
|
-
const error = new Error("Cannot find \"".concat(specifier, "\" imported from ").concat(fileURLToPath(parentUrl)));
|
|
1344
|
-
error.code = "MODULE_NOT_FOUND";
|
|
1345
|
-
return error;
|
|
1346
|
-
};
|
|
1347
|
-
const createPackageImportNotDefinedError = (specifier, {
|
|
1348
|
-
parentUrl,
|
|
1349
|
-
packageDirectoryUrl
|
|
1350
|
-
}) => {
|
|
1351
|
-
const error = new Error("Package import specifier \"".concat(specifier, "\" is not defined in ").concat(fileURLToPath(packageDirectoryUrl), "package.json imported from ").concat(fileURLToPath(parentUrl)));
|
|
1352
|
-
error.code = "PACKAGE_IMPORT_NOT_DEFINED";
|
|
1353
|
-
return error;
|
|
1354
|
-
};
|
|
1355
|
-
|
|
1356
|
-
const isSpecifierForNodeBuiltin = specifier => {
|
|
1357
|
-
return specifier.startsWith("node:") || NODE_BUILTIN_MODULE_SPECIFIERS.includes(specifier);
|
|
1358
|
-
};
|
|
1359
|
-
const NODE_BUILTIN_MODULE_SPECIFIERS = ["assert", "assert/strict", "async_hooks", "buffer_ieee754", "buffer", "child_process", "cluster", "console", "constants", "crypto", "_debugger", "dgram", "dns", "domain", "events", "freelist", "fs", "fsevents", "fs/promises", "_http_agent", "_http_client", "_http_common", "_http_incoming", "_http_outgoing", "_http_server", "http", "http2", "https", "inspector", "_linklist", "module", "net", "node-inspect/lib/_inspect", "node-inspect/lib/internal/inspect_client", "node-inspect/lib/internal/inspect_repl", "os", "path", "perf_hooks", "process", "punycode", "querystring", "readline", "repl", "smalloc", "_stream_duplex", "_stream_transform", "_stream_wrap", "_stream_passthrough", "_stream_readable", "_stream_writable", "stream", "stream/promises", "string_decoder", "sys", "timers", "_tls_common", "_tls_legacy", "_tls_wrap", "tls", "trace_events", "tty", "url", "util", "v8/tools/arguments", "v8/tools/codemap", "v8/tools/consarray", "v8/tools/csvparser", "v8/tools/logreader", "v8/tools/profile_view", "v8/tools/splaytree", "v8", "vm", "worker_threads", "zlib",
|
|
1360
|
-
// global is special
|
|
1361
|
-
"global"];
|
|
1362
|
-
|
|
1363
|
-
/*
|
|
1364
|
-
* https://nodejs.org/api/esm.html#resolver-algorithm-specification
|
|
1365
|
-
* https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/lib/internal/modules/esm/resolve.js#L1
|
|
1366
|
-
* deviations from the spec:
|
|
1367
|
-
* - take into account "browser", "module" and "jsnext"
|
|
1368
|
-
* - the check for isDirectory -> throw is delayed is descoped to the caller
|
|
1369
|
-
* - the call to real path ->
|
|
1370
|
-
* delayed to the caller so that we can decide to
|
|
1371
|
-
* maintain symlink as facade url when it's outside project directory
|
|
1372
|
-
* or use the real path when inside
|
|
1373
|
-
*/
|
|
1374
|
-
const applyNodeEsmResolution = ({
|
|
1375
|
-
specifier,
|
|
1376
|
-
parentUrl,
|
|
1377
|
-
conditions = [...readCustomConditionsFromProcessArgs(), "node", "import"],
|
|
1378
|
-
lookupPackageScope = defaultLookupPackageScope,
|
|
1379
|
-
readPackageJson = defaultReadPackageJson,
|
|
1380
|
-
preservesSymlink = false
|
|
1381
|
-
}) => {
|
|
1382
|
-
const resolution = applyPackageSpecifierResolution(specifier, {
|
|
1383
|
-
parentUrl: String(parentUrl),
|
|
1384
|
-
conditions,
|
|
1385
|
-
lookupPackageScope,
|
|
1386
|
-
readPackageJson,
|
|
1387
|
-
preservesSymlink
|
|
1388
|
-
});
|
|
1389
|
-
const {
|
|
1390
|
-
url
|
|
1391
|
-
} = resolution;
|
|
1392
|
-
if (url.startsWith("file:")) {
|
|
1393
|
-
if (url.includes("%2F") || url.includes("%5C")) {
|
|
1394
|
-
throw createInvalidModuleSpecifierError("must not include encoded \"/\" or \"\\\" characters", specifier, {
|
|
1395
|
-
parentUrl
|
|
1396
|
-
});
|
|
1397
|
-
}
|
|
1398
|
-
return resolution;
|
|
1399
|
-
}
|
|
1400
|
-
return resolution;
|
|
1401
|
-
};
|
|
1402
|
-
const applyPackageSpecifierResolution = (specifier, resolutionContext) => {
|
|
1403
|
-
const {
|
|
1404
|
-
parentUrl
|
|
1405
|
-
} = resolutionContext;
|
|
1406
|
-
// relative specifier
|
|
1407
|
-
if (specifier[0] === "/" || specifier.startsWith("./") || specifier.startsWith("../")) {
|
|
1408
|
-
if (specifier[0] !== "/") {
|
|
1409
|
-
const browserFieldResolution = applyBrowserFieldResolution(specifier, resolutionContext);
|
|
1410
|
-
if (browserFieldResolution) {
|
|
1411
|
-
return browserFieldResolution;
|
|
1412
|
-
}
|
|
1413
|
-
}
|
|
1414
|
-
return {
|
|
1415
|
-
type: "relative_specifier",
|
|
1416
|
-
url: new URL(specifier, parentUrl).href
|
|
1417
|
-
};
|
|
1418
|
-
}
|
|
1419
|
-
if (specifier[0] === "#") {
|
|
1420
|
-
return applyPackageImportsResolution(specifier, resolutionContext);
|
|
1421
|
-
}
|
|
1422
|
-
try {
|
|
1423
|
-
const urlObject = new URL(specifier);
|
|
1424
|
-
if (specifier.startsWith("node:")) {
|
|
1425
|
-
return {
|
|
1426
|
-
type: "node_builtin_specifier",
|
|
1427
|
-
url: specifier
|
|
1428
|
-
};
|
|
1429
|
-
}
|
|
1430
|
-
return {
|
|
1431
|
-
type: "absolute_specifier",
|
|
1432
|
-
url: urlObject.href
|
|
1433
|
-
};
|
|
1434
|
-
} catch (_unused) {
|
|
1435
|
-
// bare specifier
|
|
1436
|
-
const browserFieldResolution = applyBrowserFieldResolution(specifier, resolutionContext);
|
|
1437
|
-
if (browserFieldResolution) {
|
|
1438
|
-
return browserFieldResolution;
|
|
1439
|
-
}
|
|
1440
|
-
const packageResolution = applyPackageResolve(specifier, resolutionContext);
|
|
1441
|
-
const search = new URL(specifier, "file:///").search;
|
|
1442
|
-
if (search && !new URL(packageResolution.url).search) {
|
|
1443
|
-
packageResolution.url = "".concat(packageResolution.url).concat(search);
|
|
1444
|
-
}
|
|
1445
|
-
return packageResolution;
|
|
1446
|
-
}
|
|
1447
|
-
};
|
|
1448
|
-
const applyBrowserFieldResolution = (specifier, resolutionContext) => {
|
|
1449
|
-
const {
|
|
1450
|
-
parentUrl,
|
|
1451
|
-
conditions,
|
|
1452
|
-
lookupPackageScope,
|
|
1453
|
-
readPackageJson
|
|
1454
|
-
} = resolutionContext;
|
|
1455
|
-
const browserCondition = conditions.includes("browser");
|
|
1456
|
-
if (!browserCondition) {
|
|
1457
|
-
return null;
|
|
1458
|
-
}
|
|
1459
|
-
const packageDirectoryUrl = lookupPackageScope(parentUrl);
|
|
1460
|
-
if (!packageDirectoryUrl) {
|
|
1461
|
-
return null;
|
|
1462
|
-
}
|
|
1463
|
-
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
1464
|
-
if (!packageJson) {
|
|
1465
|
-
return null;
|
|
1466
|
-
}
|
|
1467
|
-
const {
|
|
1468
|
-
browser
|
|
1469
|
-
} = packageJson;
|
|
1470
|
-
if (!browser) {
|
|
1471
|
-
return null;
|
|
1472
|
-
}
|
|
1473
|
-
if (typeof browser !== "object") {
|
|
1474
|
-
return null;
|
|
1475
|
-
}
|
|
1476
|
-
let url;
|
|
1477
|
-
if (specifier.startsWith(".")) {
|
|
1478
|
-
const specifierUrl = new URL(specifier, parentUrl).href;
|
|
1479
|
-
const specifierRelativeUrl = specifierUrl.slice(packageDirectoryUrl.length);
|
|
1480
|
-
const secifierRelativeNotation = "./".concat(specifierRelativeUrl);
|
|
1481
|
-
const browserMapping = browser[secifierRelativeNotation];
|
|
1482
|
-
if (typeof browserMapping === "string") {
|
|
1483
|
-
url = new URL(browserMapping, packageDirectoryUrl).href;
|
|
1484
|
-
} else if (browserMapping === false) {
|
|
1485
|
-
url = "file:///@ignore/".concat(specifierUrl.slice("file:///"));
|
|
1486
|
-
}
|
|
1487
|
-
} else {
|
|
1488
|
-
const browserMapping = browser[specifier];
|
|
1489
|
-
if (typeof browserMapping === "string") {
|
|
1490
|
-
url = new URL(browserMapping, packageDirectoryUrl).href;
|
|
1491
|
-
} else if (browserMapping === false) {
|
|
1492
|
-
url = "file:///@ignore/".concat(specifier);
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
if (url) {
|
|
1496
|
-
return {
|
|
1497
|
-
type: "field:browser",
|
|
1498
|
-
isMain: true,
|
|
1499
|
-
packageDirectoryUrl,
|
|
1500
|
-
packageJson,
|
|
1501
|
-
url
|
|
1502
|
-
};
|
|
1503
|
-
}
|
|
1504
|
-
return null;
|
|
1505
|
-
};
|
|
1506
|
-
const applyPackageImportsResolution = (internalSpecifier, resolutionContext) => {
|
|
1507
|
-
const {
|
|
1508
|
-
parentUrl,
|
|
1509
|
-
lookupPackageScope,
|
|
1510
|
-
readPackageJson
|
|
1511
|
-
} = resolutionContext;
|
|
1512
|
-
if (internalSpecifier === "#" || internalSpecifier.startsWith("#/")) {
|
|
1513
|
-
throw createInvalidModuleSpecifierError("not a valid internal imports specifier name", internalSpecifier, resolutionContext);
|
|
1514
|
-
}
|
|
1515
|
-
const packageDirectoryUrl = lookupPackageScope(parentUrl);
|
|
1516
|
-
if (packageDirectoryUrl !== null) {
|
|
1517
|
-
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
1518
|
-
const {
|
|
1519
|
-
imports
|
|
1520
|
-
} = packageJson;
|
|
1521
|
-
if (imports !== null && typeof imports === "object") {
|
|
1522
|
-
const resolved = applyPackageImportsExportsResolution(internalSpecifier, {
|
|
1523
|
-
...resolutionContext,
|
|
1524
|
-
packageDirectoryUrl,
|
|
1525
|
-
packageJson,
|
|
1526
|
-
isImport: true
|
|
1527
|
-
});
|
|
1528
|
-
if (resolved) {
|
|
1529
|
-
return resolved;
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
|
-
}
|
|
1533
|
-
throw createPackageImportNotDefinedError(internalSpecifier, {
|
|
1534
|
-
...resolutionContext,
|
|
1535
|
-
packageDirectoryUrl
|
|
1536
|
-
});
|
|
1537
|
-
};
|
|
1538
|
-
const applyPackageResolve = (packageSpecifier, resolutionContext) => {
|
|
1539
|
-
const {
|
|
1540
|
-
parentUrl,
|
|
1541
|
-
conditions,
|
|
1542
|
-
readPackageJson,
|
|
1543
|
-
preservesSymlink
|
|
1544
|
-
} = resolutionContext;
|
|
1545
|
-
if (packageSpecifier === "") {
|
|
1546
|
-
throw new Error("invalid module specifier");
|
|
1547
|
-
}
|
|
1548
|
-
if (conditions.includes("node") && isSpecifierForNodeBuiltin(packageSpecifier)) {
|
|
1549
|
-
return {
|
|
1550
|
-
type: "node_builtin_specifier",
|
|
1551
|
-
url: "node:".concat(packageSpecifier)
|
|
1552
|
-
};
|
|
1553
|
-
}
|
|
1554
|
-
let {
|
|
1555
|
-
packageName,
|
|
1556
|
-
packageSubpath
|
|
1557
|
-
} = parsePackageSpecifier(packageSpecifier);
|
|
1558
|
-
if (packageName[0] === "." || packageName.includes("\\") || packageName.includes("%")) {
|
|
1559
|
-
throw createInvalidModuleSpecifierError("is not a valid package name", packageName, resolutionContext);
|
|
1560
|
-
}
|
|
1561
|
-
if (packageSubpath.endsWith("/")) {
|
|
1562
|
-
throw new Error("invalid module specifier");
|
|
1563
|
-
}
|
|
1564
|
-
const questionCharIndex = packageName.indexOf("?");
|
|
1565
|
-
if (questionCharIndex > -1) {
|
|
1566
|
-
packageName = packageName.slice(0, questionCharIndex);
|
|
1567
|
-
}
|
|
1568
|
-
const selfResolution = applyPackageSelfResolution(packageSubpath, {
|
|
1569
|
-
...resolutionContext,
|
|
1570
|
-
packageName
|
|
1571
|
-
});
|
|
1572
|
-
if (selfResolution) {
|
|
1573
|
-
return selfResolution;
|
|
1574
|
-
}
|
|
1575
|
-
let currentUrl = parentUrl;
|
|
1576
|
-
while (currentUrl !== "file:///") {
|
|
1577
|
-
const packageDirectoryFacadeUrl = new URL("node_modules/".concat(packageName, "/"), currentUrl).href;
|
|
1578
|
-
if (!existsSync(new URL(packageDirectoryFacadeUrl))) {
|
|
1579
|
-
currentUrl = getParentUrl(currentUrl);
|
|
1580
|
-
continue;
|
|
1581
|
-
}
|
|
1582
|
-
const packageDirectoryUrl = preservesSymlink ? packageDirectoryFacadeUrl : resolvePackageSymlink(packageDirectoryFacadeUrl);
|
|
1583
|
-
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
1584
|
-
if (packageJson !== null) {
|
|
1585
|
-
const {
|
|
1586
|
-
exports
|
|
1587
|
-
} = packageJson;
|
|
1588
|
-
if (exports !== null && exports !== undefined) {
|
|
1589
|
-
return applyPackageExportsResolution(packageSubpath, {
|
|
1590
|
-
...resolutionContext,
|
|
1591
|
-
packageDirectoryUrl,
|
|
1592
|
-
packageJson,
|
|
1593
|
-
exports
|
|
1594
|
-
});
|
|
1595
|
-
}
|
|
1596
|
-
}
|
|
1597
|
-
return applyLegacySubpathResolution(packageSubpath, {
|
|
1598
|
-
...resolutionContext,
|
|
1599
|
-
packageDirectoryUrl,
|
|
1600
|
-
packageJson
|
|
1601
|
-
});
|
|
1602
|
-
}
|
|
1603
|
-
throw createModuleNotFoundError(packageName, resolutionContext);
|
|
1604
|
-
};
|
|
1605
|
-
const applyPackageSelfResolution = (packageSubpath, resolutionContext) => {
|
|
1606
|
-
const {
|
|
1607
|
-
parentUrl,
|
|
1608
|
-
packageName,
|
|
1609
|
-
lookupPackageScope,
|
|
1610
|
-
readPackageJson
|
|
1611
|
-
} = resolutionContext;
|
|
1612
|
-
const packageDirectoryUrl = lookupPackageScope(parentUrl);
|
|
1613
|
-
if (!packageDirectoryUrl) {
|
|
1614
|
-
return undefined;
|
|
1615
|
-
}
|
|
1616
|
-
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
1617
|
-
if (!packageJson) {
|
|
1618
|
-
return undefined;
|
|
1619
|
-
}
|
|
1620
|
-
if (packageJson.name !== packageName) {
|
|
1621
|
-
return undefined;
|
|
1622
|
-
}
|
|
1623
|
-
const {
|
|
1624
|
-
exports
|
|
1625
|
-
} = packageJson;
|
|
1626
|
-
if (!exports) {
|
|
1627
|
-
const subpathResolution = applyLegacySubpathResolution(packageSubpath, {
|
|
1628
|
-
...resolutionContext,
|
|
1629
|
-
packageDirectoryUrl,
|
|
1630
|
-
packageJson
|
|
1631
|
-
});
|
|
1632
|
-
if (subpathResolution && subpathResolution.type !== "subpath") {
|
|
1633
|
-
return subpathResolution;
|
|
1634
|
-
}
|
|
1635
|
-
return undefined;
|
|
1636
|
-
}
|
|
1637
|
-
return applyPackageExportsResolution(packageSubpath, {
|
|
1638
|
-
...resolutionContext,
|
|
1639
|
-
packageDirectoryUrl,
|
|
1640
|
-
packageJson
|
|
1641
|
-
});
|
|
1642
|
-
};
|
|
1643
|
-
|
|
1644
|
-
// https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/lib/internal/modules/esm/resolve.js#L642
|
|
1645
|
-
const applyPackageExportsResolution = (packageSubpath, resolutionContext) => {
|
|
1646
|
-
if (packageSubpath === ".") {
|
|
1647
|
-
const mainExport = applyMainExportResolution(resolutionContext);
|
|
1648
|
-
if (!mainExport) {
|
|
1649
|
-
throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
|
|
1650
|
-
}
|
|
1651
|
-
const resolved = applyPackageTargetResolution(mainExport, {
|
|
1652
|
-
...resolutionContext,
|
|
1653
|
-
key: "."
|
|
1654
|
-
});
|
|
1655
|
-
if (resolved) {
|
|
1656
|
-
return resolved;
|
|
1657
|
-
}
|
|
1658
|
-
throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
|
|
1659
|
-
}
|
|
1660
|
-
const packageExportsInfo = readExports(resolutionContext);
|
|
1661
|
-
if (packageExportsInfo.type === "object" && packageExportsInfo.allKeysAreRelative) {
|
|
1662
|
-
const resolved = applyPackageImportsExportsResolution(packageSubpath, {
|
|
1663
|
-
...resolutionContext,
|
|
1664
|
-
isImport: false
|
|
1665
|
-
});
|
|
1666
|
-
if (resolved) {
|
|
1667
|
-
return resolved;
|
|
1668
|
-
}
|
|
1669
|
-
}
|
|
1670
|
-
throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
|
|
1671
|
-
};
|
|
1672
|
-
const applyPackageImportsExportsResolution = (matchKey, resolutionContext) => {
|
|
1673
|
-
const {
|
|
1674
|
-
packageJson,
|
|
1675
|
-
isImport
|
|
1676
|
-
} = resolutionContext;
|
|
1677
|
-
const matchObject = isImport ? packageJson.imports : packageJson.exports;
|
|
1678
|
-
if (!matchKey.includes("*") && matchObject.hasOwnProperty(matchKey)) {
|
|
1679
|
-
const target = matchObject[matchKey];
|
|
1680
|
-
return applyPackageTargetResolution(target, {
|
|
1681
|
-
...resolutionContext,
|
|
1682
|
-
key: matchKey,
|
|
1683
|
-
isImport
|
|
1684
|
-
});
|
|
1685
|
-
}
|
|
1686
|
-
const expansionKeys = Object.keys(matchObject).filter(key => key.split("*").length === 2).sort(comparePatternKeys);
|
|
1687
|
-
for (const expansionKey of expansionKeys) {
|
|
1688
|
-
const [patternBase, patternTrailer] = expansionKey.split("*");
|
|
1689
|
-
if (matchKey === patternBase) continue;
|
|
1690
|
-
if (!matchKey.startsWith(patternBase)) continue;
|
|
1691
|
-
if (patternTrailer.length > 0) {
|
|
1692
|
-
if (!matchKey.endsWith(patternTrailer)) continue;
|
|
1693
|
-
if (matchKey.length < expansionKey.length) continue;
|
|
1694
|
-
}
|
|
1695
|
-
const target = matchObject[expansionKey];
|
|
1696
|
-
const subpath = matchKey.slice(patternBase.length, matchKey.length - patternTrailer.length);
|
|
1697
|
-
return applyPackageTargetResolution(target, {
|
|
1698
|
-
...resolutionContext,
|
|
1699
|
-
key: matchKey,
|
|
1700
|
-
subpath,
|
|
1701
|
-
pattern: true,
|
|
1702
|
-
isImport
|
|
1703
|
-
});
|
|
1704
|
-
}
|
|
1705
|
-
return null;
|
|
1706
|
-
};
|
|
1707
|
-
const applyPackageTargetResolution = (target, resolutionContext) => {
|
|
1708
|
-
const {
|
|
1709
|
-
conditions,
|
|
1710
|
-
packageDirectoryUrl,
|
|
1711
|
-
packageJson,
|
|
1712
|
-
key,
|
|
1713
|
-
subpath = "",
|
|
1714
|
-
pattern = false,
|
|
1715
|
-
isImport = false
|
|
1716
|
-
} = resolutionContext;
|
|
1717
|
-
if (typeof target === "string") {
|
|
1718
|
-
if (pattern === false && subpath !== "" && !target.endsWith("/")) {
|
|
1719
|
-
throw new Error("invalid module specifier");
|
|
1720
|
-
}
|
|
1721
|
-
if (target.startsWith("./")) {
|
|
1722
|
-
const targetUrl = new URL(target, packageDirectoryUrl).href;
|
|
1723
|
-
if (!targetUrl.startsWith(packageDirectoryUrl)) {
|
|
1724
|
-
throw createInvalidPackageTargetError("target must be inside package", target, resolutionContext);
|
|
1725
|
-
}
|
|
1726
|
-
return {
|
|
1727
|
-
type: isImport ? "field:imports" : "field:exports",
|
|
1728
|
-
isMain: subpath === "" || subpath === ".",
|
|
1729
|
-
packageDirectoryUrl,
|
|
1730
|
-
packageJson,
|
|
1731
|
-
url: pattern ? targetUrl.replaceAll("*", subpath) : new URL(subpath, targetUrl).href
|
|
1732
|
-
};
|
|
1733
|
-
}
|
|
1734
|
-
if (!isImport || target.startsWith("../") || isValidUrl(target)) {
|
|
1735
|
-
throw createInvalidPackageTargetError("target must starst with \"./\"", target, resolutionContext);
|
|
1736
|
-
}
|
|
1737
|
-
return applyPackageResolve(pattern ? target.replaceAll("*", subpath) : "".concat(target).concat(subpath), {
|
|
1738
|
-
...resolutionContext,
|
|
1739
|
-
parentUrl: packageDirectoryUrl
|
|
1740
|
-
});
|
|
1741
|
-
}
|
|
1742
|
-
if (Array.isArray(target)) {
|
|
1743
|
-
if (target.length === 0) {
|
|
1744
|
-
return null;
|
|
1745
|
-
}
|
|
1746
|
-
let lastResult;
|
|
1747
|
-
let i = 0;
|
|
1748
|
-
while (i < target.length) {
|
|
1749
|
-
const targetValue = target[i];
|
|
1750
|
-
i++;
|
|
1751
|
-
try {
|
|
1752
|
-
const resolved = applyPackageTargetResolution(targetValue, {
|
|
1753
|
-
...resolutionContext,
|
|
1754
|
-
key: "".concat(key, "[").concat(i, "]"),
|
|
1755
|
-
subpath,
|
|
1756
|
-
pattern,
|
|
1757
|
-
isImport
|
|
1758
|
-
});
|
|
1759
|
-
if (resolved) {
|
|
1760
|
-
return resolved;
|
|
1761
|
-
}
|
|
1762
|
-
lastResult = resolved;
|
|
1763
|
-
} catch (e) {
|
|
1764
|
-
if (e.code === "INVALID_PACKAGE_TARGET") {
|
|
1765
|
-
continue;
|
|
1766
|
-
}
|
|
1767
|
-
lastResult = e;
|
|
1768
|
-
}
|
|
1769
|
-
}
|
|
1770
|
-
if (lastResult) {
|
|
1771
|
-
throw lastResult;
|
|
1772
|
-
}
|
|
1773
|
-
return null;
|
|
1774
|
-
}
|
|
1775
|
-
if (target === null) {
|
|
1776
|
-
return null;
|
|
1777
|
-
}
|
|
1778
|
-
if (typeof target === "object") {
|
|
1779
|
-
const keys = Object.keys(target);
|
|
1780
|
-
for (const key of keys) {
|
|
1781
|
-
if (Number.isInteger(key)) {
|
|
1782
|
-
throw new Error("Invalid package configuration");
|
|
1783
|
-
}
|
|
1784
|
-
if (key === "default" || conditions.includes(key)) {
|
|
1785
|
-
const targetValue = target[key];
|
|
1786
|
-
const resolved = applyPackageTargetResolution(targetValue, {
|
|
1787
|
-
...resolutionContext,
|
|
1788
|
-
key,
|
|
1789
|
-
subpath,
|
|
1790
|
-
pattern,
|
|
1791
|
-
isImport
|
|
1792
|
-
});
|
|
1793
|
-
if (resolved) {
|
|
1794
|
-
return resolved;
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
}
|
|
1798
|
-
return null;
|
|
1799
|
-
}
|
|
1800
|
-
throw createInvalidPackageTargetError("target must be a string, array, object or null", target, resolutionContext);
|
|
1801
|
-
};
|
|
1802
|
-
const readExports = ({
|
|
1803
|
-
packageDirectoryUrl,
|
|
1804
|
-
packageJson
|
|
1805
|
-
}) => {
|
|
1806
|
-
const packageExports = packageJson.exports;
|
|
1807
|
-
if (Array.isArray(packageExports)) {
|
|
1808
|
-
return {
|
|
1809
|
-
type: "array"
|
|
1810
|
-
};
|
|
1811
|
-
}
|
|
1812
|
-
if (packageExports === null) {
|
|
1813
|
-
return {};
|
|
1814
|
-
}
|
|
1815
|
-
if (typeof packageExports === "object") {
|
|
1816
|
-
const keys = Object.keys(packageExports);
|
|
1817
|
-
const relativeKeys = [];
|
|
1818
|
-
const conditionalKeys = [];
|
|
1819
|
-
keys.forEach(availableKey => {
|
|
1820
|
-
if (availableKey.startsWith(".")) {
|
|
1821
|
-
relativeKeys.push(availableKey);
|
|
1822
|
-
} else {
|
|
1823
|
-
conditionalKeys.push(availableKey);
|
|
1824
|
-
}
|
|
1825
|
-
});
|
|
1826
|
-
const hasRelativeKey = relativeKeys.length > 0;
|
|
1827
|
-
if (hasRelativeKey && conditionalKeys.length > 0) {
|
|
1828
|
-
throw new Error("Invalid package configuration: cannot mix relative and conditional keys in package.exports\n--- unexpected keys ---\n".concat(conditionalKeys.map(key => "\"".concat(key, "\"")).join("\n"), "\n--- package directory url ---\n").concat(packageDirectoryUrl));
|
|
1829
|
-
}
|
|
1830
|
-
return {
|
|
1831
|
-
type: "object",
|
|
1832
|
-
hasRelativeKey,
|
|
1833
|
-
allKeysAreRelative: relativeKeys.length === keys.length
|
|
1834
|
-
};
|
|
1835
|
-
}
|
|
1836
|
-
if (typeof packageExports === "string") {
|
|
1837
|
-
return {
|
|
1838
|
-
type: "string"
|
|
1839
|
-
};
|
|
1840
|
-
}
|
|
1841
|
-
return {};
|
|
1842
|
-
};
|
|
1843
|
-
const parsePackageSpecifier = packageSpecifier => {
|
|
1844
|
-
if (packageSpecifier[0] === "@") {
|
|
1845
|
-
const firstSlashIndex = packageSpecifier.indexOf("/");
|
|
1846
|
-
if (firstSlashIndex === -1) {
|
|
1847
|
-
throw new Error("invalid module specifier");
|
|
1848
|
-
}
|
|
1849
|
-
const secondSlashIndex = packageSpecifier.indexOf("/", firstSlashIndex + 1);
|
|
1850
|
-
if (secondSlashIndex === -1) {
|
|
1851
|
-
return {
|
|
1852
|
-
packageName: packageSpecifier,
|
|
1853
|
-
packageSubpath: ".",
|
|
1854
|
-
isScoped: true
|
|
1855
|
-
};
|
|
1856
|
-
}
|
|
1857
|
-
const packageName = packageSpecifier.slice(0, secondSlashIndex);
|
|
1858
|
-
const afterSecondSlash = packageSpecifier.slice(secondSlashIndex + 1);
|
|
1859
|
-
const packageSubpath = "./".concat(afterSecondSlash);
|
|
1860
|
-
return {
|
|
1861
|
-
packageName,
|
|
1862
|
-
packageSubpath,
|
|
1863
|
-
isScoped: true
|
|
1864
|
-
};
|
|
1865
|
-
}
|
|
1866
|
-
const firstSlashIndex = packageSpecifier.indexOf("/");
|
|
1867
|
-
if (firstSlashIndex === -1) {
|
|
1868
|
-
return {
|
|
1869
|
-
packageName: packageSpecifier,
|
|
1870
|
-
packageSubpath: "."
|
|
1871
|
-
};
|
|
1872
|
-
}
|
|
1873
|
-
const packageName = packageSpecifier.slice(0, firstSlashIndex);
|
|
1874
|
-
const afterFirstSlash = packageSpecifier.slice(firstSlashIndex + 1);
|
|
1875
|
-
const packageSubpath = "./".concat(afterFirstSlash);
|
|
1876
|
-
return {
|
|
1877
|
-
packageName,
|
|
1878
|
-
packageSubpath
|
|
1879
|
-
};
|
|
1880
|
-
};
|
|
1881
|
-
const applyMainExportResolution = resolutionContext => {
|
|
1882
|
-
const {
|
|
1883
|
-
packageJson
|
|
1884
|
-
} = resolutionContext;
|
|
1885
|
-
const packageExportsInfo = readExports(resolutionContext);
|
|
1886
|
-
if (packageExportsInfo.type === "array" || packageExportsInfo.type === "string") {
|
|
1887
|
-
return packageJson.exports;
|
|
1888
|
-
}
|
|
1889
|
-
if (packageExportsInfo.type === "object") {
|
|
1890
|
-
if (packageExportsInfo.hasRelativeKey) {
|
|
1891
|
-
return packageJson.exports["."];
|
|
1892
|
-
}
|
|
1893
|
-
return packageJson.exports;
|
|
1894
|
-
}
|
|
1895
|
-
return undefined;
|
|
1896
|
-
};
|
|
1897
|
-
const applyLegacySubpathResolution = (packageSubpath, resolutionContext) => {
|
|
1898
|
-
const {
|
|
1899
|
-
packageDirectoryUrl,
|
|
1900
|
-
packageJson
|
|
1901
|
-
} = resolutionContext;
|
|
1902
|
-
if (packageSubpath === ".") {
|
|
1903
|
-
return applyLegacyMainResolution(packageSubpath, resolutionContext);
|
|
1904
|
-
}
|
|
1905
|
-
const browserFieldResolution = applyBrowserFieldResolution(packageSubpath, resolutionContext);
|
|
1906
|
-
if (browserFieldResolution) {
|
|
1907
|
-
return browserFieldResolution;
|
|
1908
|
-
}
|
|
1909
|
-
return {
|
|
1910
|
-
type: "subpath",
|
|
1911
|
-
isMain: packageSubpath === ".",
|
|
1912
|
-
packageDirectoryUrl,
|
|
1913
|
-
packageJson,
|
|
1914
|
-
url: new URL(packageSubpath, packageDirectoryUrl).href
|
|
1915
|
-
};
|
|
1916
|
-
};
|
|
1917
|
-
const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
|
|
1918
|
-
const {
|
|
1919
|
-
conditions,
|
|
1920
|
-
packageDirectoryUrl,
|
|
1921
|
-
packageJson
|
|
1922
|
-
} = resolutionContext;
|
|
1923
|
-
for (const condition of conditions) {
|
|
1924
|
-
const conditionResolver = mainLegacyResolvers[condition];
|
|
1925
|
-
if (!conditionResolver) {
|
|
1926
|
-
continue;
|
|
1927
|
-
}
|
|
1928
|
-
const resolved = conditionResolver(resolutionContext);
|
|
1929
|
-
if (resolved) {
|
|
1930
|
-
return {
|
|
1931
|
-
type: resolved.type,
|
|
1932
|
-
isMain: resolved.isMain,
|
|
1933
|
-
packageDirectoryUrl,
|
|
1934
|
-
packageJson,
|
|
1935
|
-
url: new URL(resolved.path, packageDirectoryUrl).href
|
|
1936
|
-
};
|
|
1937
|
-
}
|
|
1938
|
-
}
|
|
1939
|
-
return {
|
|
1940
|
-
type: "field:main",
|
|
1941
|
-
// the absence of "main" field
|
|
1942
|
-
isMain: true,
|
|
1943
|
-
packageDirectoryUrl,
|
|
1944
|
-
packageJson,
|
|
1945
|
-
url: new URL("index.js", packageDirectoryUrl).href
|
|
1946
|
-
};
|
|
1947
|
-
};
|
|
1948
|
-
const mainLegacyResolvers = {
|
|
1949
|
-
import: ({
|
|
1950
|
-
packageJson
|
|
1951
|
-
}) => {
|
|
1952
|
-
if (typeof packageJson.module === "string") {
|
|
1953
|
-
return {
|
|
1954
|
-
type: "field:module",
|
|
1955
|
-
isMain: true,
|
|
1956
|
-
path: packageJson.module
|
|
1957
|
-
};
|
|
1958
|
-
}
|
|
1959
|
-
if (typeof packageJson.jsnext === "string") {
|
|
1960
|
-
return {
|
|
1961
|
-
type: "field:jsnext",
|
|
1962
|
-
isMain: true,
|
|
1963
|
-
path: packageJson.jsnext
|
|
1964
|
-
};
|
|
1965
|
-
}
|
|
1966
|
-
if (typeof packageJson.main === "string") {
|
|
1967
|
-
return {
|
|
1968
|
-
type: "field:main",
|
|
1969
|
-
isMain: true,
|
|
1970
|
-
path: packageJson.main
|
|
1971
|
-
};
|
|
1972
|
-
}
|
|
1973
|
-
return null;
|
|
1974
|
-
},
|
|
1975
|
-
browser: ({
|
|
1976
|
-
packageDirectoryUrl,
|
|
1977
|
-
packageJson
|
|
1978
|
-
}) => {
|
|
1979
|
-
const browserMain = (() => {
|
|
1980
|
-
if (typeof packageJson.browser === "string") {
|
|
1981
|
-
return packageJson.browser;
|
|
1982
|
-
}
|
|
1983
|
-
if (typeof packageJson.browser === "object" && packageJson.browser !== null) {
|
|
1984
|
-
return packageJson.browser["."];
|
|
1985
|
-
}
|
|
1986
|
-
return "";
|
|
1987
|
-
})();
|
|
1988
|
-
if (!browserMain) {
|
|
1989
|
-
if (typeof packageJson.module === "string") {
|
|
1990
|
-
return {
|
|
1991
|
-
type: "field:module",
|
|
1992
|
-
isMain: true,
|
|
1993
|
-
path: packageJson.module
|
|
1994
|
-
};
|
|
1995
|
-
}
|
|
1996
|
-
return null;
|
|
1997
|
-
}
|
|
1998
|
-
if (typeof packageJson.module !== "string" || packageJson.module === browserMain) {
|
|
1999
|
-
return {
|
|
2000
|
-
type: "field:browser",
|
|
2001
|
-
isMain: true,
|
|
2002
|
-
path: browserMain
|
|
2003
|
-
};
|
|
2004
|
-
}
|
|
2005
|
-
const browserMainUrlObject = new URL(browserMain, packageDirectoryUrl);
|
|
2006
|
-
const content = readFileSync(browserMainUrlObject, "utf-8");
|
|
2007
|
-
if (/typeof exports\s*==/.test(content) && /typeof module\s*==/.test(content) || /module\.exports\s*=/.test(content)) {
|
|
2008
|
-
return {
|
|
2009
|
-
type: "field:module",
|
|
2010
|
-
isMain: true,
|
|
2011
|
-
path: packageJson.module
|
|
2012
|
-
};
|
|
2013
|
-
}
|
|
2014
|
-
return {
|
|
2015
|
-
type: "field:browser",
|
|
2016
|
-
isMain: true,
|
|
2017
|
-
path: browserMain
|
|
2018
|
-
};
|
|
2019
|
-
},
|
|
2020
|
-
node: ({
|
|
2021
|
-
packageJson
|
|
2022
|
-
}) => {
|
|
2023
|
-
if (typeof packageJson.main === "string") {
|
|
2024
|
-
return {
|
|
2025
|
-
type: "field:main",
|
|
2026
|
-
isMain: true,
|
|
2027
|
-
path: packageJson.main
|
|
2028
|
-
};
|
|
2029
|
-
}
|
|
2030
|
-
return null;
|
|
2031
|
-
}
|
|
2032
|
-
};
|
|
2033
|
-
const comparePatternKeys = (keyA, keyB) => {
|
|
2034
|
-
if (!keyA.endsWith("/") && !keyA.includes("*")) {
|
|
2035
|
-
throw new Error("Invalid package configuration");
|
|
2036
|
-
}
|
|
2037
|
-
if (!keyB.endsWith("/") && !keyB.includes("*")) {
|
|
2038
|
-
throw new Error("Invalid package configuration");
|
|
2039
|
-
}
|
|
2040
|
-
const aStarIndex = keyA.indexOf("*");
|
|
2041
|
-
const baseLengthA = aStarIndex > -1 ? aStarIndex + 1 : keyA.length;
|
|
2042
|
-
const bStarIndex = keyB.indexOf("*");
|
|
2043
|
-
const baseLengthB = bStarIndex > -1 ? bStarIndex + 1 : keyB.length;
|
|
2044
|
-
if (baseLengthA > baseLengthB) {
|
|
2045
|
-
return -1;
|
|
2046
|
-
}
|
|
2047
|
-
if (baseLengthB > baseLengthA) {
|
|
2048
|
-
return 1;
|
|
2049
|
-
}
|
|
2050
|
-
if (aStarIndex === -1) {
|
|
2051
|
-
return 1;
|
|
2052
|
-
}
|
|
2053
|
-
if (bStarIndex === -1) {
|
|
2054
|
-
return -1;
|
|
2055
|
-
}
|
|
2056
|
-
if (keyA.length > keyB.length) {
|
|
2057
|
-
return -1;
|
|
2058
|
-
}
|
|
2059
|
-
if (keyB.length > keyA.length) {
|
|
2060
|
-
return 1;
|
|
2061
|
-
}
|
|
2062
|
-
return 0;
|
|
2063
|
-
};
|
|
2064
|
-
const resolvePackageSymlink = packageDirectoryUrl => {
|
|
2065
|
-
const packageDirectoryPath = realpathSync(new URL(packageDirectoryUrl));
|
|
2066
|
-
const packageDirectoryResolvedUrl = pathToFileURL(packageDirectoryPath).href;
|
|
2067
|
-
return "".concat(packageDirectoryResolvedUrl, "/");
|
|
2068
|
-
};
|
|
2069
|
-
|
|
2070
907
|
/*
|
|
2071
908
|
* This plugin provides a way for jsenv to supervisor js execution:
|
|
2072
909
|
* - Know how many js are executed, when they are done, collect errors, etc...
|
|
@@ -2206,11 +1043,7 @@ const jsenvPluginSupervisor = ({
|
|
|
2206
1043
|
}
|
|
2207
1044
|
const {
|
|
2208
1045
|
url
|
|
2209
|
-
} =
|
|
2210
|
-
conditions: [],
|
|
2211
|
-
parentUrl: kitchen.context.rootDirectoryUrl,
|
|
2212
|
-
specifier: file
|
|
2213
|
-
});
|
|
1046
|
+
} = kitchen.resolve(file, kitchen.context.rootDirectoryUrl);
|
|
2214
1047
|
file = url;
|
|
2215
1048
|
const getErrorCauseInfo = () => {
|
|
2216
1049
|
const urlInfo = kitchen.graph.getUrlInfo(file);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/plugin-supervisor",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.12",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"./src/client/supervisor.js"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@jsenv/ast": "6.6.
|
|
33
|
-
"@jsenv/urls": "2.7.
|
|
34
|
-
"@jsenv/sourcemap": "1.3.
|
|
32
|
+
"@jsenv/ast": "6.6.7",
|
|
33
|
+
"@jsenv/urls": "2.7.1",
|
|
34
|
+
"@jsenv/sourcemap": "1.3.4"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "node ./scripts/build.mjs",
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { generateContentFrame } from "@jsenv/humanize";
|
|
7
|
-
import { applyNodeEsmResolution } from "@jsenv/node-esm-resolution";
|
|
8
7
|
import { getOriginalPosition } from "@jsenv/sourcemap";
|
|
9
8
|
import { injectQueryParams } from "@jsenv/urls";
|
|
10
9
|
import {
|
|
@@ -151,11 +150,10 @@ export const jsenvPluginSupervisor = ({
|
|
|
151
150
|
body: "Missing file in url",
|
|
152
151
|
};
|
|
153
152
|
}
|
|
154
|
-
const { url } =
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
});
|
|
153
|
+
const { url } = kitchen.resolve(
|
|
154
|
+
file,
|
|
155
|
+
kitchen.context.rootDirectoryUrl,
|
|
156
|
+
);
|
|
159
157
|
file = url;
|
|
160
158
|
const getErrorCauseInfo = () => {
|
|
161
159
|
const urlInfo = kitchen.graph.getUrlInfo(file);
|