dtsroll 1.7.0 → 1.7.2
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.
- package/dist/cli.mjs +5 -5
- package/dist/index-DaiC_aHH.mjs +4729 -0
- package/dist/index.mjs +4 -4
- package/dist/vite.mjs +4 -4
- package/package.json +2 -3
- package/dist/index-r9RZgCg7.mjs +0 -859
package/dist/index-r9RZgCg7.mjs
DELETED
|
@@ -1,859 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import fs from 'node:fs/promises';
|
|
3
|
-
import { rollup } from 'rollup';
|
|
4
|
-
import { dts } from 'rollup-plugin-dts';
|
|
5
|
-
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
6
|
-
import fs$1 from 'node:fs';
|
|
7
|
-
import convert from 'convert-source-map';
|
|
8
|
-
import { up } from 'empathic/find';
|
|
9
|
-
import { resolveImports } from 'resolve-pkg-maps';
|
|
10
|
-
import byteSize from 'byte-size';
|
|
11
|
-
|
|
12
|
-
let enabled = true;
|
|
13
|
-
const globalVar = typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {};
|
|
14
|
-
let supportLevel = 0;
|
|
15
|
-
if (globalVar.process && globalVar.process.env && globalVar.process.stdout) {
|
|
16
|
-
const { FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, COLORTERM } = globalVar.process.env;
|
|
17
|
-
if (NODE_DISABLE_COLORS || NO_COLOR || FORCE_COLOR === "0") {
|
|
18
|
-
enabled = false;
|
|
19
|
-
} else if (FORCE_COLOR === "1" || FORCE_COLOR === "2" || FORCE_COLOR === "3") {
|
|
20
|
-
enabled = true;
|
|
21
|
-
} else if (TERM === "dumb") {
|
|
22
|
-
enabled = false;
|
|
23
|
-
} else if ("CI" in globalVar.process.env && [
|
|
24
|
-
"TRAVIS",
|
|
25
|
-
"CIRCLECI",
|
|
26
|
-
"APPVEYOR",
|
|
27
|
-
"GITLAB_CI",
|
|
28
|
-
"GITHUB_ACTIONS",
|
|
29
|
-
"BUILDKITE",
|
|
30
|
-
"DRONE"
|
|
31
|
-
].some((vendor) => vendor in globalVar.process.env)) {
|
|
32
|
-
enabled = true;
|
|
33
|
-
} else {
|
|
34
|
-
enabled = process.stdout.isTTY;
|
|
35
|
-
}
|
|
36
|
-
if (enabled) {
|
|
37
|
-
if (process.platform === "win32") {
|
|
38
|
-
supportLevel = 3;
|
|
39
|
-
} else {
|
|
40
|
-
if (COLORTERM && (COLORTERM === "truecolor" || COLORTERM === "24bit")) {
|
|
41
|
-
supportLevel = 3;
|
|
42
|
-
} else if (TERM && (TERM.endsWith("-256color") || TERM.endsWith("256"))) {
|
|
43
|
-
supportLevel = 2;
|
|
44
|
-
} else {
|
|
45
|
-
supportLevel = 1;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
let options = {
|
|
51
|
-
enabled,
|
|
52
|
-
supportLevel
|
|
53
|
-
};
|
|
54
|
-
function kolorist(start, end, level = 1) {
|
|
55
|
-
const open = `\x1B[${start}m`;
|
|
56
|
-
const close = `\x1B[${end}m`;
|
|
57
|
-
const regex = new RegExp(`\\x1b\\[${end}m`, "g");
|
|
58
|
-
return (str) => {
|
|
59
|
-
return options.enabled && options.supportLevel >= level ? open + ("" + str).replace(regex, open) + close : "" + str;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
const bold = kolorist(1, 22);
|
|
63
|
-
const dim = kolorist(2, 22);
|
|
64
|
-
const underline = kolorist(4, 24);
|
|
65
|
-
const black = kolorist(30, 39);
|
|
66
|
-
const red = kolorist(31, 39);
|
|
67
|
-
const green = kolorist(32, 39);
|
|
68
|
-
const yellow = kolorist(33, 39);
|
|
69
|
-
const magenta = kolorist(35, 39);
|
|
70
|
-
const lightYellow = kolorist(93, 39);
|
|
71
|
-
const bgYellow = kolorist(43, 49);
|
|
72
|
-
|
|
73
|
-
const cwd = process.cwd();
|
|
74
|
-
|
|
75
|
-
const isPath = (filePath) => filePath[0] === "." || path.isAbsolute(filePath);
|
|
76
|
-
const normalizePath$1 = (filepath) => filepath.replaceAll("\\", "/");
|
|
77
|
-
const getDisplayPath = (fullPath) => {
|
|
78
|
-
const relativePath = path.relative(cwd, fullPath);
|
|
79
|
-
return normalizePath$1(
|
|
80
|
-
relativePath.length < fullPath.length ? relativePath : fullPath
|
|
81
|
-
);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const warningSignUnicode = "\u26A0";
|
|
85
|
-
const warningPrefix = yellow("Warning:");
|
|
86
|
-
const logOutput = (dtsOutput) => {
|
|
87
|
-
console.log(underline("dtsroll"));
|
|
88
|
-
const { inputs } = dtsOutput;
|
|
89
|
-
const isCliInput = inputs[0]?.[1] === void 0;
|
|
90
|
-
console.log(bold(`
|
|
91
|
-
\u{1F4E5} Entry points${isCliInput ? "" : " in package.json"}`));
|
|
92
|
-
console.log(
|
|
93
|
-
inputs.map(([inputFile, inputSource, error]) => {
|
|
94
|
-
const logPath2 = getDisplayPath(inputFile);
|
|
95
|
-
if (error) {
|
|
96
|
-
return ` ${lightYellow(`${warningSignUnicode} ${logPath2} ${dim(error)}`)}`;
|
|
97
|
-
}
|
|
98
|
-
return ` \u2192 ${green(logPath2)}${inputSource ? ` ${dim(`from ${inputSource}`)}` : ""}`;
|
|
99
|
-
}).join("\n")
|
|
100
|
-
);
|
|
101
|
-
if ("error" in dtsOutput) {
|
|
102
|
-
console.error(`${red("Error:")} ${dtsOutput.error}`);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
const {
|
|
106
|
-
outputDirectory,
|
|
107
|
-
output: {
|
|
108
|
-
entries: outputEntries,
|
|
109
|
-
chunks: outputChunks
|
|
110
|
-
},
|
|
111
|
-
size,
|
|
112
|
-
externals
|
|
113
|
-
} = dtsOutput;
|
|
114
|
-
const logPath = `${getDisplayPath(outputDirectory)}/`;
|
|
115
|
-
const logChunk = ({
|
|
116
|
-
file,
|
|
117
|
-
indent,
|
|
118
|
-
bullet,
|
|
119
|
-
color
|
|
120
|
-
}) => {
|
|
121
|
-
const sizeFormatted = byteSize(file.size).toString();
|
|
122
|
-
let log = `${indent}${bullet} ${dim(color(logPath))}${color(file.fileName)} ${sizeFormatted}`;
|
|
123
|
-
const { moduleIds, moduleToPackage } = file;
|
|
124
|
-
log += `
|
|
125
|
-
${moduleIds.sort().map((moduleId, index) => {
|
|
126
|
-
const isLast = index === moduleIds.length - 1;
|
|
127
|
-
const prefix = `${indent} ${isLast ? "\u2514\u2500 " : "\u251C\u2500 "}`;
|
|
128
|
-
const logModuleId = getDisplayPath(moduleId);
|
|
129
|
-
const bareSpecifier = moduleToPackage[moduleId];
|
|
130
|
-
if (bareSpecifier) {
|
|
131
|
-
return `${prefix}${dim(`${magenta(bareSpecifier)} (${logModuleId})`)}`;
|
|
132
|
-
}
|
|
133
|
-
return `${prefix}${dim(logModuleId)}`;
|
|
134
|
-
}).join("\n")}`;
|
|
135
|
-
return log;
|
|
136
|
-
};
|
|
137
|
-
console.log(bold("\n\u{1F4A0} Bundled output"));
|
|
138
|
-
console.log(
|
|
139
|
-
outputEntries.map((file) => logChunk({
|
|
140
|
-
file,
|
|
141
|
-
indent: " ",
|
|
142
|
-
bullet: "\u25CF",
|
|
143
|
-
color: green
|
|
144
|
-
})).join("\n\n")
|
|
145
|
-
);
|
|
146
|
-
if (outputChunks.length > 0) {
|
|
147
|
-
console.log(bold("\n Chunks"));
|
|
148
|
-
console.log(
|
|
149
|
-
outputChunks.map((file) => logChunk({
|
|
150
|
-
file,
|
|
151
|
-
indent: " ",
|
|
152
|
-
bullet: "\u25A0",
|
|
153
|
-
color: yellow
|
|
154
|
-
})).join("\n\n")
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
console.log(bold("\n\u2696\uFE0F Size savings"));
|
|
158
|
-
const difference = size.input - size.output;
|
|
159
|
-
const direction = difference > 0 ? "decrease" : "increase";
|
|
160
|
-
const percentage = (Math.abs(difference / size.input) * 100).toFixed(0);
|
|
161
|
-
console.log(` Input source size: ${byteSize(size.input).toString()}`);
|
|
162
|
-
console.log(` Bundled output size: ${byteSize(size.output).toString()}${difference === 0 ? "" : ` (${percentage}% ${direction})`}`);
|
|
163
|
-
if (externals.length > 0) {
|
|
164
|
-
console.log(bold("\n\u{1F4E6} External packages"));
|
|
165
|
-
console.log(
|
|
166
|
-
externals.map(([packageName, reason, devTypePackage]) => {
|
|
167
|
-
let stdout = ` \u2500 ${magenta(packageName)} ${dim(`externalized ${reason}`)}`;
|
|
168
|
-
if (devTypePackage) {
|
|
169
|
-
stdout += `
|
|
170
|
-
${warningPrefix} ${magenta(devTypePackage)} should not be in devDependencies if ${magenta(packageName)} is externalized`;
|
|
171
|
-
}
|
|
172
|
-
return stdout;
|
|
173
|
-
}).sort().join("\n")
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
class DtsrollBuildError extends Error {
|
|
179
|
-
id;
|
|
180
|
-
importChain;
|
|
181
|
-
constructor(message, id, importChain) {
|
|
182
|
-
super(message);
|
|
183
|
-
this.name = "DtsrollBuildError";
|
|
184
|
-
this.id = id;
|
|
185
|
-
this.importChain = importChain;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const dtsExtensions = [".d.ts", ".d.cts", ".d.mts"];
|
|
190
|
-
const isDts = (fileName) => dtsExtensions.some((extension) => fileName.endsWith(extension));
|
|
191
|
-
|
|
192
|
-
const isValidIdentifier = /^[$_\p{ID_Start}][$\u200C\u200D\p{ID_Continue}]*$/u;
|
|
193
|
-
const reservedWords = /* @__PURE__ */ new Set([
|
|
194
|
-
"do",
|
|
195
|
-
"if",
|
|
196
|
-
"in",
|
|
197
|
-
"for",
|
|
198
|
-
"int",
|
|
199
|
-
"new",
|
|
200
|
-
"try",
|
|
201
|
-
"var",
|
|
202
|
-
"byte",
|
|
203
|
-
"case",
|
|
204
|
-
"char",
|
|
205
|
-
"else",
|
|
206
|
-
"enum",
|
|
207
|
-
"goto",
|
|
208
|
-
"long",
|
|
209
|
-
"null",
|
|
210
|
-
"this",
|
|
211
|
-
"true",
|
|
212
|
-
"void",
|
|
213
|
-
"with",
|
|
214
|
-
"break",
|
|
215
|
-
"catch",
|
|
216
|
-
"class",
|
|
217
|
-
"const",
|
|
218
|
-
"false",
|
|
219
|
-
"final",
|
|
220
|
-
"float",
|
|
221
|
-
"short",
|
|
222
|
-
"super",
|
|
223
|
-
"throw",
|
|
224
|
-
"while",
|
|
225
|
-
"delete",
|
|
226
|
-
"double",
|
|
227
|
-
"export",
|
|
228
|
-
"import",
|
|
229
|
-
"native",
|
|
230
|
-
"public",
|
|
231
|
-
"return",
|
|
232
|
-
"static",
|
|
233
|
-
"switch",
|
|
234
|
-
"throws",
|
|
235
|
-
"typeof",
|
|
236
|
-
"boolean",
|
|
237
|
-
"default",
|
|
238
|
-
"extends",
|
|
239
|
-
"finally",
|
|
240
|
-
"package",
|
|
241
|
-
"private",
|
|
242
|
-
"abstract",
|
|
243
|
-
"continue",
|
|
244
|
-
"debugger",
|
|
245
|
-
"function",
|
|
246
|
-
"volatile",
|
|
247
|
-
"interface",
|
|
248
|
-
"protected",
|
|
249
|
-
"transient",
|
|
250
|
-
"implements",
|
|
251
|
-
"instanceof",
|
|
252
|
-
"synchronized"
|
|
253
|
-
]);
|
|
254
|
-
const propertyNeedsQuotes = (property) => !isValidIdentifier.test(property) || reservedWords.has(property);
|
|
255
|
-
|
|
256
|
-
const pathExists = async (filePath) => fs.access(filePath).then(() => true, () => false);
|
|
257
|
-
|
|
258
|
-
const typesPrefix = "@types/";
|
|
259
|
-
const getOriginalPackageName = (typePackageName) => {
|
|
260
|
-
let originalPackageName = typePackageName.slice(typesPrefix.length);
|
|
261
|
-
if (originalPackageName.includes("__")) {
|
|
262
|
-
originalPackageName = `@${originalPackageName.replace("__", "/")}`;
|
|
263
|
-
}
|
|
264
|
-
return originalPackageName;
|
|
265
|
-
};
|
|
266
|
-
const getPackageName = (id) => {
|
|
267
|
-
let indexOfSlash = id.indexOf("/");
|
|
268
|
-
if (indexOfSlash === -1) {
|
|
269
|
-
return id;
|
|
270
|
-
}
|
|
271
|
-
if (id[0] === "@") {
|
|
272
|
-
const secondSlash = id.indexOf("/", indexOfSlash + 1);
|
|
273
|
-
if (secondSlash === -1) {
|
|
274
|
-
return id;
|
|
275
|
-
}
|
|
276
|
-
indexOfSlash = secondSlash;
|
|
277
|
-
}
|
|
278
|
-
return id.slice(0, indexOfSlash);
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
const normalizePath = (filePath) => filePath.replaceAll("\\", "/");
|
|
282
|
-
const getAllFiles = async (directoryPath, dontShortenPath) => {
|
|
283
|
-
const directoryFiles = await fs.readdir(directoryPath, { withFileTypes: true });
|
|
284
|
-
const fileTree = await Promise.all(
|
|
285
|
-
directoryFiles.map(async (entry) => {
|
|
286
|
-
const filePath = path.join(directoryPath, entry.name);
|
|
287
|
-
if (entry.isDirectory()) {
|
|
288
|
-
const files = await getAllFiles(filePath, true);
|
|
289
|
-
return dontShortenPath ? files : files.map((file) => `./${normalizePath(path.relative(directoryPath, file))}`);
|
|
290
|
-
}
|
|
291
|
-
return dontShortenPath ? filePath : `./${normalizePath(path.relative(directoryPath, filePath))}`;
|
|
292
|
-
})
|
|
293
|
-
);
|
|
294
|
-
return fileTree.flat();
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
const readPackageJson = async (filePath) => {
|
|
298
|
-
const packageJsonString = await fs.readFile(filePath, "utf8");
|
|
299
|
-
return JSON.parse(packageJsonString);
|
|
300
|
-
};
|
|
301
|
-
const traverseExports = (exportValue, propertyPath) => {
|
|
302
|
-
if (typeof exportValue === "string") {
|
|
303
|
-
return [[exportValue, propertyPath]];
|
|
304
|
-
}
|
|
305
|
-
if (Array.isArray(exportValue)) {
|
|
306
|
-
return exportValue.flatMap((value, index) => traverseExports(value, `${propertyPath}[${index}]`));
|
|
307
|
-
}
|
|
308
|
-
if (typeof exportValue === "object" && exportValue !== null) {
|
|
309
|
-
return Object.entries(exportValue).flatMap(([property, value]) => {
|
|
310
|
-
const newProperty = propertyNeedsQuotes(property) ? `["${property}"]` : `.${property}`;
|
|
311
|
-
return traverseExports(value, propertyPath + newProperty);
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
return [];
|
|
315
|
-
};
|
|
316
|
-
const getDtsEntryPoints = async (packageJson, packageJsonDirectory) => {
|
|
317
|
-
const entryPoints = {};
|
|
318
|
-
const addEntry = (subpath, from) => {
|
|
319
|
-
if (!isDts(subpath)) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
const entryPath = path.join(packageJsonDirectory, subpath);
|
|
323
|
-
if (!entryPoints[entryPath]) {
|
|
324
|
-
entryPoints[entryPath] = from;
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
if (packageJson.types) {
|
|
328
|
-
addEntry(packageJson.types, "types");
|
|
329
|
-
}
|
|
330
|
-
if (packageJson.typings) {
|
|
331
|
-
addEntry(packageJson.typings, "typings");
|
|
332
|
-
}
|
|
333
|
-
if (packageJson.exports) {
|
|
334
|
-
const subpaths = traverseExports(packageJson.exports, "exports");
|
|
335
|
-
let packageFiles;
|
|
336
|
-
for (const [subpath, fromProperty] of subpaths) {
|
|
337
|
-
if (!subpath.includes("*")) {
|
|
338
|
-
addEntry(subpath, fromProperty);
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
if (!packageFiles) {
|
|
342
|
-
packageFiles = await getAllFiles(packageJsonDirectory);
|
|
343
|
-
}
|
|
344
|
-
const [prefix, suffix] = subpath.split("*", 2);
|
|
345
|
-
for (const file of packageFiles) {
|
|
346
|
-
if (file.startsWith(prefix) && file.endsWith(suffix)) {
|
|
347
|
-
addEntry(file, fromProperty);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
return entryPoints;
|
|
353
|
-
};
|
|
354
|
-
const externalizedDependencies = [
|
|
355
|
-
"dependencies",
|
|
356
|
-
"peerDependencies",
|
|
357
|
-
"optionalDependencies"
|
|
358
|
-
];
|
|
359
|
-
const getExternals = (packageJson) => {
|
|
360
|
-
const external = /* @__PURE__ */ new Map();
|
|
361
|
-
for (const dependencyType of externalizedDependencies) {
|
|
362
|
-
const dependencyObject = packageJson[dependencyType];
|
|
363
|
-
if (dependencyObject) {
|
|
364
|
-
const dependencyNames = Object.keys(dependencyObject);
|
|
365
|
-
for (const dependencyName of dependencyNames) {
|
|
366
|
-
external.set(dependencyName, `by package.json ${dependencyType}`);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
return external;
|
|
371
|
-
};
|
|
372
|
-
const getPackageJson = async (cwd) => {
|
|
373
|
-
const packageJsonPath = path.resolve(cwd, "package.json");
|
|
374
|
-
const exists = await pathExists(packageJsonPath);
|
|
375
|
-
if (!exists) {
|
|
376
|
-
return;
|
|
377
|
-
}
|
|
378
|
-
let packageJson;
|
|
379
|
-
try {
|
|
380
|
-
packageJson = await readPackageJson(packageJsonPath);
|
|
381
|
-
} catch (error) {
|
|
382
|
-
throw new Error(`Failed to parse package.json at ${packageJsonPath}: ${error.message}`);
|
|
383
|
-
}
|
|
384
|
-
return {
|
|
385
|
-
getExternals: () => getExternals(packageJson),
|
|
386
|
-
getDtsEntryPoints: () => getDtsEntryPoints(packageJson, path.dirname(packageJsonPath)),
|
|
387
|
-
devTypePackages: !packageJson.private && packageJson.devDependencies ? Object.fromEntries(
|
|
388
|
-
Object.keys(packageJson.devDependencies).filter((dep) => dep.startsWith(typesPrefix)).map((dep) => [getOriginalPackageName(dep), dep])
|
|
389
|
-
) : {}
|
|
390
|
-
};
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
const getCommonDirectory = (filePaths) => {
|
|
394
|
-
const splitPaths = filePaths.map((filePath) => filePath.split(path.sep).slice(0, -1));
|
|
395
|
-
const commonPath = [];
|
|
396
|
-
const [firstPath] = splitPaths;
|
|
397
|
-
for (let i = 0; i < firstPath.length; i += 1) {
|
|
398
|
-
const segment = firstPath[i];
|
|
399
|
-
const segmentIsCommon = splitPaths.every((pathParts) => pathParts[i] === segment);
|
|
400
|
-
if (!segmentIsCommon) {
|
|
401
|
-
break;
|
|
402
|
-
}
|
|
403
|
-
commonPath.push(segment);
|
|
404
|
-
}
|
|
405
|
-
return commonPath.join(path.sep);
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
const validateInput = async (inputFiles) => {
|
|
409
|
-
if (!inputFiles) {
|
|
410
|
-
throw new Error("No input files");
|
|
411
|
-
}
|
|
412
|
-
const isCliInput = Array.isArray(inputFiles);
|
|
413
|
-
const inputNormalized = isCliInput ? inputFiles.map((i) => [i]) : Object.entries(inputFiles);
|
|
414
|
-
return await Promise.all(inputNormalized.map(
|
|
415
|
-
async ([inputFile, inputSource]) => {
|
|
416
|
-
if (!isDts(inputFile)) {
|
|
417
|
-
return [inputFile, inputSource, "Ignoring non-d.ts input"];
|
|
418
|
-
}
|
|
419
|
-
const exists = await pathExists(inputFile);
|
|
420
|
-
if (!exists) {
|
|
421
|
-
return [inputFile, inputSource, "File not found"];
|
|
422
|
-
}
|
|
423
|
-
return [inputFile, inputSource];
|
|
424
|
-
}
|
|
425
|
-
));
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
const createExternalizePlugin = (configuredExternals) => {
|
|
429
|
-
const resolvedBareSpecifiers = /* @__PURE__ */ new Map();
|
|
430
|
-
const importPath = /* @__PURE__ */ new Map();
|
|
431
|
-
const externalized = /* @__PURE__ */ new Map();
|
|
432
|
-
const externalizePlugin = {
|
|
433
|
-
name: "externalize",
|
|
434
|
-
async resolveId(id, importer, options) {
|
|
435
|
-
const packageName = !isPath(id) && getPackageName(id);
|
|
436
|
-
if (packageName) {
|
|
437
|
-
const externalReason = configuredExternals.get(packageName);
|
|
438
|
-
if (externalReason) {
|
|
439
|
-
externalized.set(packageName, externalReason);
|
|
440
|
-
return {
|
|
441
|
-
id,
|
|
442
|
-
external: true
|
|
443
|
-
};
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
const resolved = await this.resolve(id, importer, options);
|
|
447
|
-
if (resolved) {
|
|
448
|
-
if (packageName) {
|
|
449
|
-
resolvedBareSpecifiers.set(resolved.id, id);
|
|
450
|
-
}
|
|
451
|
-
if (
|
|
452
|
-
// Self imports happen
|
|
453
|
-
importer && resolved.id !== importer && importPath.get(importer) !== resolved.id
|
|
454
|
-
) {
|
|
455
|
-
importPath.set(resolved.id, importer);
|
|
456
|
-
}
|
|
457
|
-
return resolved;
|
|
458
|
-
}
|
|
459
|
-
if (packageName) {
|
|
460
|
-
externalized.set(packageName, "because unresolvable");
|
|
461
|
-
return {
|
|
462
|
-
id,
|
|
463
|
-
external: true
|
|
464
|
-
};
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
const getPackageEntryPoint = (subpackagePath) => {
|
|
469
|
-
let i = 0;
|
|
470
|
-
let lastEntry = subpackagePath;
|
|
471
|
-
do {
|
|
472
|
-
if (resolvedBareSpecifiers.has(lastEntry)) {
|
|
473
|
-
return resolvedBareSpecifiers.get(lastEntry);
|
|
474
|
-
}
|
|
475
|
-
lastEntry = importPath.get(lastEntry);
|
|
476
|
-
i += 1;
|
|
477
|
-
} while (lastEntry && i < 100);
|
|
478
|
-
};
|
|
479
|
-
return {
|
|
480
|
-
externalizePlugin,
|
|
481
|
-
externalized,
|
|
482
|
-
getPackageEntryPoint
|
|
483
|
-
};
|
|
484
|
-
};
|
|
485
|
-
|
|
486
|
-
const createImportChainPlugin = () => {
|
|
487
|
-
const importerMap = /* @__PURE__ */ new Map();
|
|
488
|
-
const plugin = {
|
|
489
|
-
name: "import-chain-tracker",
|
|
490
|
-
buildStart: () => {
|
|
491
|
-
importerMap.clear();
|
|
492
|
-
},
|
|
493
|
-
async resolveId(source, importer) {
|
|
494
|
-
if (!importer) {
|
|
495
|
-
return null;
|
|
496
|
-
}
|
|
497
|
-
const resolved = await this.resolve(source, importer, { skipSelf: true });
|
|
498
|
-
if (resolved && !resolved.external && !importerMap.has(resolved.id)) {
|
|
499
|
-
importerMap.set(resolved.id, importer);
|
|
500
|
-
}
|
|
501
|
-
return null;
|
|
502
|
-
}
|
|
503
|
-
};
|
|
504
|
-
const getImportChain = (errorFileId) => {
|
|
505
|
-
const chain = [];
|
|
506
|
-
let current = errorFileId;
|
|
507
|
-
while (current) {
|
|
508
|
-
chain.unshift(current);
|
|
509
|
-
current = importerMap.get(current);
|
|
510
|
-
}
|
|
511
|
-
return chain;
|
|
512
|
-
};
|
|
513
|
-
return {
|
|
514
|
-
plugin,
|
|
515
|
-
getImportChain
|
|
516
|
-
};
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
const tryReadFile = async (filePath) => {
|
|
520
|
-
try {
|
|
521
|
-
return await fs$1.promises.readFile(filePath, "utf8");
|
|
522
|
-
} catch {
|
|
523
|
-
return null;
|
|
524
|
-
}
|
|
525
|
-
};
|
|
526
|
-
const loadSourceMap = async (codePath, code) => {
|
|
527
|
-
const adjacentMapPath = `${codePath}.map`;
|
|
528
|
-
const adjacentMapContent = await tryReadFile(adjacentMapPath);
|
|
529
|
-
if (adjacentMapContent) {
|
|
530
|
-
try {
|
|
531
|
-
const converter = convert.fromJSON(adjacentMapContent);
|
|
532
|
-
return {
|
|
533
|
-
map: converter.toObject(),
|
|
534
|
-
mapPath: adjacentMapPath
|
|
535
|
-
};
|
|
536
|
-
} catch {
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
try {
|
|
540
|
-
const inlineConverter = convert.fromSource(code);
|
|
541
|
-
if (inlineConverter) {
|
|
542
|
-
return {
|
|
543
|
-
map: inlineConverter.toObject(),
|
|
544
|
-
mapPath: codePath
|
|
545
|
-
};
|
|
546
|
-
}
|
|
547
|
-
} catch {
|
|
548
|
-
}
|
|
549
|
-
try {
|
|
550
|
-
const regex = new RegExp(convert.mapFileCommentRegex.source);
|
|
551
|
-
const commentMatch = regex.exec(code);
|
|
552
|
-
const referencedPath = commentMatch?.[1] ?? commentMatch?.[2];
|
|
553
|
-
if (!referencedPath) {
|
|
554
|
-
return;
|
|
555
|
-
}
|
|
556
|
-
const mapFilePath = path.join(path.dirname(codePath), referencedPath);
|
|
557
|
-
const mapContent = await tryReadFile(mapFilePath);
|
|
558
|
-
if (!mapContent) {
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
const converter = convert.fromJSON(mapContent);
|
|
562
|
-
return {
|
|
563
|
-
map: converter.toObject(),
|
|
564
|
-
mapPath: mapFilePath
|
|
565
|
-
};
|
|
566
|
-
} catch {
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
const loadInputSourcemapsPlugin = () => ({
|
|
570
|
-
name: "load-input-sourcemaps",
|
|
571
|
-
async load(id) {
|
|
572
|
-
const isDts = dtsExtensions.some((extension) => id.endsWith(extension));
|
|
573
|
-
if (!isDts) {
|
|
574
|
-
return null;
|
|
575
|
-
}
|
|
576
|
-
const code = await tryReadFile(id);
|
|
577
|
-
if (!code) {
|
|
578
|
-
return null;
|
|
579
|
-
}
|
|
580
|
-
const result = await loadSourceMap(id, code);
|
|
581
|
-
if (!result) {
|
|
582
|
-
return { code };
|
|
583
|
-
}
|
|
584
|
-
const { map: inputMap, mapPath } = result;
|
|
585
|
-
const sourceRoot = path.resolve(path.dirname(mapPath), inputMap.sourceRoot ?? ".");
|
|
586
|
-
const sources = inputMap.sources.map(
|
|
587
|
-
(source) => path.isAbsolute(source) ? source : path.resolve(sourceRoot, source)
|
|
588
|
-
);
|
|
589
|
-
const sourcesContentRaw = await Promise.all(
|
|
590
|
-
sources.map(async (source, index) => inputMap.sourcesContent?.[index] ?? tryReadFile(source))
|
|
591
|
-
);
|
|
592
|
-
const sourcesContent = sourcesContentRaw.filter(
|
|
593
|
-
(content) => content !== null
|
|
594
|
-
);
|
|
595
|
-
return {
|
|
596
|
-
code,
|
|
597
|
-
map: {
|
|
598
|
-
version: inputMap.version,
|
|
599
|
-
names: inputMap.names,
|
|
600
|
-
sources,
|
|
601
|
-
mappings: inputMap.mappings,
|
|
602
|
-
...sourcesContent.length > 0 ? { sourcesContent } : {},
|
|
603
|
-
...inputMap.file ? { file: inputMap.file } : {}
|
|
604
|
-
}
|
|
605
|
-
};
|
|
606
|
-
}
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
const nodeModules = `${path.sep}node_modules${path.sep}`;
|
|
610
|
-
const removeBundledModulesPlugin = (outputDirectory, sizeRef) => {
|
|
611
|
-
let deleteFiles = [];
|
|
612
|
-
return {
|
|
613
|
-
name: "remove-bundled-modules",
|
|
614
|
-
transform: {
|
|
615
|
-
// Get size of raw code before other transformations
|
|
616
|
-
order: "pre",
|
|
617
|
-
handler: (code) => ({
|
|
618
|
-
meta: {
|
|
619
|
-
size: Buffer.byteLength(code)
|
|
620
|
-
}
|
|
621
|
-
})
|
|
622
|
-
},
|
|
623
|
-
async generateBundle(options, bundle) {
|
|
624
|
-
const modules = Object.values(bundle);
|
|
625
|
-
const bundledFiles = Array.from(new Set(modules.flatMap(({ moduleIds }) => moduleIds)));
|
|
626
|
-
let totalSize = 0;
|
|
627
|
-
for (const moduleId of bundledFiles) {
|
|
628
|
-
const moduleInfo = this.getModuleInfo(moduleId);
|
|
629
|
-
const size = moduleInfo?.meta?.size;
|
|
630
|
-
if (typeof size === "number") {
|
|
631
|
-
totalSize += size;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
sizeRef.value = totalSize;
|
|
635
|
-
const outputFiles = new Set(modules.map(({ fileName }) => path.join(options.dir, fileName)));
|
|
636
|
-
deleteFiles = bundledFiles.filter((moduleId) => moduleId && moduleId.startsWith(outputDirectory) && !moduleId.includes(nodeModules) && !outputFiles.has(moduleId));
|
|
637
|
-
},
|
|
638
|
-
writeBundle: async () => {
|
|
639
|
-
await Promise.all(
|
|
640
|
-
deleteFiles.map((moduleId) => fs.rm(moduleId))
|
|
641
|
-
);
|
|
642
|
-
}
|
|
643
|
-
};
|
|
644
|
-
};
|
|
645
|
-
|
|
646
|
-
const packageJsonCache = /* @__PURE__ */ new Map();
|
|
647
|
-
const findPackageJsonUp = async (cwd) => {
|
|
648
|
-
const packageJsonPath = up("package.json", { cwd });
|
|
649
|
-
if (!packageJsonPath) {
|
|
650
|
-
return void 0;
|
|
651
|
-
}
|
|
652
|
-
const packageRoot = path.dirname(packageJsonPath);
|
|
653
|
-
let packageJson = packageJsonCache.get(packageRoot);
|
|
654
|
-
if (!packageJson) {
|
|
655
|
-
try {
|
|
656
|
-
const content = await fs.readFile(packageJsonPath, "utf8");
|
|
657
|
-
packageJson = JSON.parse(content);
|
|
658
|
-
packageJsonCache.set(packageRoot, packageJson);
|
|
659
|
-
} catch {
|
|
660
|
-
return void 0;
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
if (packageJson.imports) {
|
|
664
|
-
return {
|
|
665
|
-
imports: packageJson.imports,
|
|
666
|
-
packageRoot
|
|
667
|
-
};
|
|
668
|
-
}
|
|
669
|
-
};
|
|
670
|
-
const resolveSubpathImportsPlugin = () => ({
|
|
671
|
-
name: "resolve-subpath-imports",
|
|
672
|
-
async resolveId(id, importer) {
|
|
673
|
-
if (id[0] !== "#" || !importer) {
|
|
674
|
-
return null;
|
|
675
|
-
}
|
|
676
|
-
const result = await findPackageJsonUp(path.dirname(importer));
|
|
677
|
-
if (!result) {
|
|
678
|
-
return null;
|
|
679
|
-
}
|
|
680
|
-
const { imports, packageRoot } = result;
|
|
681
|
-
let resolvedPaths;
|
|
682
|
-
try {
|
|
683
|
-
resolvedPaths = resolveImports(imports, id, ["types", "import"]);
|
|
684
|
-
} catch {
|
|
685
|
-
return null;
|
|
686
|
-
}
|
|
687
|
-
if (resolvedPaths.length === 0) {
|
|
688
|
-
return null;
|
|
689
|
-
}
|
|
690
|
-
return this.resolve(
|
|
691
|
-
path.join(packageRoot, resolvedPaths[0]),
|
|
692
|
-
importer,
|
|
693
|
-
{ skipSelf: true }
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
|
|
698
|
-
const createInputMap = (input, outputDirectory) => Object.fromEntries(
|
|
699
|
-
input.map((inputFile) => [
|
|
700
|
-
inputFile.slice(outputDirectory.length + 1),
|
|
701
|
-
inputFile
|
|
702
|
-
])
|
|
703
|
-
);
|
|
704
|
-
const build = async (input, outputDirectory, externals, mode, conditions, sourcemap) => {
|
|
705
|
-
const {
|
|
706
|
-
externalizePlugin,
|
|
707
|
-
externalized,
|
|
708
|
-
getPackageEntryPoint
|
|
709
|
-
} = createExternalizePlugin(externals);
|
|
710
|
-
const { plugin: importChainPlugin, getImportChain } = createImportChainPlugin();
|
|
711
|
-
const sizeRef = {};
|
|
712
|
-
const rollupConfig = {
|
|
713
|
-
input: createInputMap(input, outputDirectory),
|
|
714
|
-
output: {
|
|
715
|
-
sourcemap,
|
|
716
|
-
dir: outputDirectory,
|
|
717
|
-
entryFileNames: "[name]",
|
|
718
|
-
chunkFileNames: "_dtsroll-chunks/[hash]-[name].ts"
|
|
719
|
-
},
|
|
720
|
-
plugins: [
|
|
721
|
-
importChainPlugin,
|
|
722
|
-
externalizePlugin,
|
|
723
|
-
removeBundledModulesPlugin(outputDirectory, sizeRef),
|
|
724
|
-
resolveSubpathImportsPlugin(),
|
|
725
|
-
// Load existing .d.ts.map files to chain sourcemaps back to original .ts sources
|
|
726
|
-
sourcemap && loadInputSourcemapsPlugin(),
|
|
727
|
-
nodeResolve({
|
|
728
|
-
extensions: [".ts", ...dtsExtensions],
|
|
729
|
-
exportConditions: conditions
|
|
730
|
-
}),
|
|
731
|
-
dts({
|
|
732
|
-
respectExternal: true
|
|
733
|
-
/**
|
|
734
|
-
* Setting a tsconfig or compilerOptions shouldn't be necessary since
|
|
735
|
-
* we're dealing with pre-compiled d.ts files
|
|
736
|
-
*
|
|
737
|
-
* But may be something we need to support if we want to support
|
|
738
|
-
* aliases in the future
|
|
739
|
-
*/
|
|
740
|
-
})
|
|
741
|
-
]
|
|
742
|
-
};
|
|
743
|
-
try {
|
|
744
|
-
const rollupBuild = await rollup(rollupConfig);
|
|
745
|
-
const built = await rollupBuild[mode](rollupConfig.output);
|
|
746
|
-
await rollupBuild.close();
|
|
747
|
-
return {
|
|
748
|
-
built,
|
|
749
|
-
externalized,
|
|
750
|
-
getPackageEntryPoint,
|
|
751
|
-
sourceSize: sizeRef.value ?? 0
|
|
752
|
-
};
|
|
753
|
-
} catch (error) {
|
|
754
|
-
if (error instanceof Error && "id" in error && typeof error.id === "string") {
|
|
755
|
-
throw new DtsrollBuildError(
|
|
756
|
-
error.message,
|
|
757
|
-
error.id,
|
|
758
|
-
getImportChain(error.id)
|
|
759
|
-
);
|
|
760
|
-
}
|
|
761
|
-
throw error;
|
|
762
|
-
}
|
|
763
|
-
};
|
|
764
|
-
|
|
765
|
-
const dtsroll = async ({
|
|
766
|
-
cwd = process.cwd(),
|
|
767
|
-
inputs,
|
|
768
|
-
external,
|
|
769
|
-
conditions,
|
|
770
|
-
dryRun,
|
|
771
|
-
sourcemap
|
|
772
|
-
} = {}) => {
|
|
773
|
-
const pkgJson = await getPackageJson(cwd);
|
|
774
|
-
const externals = pkgJson ? pkgJson.getExternals() : /* @__PURE__ */ new Map();
|
|
775
|
-
if (external && external.length > 0) {
|
|
776
|
-
if (pkgJson) {
|
|
777
|
-
console.warn(`${warningPrefix} The --external flag is only supported when there is no package.json`);
|
|
778
|
-
} else {
|
|
779
|
-
for (const externalDependency of external) {
|
|
780
|
-
externals.set(externalDependency, "by --external flag");
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
const manualInput = inputs && inputs.length > 0;
|
|
785
|
-
const validatedInputs = await validateInput(
|
|
786
|
-
manualInput ? inputs.map((file) => path.resolve(file)) : await pkgJson?.getDtsEntryPoints()
|
|
787
|
-
);
|
|
788
|
-
const inputFiles = validatedInputs.filter((input) => !input[2]).map(([file]) => file);
|
|
789
|
-
if (inputFiles.length === 0) {
|
|
790
|
-
return {
|
|
791
|
-
inputs: validatedInputs,
|
|
792
|
-
error: "No input files"
|
|
793
|
-
};
|
|
794
|
-
}
|
|
795
|
-
const outputDirectory = getCommonDirectory(inputFiles);
|
|
796
|
-
const {
|
|
797
|
-
built,
|
|
798
|
-
externalized,
|
|
799
|
-
getPackageEntryPoint,
|
|
800
|
-
sourceSize
|
|
801
|
-
} = await build(
|
|
802
|
-
inputFiles,
|
|
803
|
-
outputDirectory,
|
|
804
|
-
externals,
|
|
805
|
-
dryRun ? "generate" : "write",
|
|
806
|
-
conditions,
|
|
807
|
-
sourcemap
|
|
808
|
-
);
|
|
809
|
-
let outputSize = 0;
|
|
810
|
-
const outputEntries = [];
|
|
811
|
-
const outputChunks = [];
|
|
812
|
-
const moduleImports = /* @__PURE__ */ new Set();
|
|
813
|
-
const chunks = built.output.filter((file) => file.type === "chunk");
|
|
814
|
-
for (const file of chunks) {
|
|
815
|
-
const size = Buffer.byteLength(file.code);
|
|
816
|
-
outputSize += size;
|
|
817
|
-
const moduleToPackage = Object.fromEntries(
|
|
818
|
-
file.moduleIds.map((moduleId) => [moduleId, getPackageEntryPoint(moduleId)])
|
|
819
|
-
);
|
|
820
|
-
const chunkWithSize = Object.assign(file, {
|
|
821
|
-
size,
|
|
822
|
-
moduleToPackage
|
|
823
|
-
});
|
|
824
|
-
if (chunkWithSize.isEntry) {
|
|
825
|
-
outputEntries.push(chunkWithSize);
|
|
826
|
-
} else {
|
|
827
|
-
outputChunks.push(chunkWithSize);
|
|
828
|
-
}
|
|
829
|
-
for (const id of file.imports) {
|
|
830
|
-
moduleImports.add(getPackageName(id));
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
const externalPackages = [];
|
|
834
|
-
moduleImports.forEach((importedSpecifier) => {
|
|
835
|
-
const reason = externalized.get(importedSpecifier);
|
|
836
|
-
if (reason) {
|
|
837
|
-
externalPackages.push([
|
|
838
|
-
importedSpecifier,
|
|
839
|
-
reason,
|
|
840
|
-
pkgJson?.devTypePackages?.[importedSpecifier]
|
|
841
|
-
]);
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
return {
|
|
845
|
-
inputs: validatedInputs,
|
|
846
|
-
outputDirectory,
|
|
847
|
-
output: {
|
|
848
|
-
entries: outputEntries,
|
|
849
|
-
chunks: outputChunks
|
|
850
|
-
},
|
|
851
|
-
size: {
|
|
852
|
-
input: sourceSize,
|
|
853
|
-
output: outputSize
|
|
854
|
-
},
|
|
855
|
-
externals: externalPackages
|
|
856
|
-
};
|
|
857
|
-
};
|
|
858
|
-
|
|
859
|
-
export { DtsrollBuildError as D, black as a, bgYellow as b, dtsroll as d, logOutput as l };
|