-
Notifications
You must be signed in to change notification settings - Fork 535
/
externals.ts
556 lines (508 loc) · 16.8 KB
/
externals.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import { existsSync, promises as fsp } from "node:fs";
import { platform } from "node:os";
import { nodeFileTrace } from "@vercel/nft";
import {
isValidNodeImport,
lookupNodeModuleSubpath,
normalizeid,
parseNodeModulePath,
resolvePath,
} from "mlly";
import { isDirectory } from "nitropack/kit";
import type { NodeExternalsOptions } from "nitropack/types";
import { dirname, isAbsolute, join, normalize, relative, resolve } from "pathe";
import type { PackageJson } from "pkg-types";
import { readPackageJSON, writePackageJSON } from "pkg-types";
import type { Plugin } from "rollup";
import semver from "semver";
export function externals(opts: NodeExternalsOptions): Plugin {
const trackedExternals = new Set<string>();
const _resolveCache = new Map();
const _resolve = async (id: string): Promise<string> => {
if (id.startsWith("\0")) {
return id;
}
let resolved = _resolveCache.get(id);
if (resolved) {
return resolved;
}
resolved = await resolvePath(id, {
conditions: opts.exportConditions,
url: opts.moduleDirectories,
});
_resolveCache.set(id, resolved);
return resolved;
};
// Normalize options
const inlineMatchers = (opts.inline || [])
.map((p) => normalizeMatcher(p))
.sort((a, b) => (b.score || 0) - (a.score || 0));
const externalMatchers = (opts.external || [])
.map((p) => normalizeMatcher(p))
.sort((a, b) => (b.score || 0) - (a.score || 0));
// Utility to check explicit inlines
const isExplicitInline = (id: string, importer?: string) => {
if (id.startsWith("\0")) {
return true;
}
const inlineMatch = inlineMatchers.find((m) => m(id, importer));
const externalMatch = externalMatchers.find((m) => m(id, importer));
if (
inlineMatch &&
(!externalMatch ||
(externalMatch &&
(inlineMatch.score || 0) > (externalMatch.score || 0)))
) {
return true;
}
};
return {
name: "node-externals",
async resolveId(originalId, importer, options) {
// Skip internals
if (
!originalId ||
originalId.startsWith("\u0000") ||
originalId.includes("?") ||
originalId.startsWith("#")
) {
return null;
}
// Skip relative paths
if (originalId.startsWith(".")) {
return null;
}
// Normalize path (windows)
const id = normalize(originalId);
// Check for explicit inlines and externals
if (isExplicitInline(id, importer)) {
return null;
}
// Resolve id using rollup resolver
const resolved = (await this.resolve(originalId, importer, options)) || {
id,
};
// Check for explicit inlines and externals
if (isExplicitInline(resolved.id, importer)) {
return null;
}
// Try resolving with mlly as fallback
if (
!isAbsolute(resolved.id) ||
!existsSync(resolved.id) ||
(await isDirectory(resolved.id))
) {
resolved.id = await _resolve(resolved.id).catch(() => resolved.id);
}
// Inline invalid node imports
if (!(await isValidNodeImport(resolved.id).catch(() => false))) {
return null;
}
// Externalize with full path if trace is disabled
if (opts.trace === false) {
return {
...resolved,
id: isAbsolute(resolved.id) ? normalizeid(resolved.id) : resolved.id,
external: true,
};
}
// -- Trace externals --
// Try to extract package name from path
const { name: pkgName } = parseNodeModulePath(resolved.id);
// Inline if cannot detect package name
if (!pkgName) {
return null;
}
// Normally package name should be same as originalId
// Edge cases: Subpath export and full paths
if (pkgName !== originalId) {
// Subpath export
if (!isAbsolute(originalId)) {
const fullPath = await _resolve(originalId);
trackedExternals.add(fullPath);
return {
id: originalId,
external: true,
};
}
// Absolute path, we are not sure about subpath to generate import statement
// Guess as main subpath export
const packageEntry = await _resolve(pkgName).catch(() => null);
if (packageEntry !== id) {
// Reverse engineer subpath export
const guessedSubpath: string | null | undefined =
await lookupNodeModuleSubpath(id).catch(() => null);
const resolvedGuess =
guessedSubpath &&
(await _resolve(join(pkgName, guessedSubpath)).catch(() => null));
if (resolvedGuess === id) {
trackedExternals.add(resolvedGuess);
return {
id: join(pkgName, guessedSubpath!),
external: true,
};
}
// Inline since we cannot guess subpath
return null;
}
}
trackedExternals.add(resolved.id);
return {
id: pkgName,
external: true,
};
},
async buildEnd() {
if (opts.trace === false) {
return;
}
// Manually traced paths
for (const pkgName of opts.traceInclude || []) {
const path = await this.resolve(pkgName);
if (path?.id) {
trackedExternals.add(path.id.replace(/\?.+/, ""));
}
}
// Trace used files using nft
const _fileTrace = await nodeFileTrace([...trackedExternals], {
// https://github.com/nitrojs/nitro/pull/1562
conditions: (opts.exportConditions || []).filter(
(c) => !["require", "import", "default"].includes(c)
),
...opts.traceOptions,
});
// Resolve traced files
type TracedFile = {
path: string;
subpath: string;
parents: string[];
pkgPath: string;
pkgName: string;
pkgVersion: string;
};
const _resolveTracedPath = (p: string) =>
fsp.realpath(resolve(opts.traceOptions?.base || ".", p));
const tracedFiles: Record<string, TracedFile> = Object.fromEntries(
(await Promise.all(
[..._fileTrace.reasons.entries()].map(async ([_path, reasons]) => {
if (reasons.ignored) {
return;
}
const path = await _resolveTracedPath(_path);
if (!path.includes("node_modules")) {
return;
}
if (!(await isFile(path))) {
return;
}
const {
dir: baseDir,
name: pkgName,
subpath,
} = parseNodeModulePath(path);
if (!baseDir || !pkgName) {
return;
}
const pkgPath = join(baseDir, pkgName);
const parents = await Promise.all(
[...reasons.parents].map((p) => _resolveTracedPath(p))
);
const tracedFile = <TracedFile>{
path,
parents,
subpath,
pkgName,
pkgPath,
};
return [path, tracedFile];
})
).then((r) => r.filter(Boolean))) as [string, TracedFile][]
);
// Resolve traced packages
type TracedPackage = {
name: string;
versions: Record<
string,
{
pkgJSON: PackageJson;
path: string;
files: string[];
}
>;
};
const tracedPackages: Record<string, TracedPackage> = {};
for (const tracedFile of Object.values(tracedFiles)) {
// Use `node_modules/{name}` in path as name to support aliases
const pkgName = tracedFile.pkgName;
let tracedPackage = tracedPackages[pkgName];
// Read package.json for file
let pkgJSON = await readPackageJSON(tracedFile.pkgPath, {
cache: true,
}).catch(
() => {} // TODO: Only catch ENOENT
);
if (!pkgJSON) {
pkgJSON = <PackageJson>{ name: pkgName, version: "0.0.0" };
}
if (!tracedPackage) {
tracedPackage = {
name: pkgName,
versions: {},
};
tracedPackages[pkgName] = tracedPackage;
}
let tracedPackageVersion =
tracedPackage.versions[pkgJSON.version || "0.0.0"];
if (!tracedPackageVersion) {
tracedPackageVersion = {
path: tracedFile.pkgPath,
files: [],
pkgJSON,
};
tracedPackage.versions[pkgJSON.version || "0.0.0"] =
tracedPackageVersion;
}
tracedPackageVersion.files.push(tracedFile.path);
tracedFile.pkgName = pkgName;
if (pkgJSON.version) {
tracedFile.pkgVersion = pkgJSON.version;
}
}
const usedAliases: Record<string, string> = {};
const writePackage = async (
name: string,
version: string,
_pkgPath?: string
) => {
// Find pkg
const pkg = tracedPackages[name];
const pkgPath = _pkgPath || pkg.name;
// Copy files
for (const src of pkg.versions[version].files) {
const { subpath } = parseNodeModulePath(src);
if (!subpath) {
continue;
}
const dst = join(opts.outDir, "node_modules", pkgPath, subpath);
await fsp.mkdir(dirname(dst), { recursive: true });
await fsp.copyFile(src, dst);
}
// Copy package.json
const pkgJSON = pkg.versions[version].pkgJSON;
applyProductionCondition(pkgJSON.exports);
const pkgJSONPath = join(
opts.outDir,
"node_modules",
pkgPath,
"package.json"
);
await fsp.mkdir(dirname(pkgJSONPath), { recursive: true });
await fsp.writeFile(
pkgJSONPath,
JSON.stringify(pkgJSON, null, 2),
"utf8"
);
// Link aliases
if (opts.traceAlias && pkgPath in opts.traceAlias) {
usedAliases[opts.traceAlias[pkgPath]] = version;
await linkPackage(pkgPath, opts.traceAlias[pkgPath]);
}
};
const isWindows = platform() === "win32";
const linkPackage = async (from: string, to: string) => {
const src = join(opts.outDir, "node_modules", from);
const dst = join(opts.outDir, "node_modules", to);
const dstStat = await fsp.lstat(dst).catch(() => null);
const exists = dstStat?.isSymbolicLink();
// console.log("Linking", from, "to", to, exists ? "!!!!" : "");
if (exists) {
return;
}
await fsp.mkdir(dirname(dst), { recursive: true });
await fsp
.symlink(
relative(dirname(dst), src),
dst,
isWindows ? "junction" : "dir"
)
.catch((error) => {
console.error("Cannot link", from, "to", to, error);
});
};
// Utility to find package parents
const findPackageParents = (pkg: TracedPackage, version: string) => {
// Try to find parent packages
const versionFiles: TracedFile[] = pkg.versions[version].files.map(
(path) => tracedFiles[path]
);
const parentPkgs = [
...new Set(
versionFiles.flatMap((file) =>
file.parents
.map((parentPath) => {
const parentFile = tracedFiles[parentPath];
if (parentFile.pkgName === pkg.name) {
return null;
}
return `${parentFile.pkgName}@${parentFile.pkgVersion}`;
})
.filter(Boolean)
) as string[]
),
];
return parentPkgs;
};
// Analyze dependency tree
const multiVersionPkgs: Record<string, { [version: string]: string[] }> =
{};
const singleVersionPackages: string[] = [];
for (const tracedPackage of Object.values(tracedPackages)) {
const versions = Object.keys(tracedPackage.versions);
if (versions.length === 1) {
singleVersionPackages.push(tracedPackage.name);
continue;
}
multiVersionPkgs[tracedPackage.name] = {};
for (const version of versions) {
multiVersionPkgs[tracedPackage.name][version] = findPackageParents(
tracedPackage,
version
);
}
}
// Directly write single version packages
await Promise.all(
singleVersionPackages.map((pkgName) => {
const pkg = tracedPackages[pkgName];
const version = Object.keys(pkg.versions)[0];
return writePackage(pkgName, version);
})
);
// Write packages with multiple versions
for (const [pkgName, pkgVersions] of Object.entries(multiVersionPkgs)) {
const versionEntries = Object.entries(pkgVersions).sort(
([v1, p1], [v2, p2]) => {
// 1. Package with no parent packages to be hoisted
if (p1.length === 0) {
return -1;
}
if (p2.length === 0) {
return 1;
}
// 2. Newest version to be hoisted
return compareVersions(v1, v2);
}
);
for (const [version, parentPkgs] of versionEntries) {
// Write each version into node_modules/.nitro/{name}@{version}
await writePackage(pkgName, version, `.nitro/${pkgName}@${version}`);
// Link one version to the top level (for indirect bundle deps)
await linkPackage(`.nitro/${pkgName}@${version}`, `${pkgName}`);
// Link to parent packages
for (const parentPkg of parentPkgs) {
const parentPkgName = parentPkg.replace(/@[^@]+$/, "");
await (multiVersionPkgs[parentPkgName]
? linkPackage(
`.nitro/${pkgName}@${version}`,
`.nitro/${parentPkg}/node_modules/${pkgName}`
)
: linkPackage(
`.nitro/${pkgName}@${version}`,
`${parentPkgName}/node_modules/${pkgName}`
));
}
}
}
// Write an informative package.json
const userPkg = await readPackageJSON(
opts.rootDir || process.cwd()
).catch(() => ({}) as PackageJson);
await writePackageJSON(resolve(opts.outDir, "package.json"), {
name: (userPkg.name || "server") + "-prod",
version: userPkg.version || "0.0.0",
type: "module",
private: true,
dependencies: Object.fromEntries(
[
...Object.values(tracedPackages).map((pkg) => [
pkg.name,
Object.keys(pkg.versions)[0],
]),
...Object.entries(usedAliases),
].sort(([a], [b]) => a.localeCompare(b))
),
});
},
};
}
function compareVersions(v1 = "0.0.0", v2 = "0.0.0") {
try {
return semver.lt(v1, v2, { loose: true }) ? 1 : -1;
} catch {
return v1.localeCompare(v2);
}
}
export function applyProductionCondition(exports: PackageJson["exports"]) {
if (
!exports ||
typeof exports === "string" ||
Array.isArray(exports) /* TODO: unhandled */
) {
return;
}
if ("production" in exports) {
if (typeof exports.production === "string") {
exports.default = exports.production;
} else {
Object.assign(exports, exports.production);
}
}
for (const key in exports) {
applyProductionCondition(exports[key as keyof typeof exports]);
}
}
async function isFile(file: string) {
try {
const stat = await fsp.stat(file);
return stat.isFile();
} catch (error) {
if ((error as any)?.code === "ENOENT") {
return false;
}
throw error;
}
}
type Matcher = ((
id: string,
importer?: string
) => Promise<boolean> | boolean) & { score?: number };
export function normalizeMatcher(input: string | RegExp | Matcher): Matcher {
if (typeof input === "function") {
input.score = input.score ?? 10_000;
return input;
}
if (input instanceof RegExp) {
const matcher = ((id: string) => input.test(id)) as Matcher;
matcher.score = input.toString().length;
Object.defineProperty(matcher, "name", { value: `match(${input})` });
return matcher;
}
if (typeof input === "string") {
const pattern = normalize(input);
const matcher = ((id: string) => {
const idWithoutNodeModules = id.split("node_modules/").pop();
return (
id.startsWith(pattern) || idWithoutNodeModules?.startsWith(pattern)
);
}) as Matcher;
matcher.score = input.length;
// Increase score for npm package names to avoid breaking changes
// TODO: Remove in next major version
if (!isAbsolute(input) && input[0] !== ".") {
matcher.score += 1000;
}
Object.defineProperty(matcher, "name", { value: `match(${pattern})` });
return matcher;
}
throw new Error(`Invalid matcher or pattern: ${input}`);
}