bunup 0.8.41 → 0.8.42
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/index.js +1335 -49
- package/dist/index.js +772 -6
- package/dist/plugins/index.d.ts +1 -7
- package/dist/plugins/index.js +389 -73
- package/package.json +4 -4
- package/dist/chunk-5ng6vk19.js +0 -284
- package/dist/chunk-5s4mpe5d.js +0 -355
- package/dist/chunk-a7j07gk7.js +0 -25
- package/dist/chunk-c1eyecm3.js +0 -367
- package/dist/chunk-gh7z7s46.js +0 -30
- package/dist/chunk-q3kvndyh.js +0 -155
package/dist/index.js
CHANGED
|
@@ -1,10 +1,410 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
+
for (let key of __getOwnPropNames(mod))
|
|
11
|
+
if (!__hasOwnProp.call(to, key))
|
|
12
|
+
__defProp(to, key, {
|
|
13
|
+
get: () => mod[key],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __export = (target, all) => {
|
|
19
|
+
for (var name in all)
|
|
20
|
+
__defProp(target, name, {
|
|
21
|
+
get: all[name],
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
set: (newValue) => all[name] = () => newValue
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
28
|
+
var __require = import.meta.require;
|
|
29
|
+
|
|
30
|
+
// src/logger.ts
|
|
31
|
+
import pc from "picocolors";
|
|
32
|
+
function setSilent(value) {
|
|
33
|
+
silent = value ?? false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class Logger {
|
|
37
|
+
static instance;
|
|
38
|
+
loggedOnceMessages = new Set;
|
|
39
|
+
constructor() {}
|
|
40
|
+
static getInstance() {
|
|
41
|
+
if (!Logger.instance) {
|
|
42
|
+
Logger.instance = new Logger;
|
|
43
|
+
}
|
|
44
|
+
return Logger.instance;
|
|
45
|
+
}
|
|
46
|
+
dispose() {
|
|
47
|
+
this.loggedOnceMessages.clear();
|
|
48
|
+
}
|
|
49
|
+
shouldLog(options) {
|
|
50
|
+
if (!options?.once) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (this.loggedOnceMessages.has(options.once)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
this.loggedOnceMessages.add(options.once);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
getIcon(type, tick) {
|
|
60
|
+
if (tick) {
|
|
61
|
+
return pc.green("\u2713");
|
|
62
|
+
}
|
|
63
|
+
const iconMap = {
|
|
64
|
+
info: pc.blue("i"),
|
|
65
|
+
warn: pc.yellow("!"),
|
|
66
|
+
error: pc.red("\u2715")
|
|
67
|
+
};
|
|
68
|
+
return iconMap[type];
|
|
69
|
+
}
|
|
70
|
+
formatIdentifier(identifier) {
|
|
71
|
+
return identifier ? ` ${pc.bgBlue(pc.black(` ${identifier} `))}` : "";
|
|
72
|
+
}
|
|
73
|
+
formatMessage(options) {
|
|
74
|
+
const {
|
|
75
|
+
message,
|
|
76
|
+
identifier,
|
|
77
|
+
muted = false,
|
|
78
|
+
tick = false,
|
|
79
|
+
type = "info"
|
|
80
|
+
} = options;
|
|
81
|
+
const icon = this.getIcon(type, tick);
|
|
82
|
+
const styledMessage = muted ? pc.dim(message) : type === "error" ? pc.red(message) : type === "warn" ? pc.yellow(message) : message;
|
|
83
|
+
const identifierPart = this.formatIdentifier(identifier);
|
|
84
|
+
return `${icon} ${styledMessage}${identifierPart}`;
|
|
85
|
+
}
|
|
86
|
+
output(message, options = {}, logFn = console.log) {
|
|
87
|
+
if (silent || !this.shouldLog(options)) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (options.verticalSpace) {
|
|
91
|
+
logFn("");
|
|
92
|
+
}
|
|
93
|
+
logFn(message);
|
|
94
|
+
if (options.verticalSpace) {
|
|
95
|
+
logFn("");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
info(message, options = {}) {
|
|
99
|
+
const formattedMessage = this.formatMessage({
|
|
100
|
+
...options,
|
|
101
|
+
message,
|
|
102
|
+
type: "info"
|
|
103
|
+
});
|
|
104
|
+
this.output(formattedMessage, options);
|
|
105
|
+
}
|
|
106
|
+
warn(message, options = {}) {
|
|
107
|
+
const formattedMessage = this.formatMessage({
|
|
108
|
+
...options,
|
|
109
|
+
message,
|
|
110
|
+
type: "warn"
|
|
111
|
+
});
|
|
112
|
+
this.output(formattedMessage, options);
|
|
113
|
+
}
|
|
114
|
+
error(message, options = {}) {
|
|
115
|
+
const formattedMessage = this.formatMessage({
|
|
116
|
+
...options,
|
|
117
|
+
message,
|
|
118
|
+
type: "error"
|
|
119
|
+
});
|
|
120
|
+
this.output(formattedMessage, options);
|
|
121
|
+
}
|
|
122
|
+
success(message, options = {}) {
|
|
123
|
+
const formattedMessage = this.formatMessage({
|
|
124
|
+
...options,
|
|
125
|
+
message,
|
|
126
|
+
tick: true
|
|
127
|
+
});
|
|
128
|
+
this.output(formattedMessage, options);
|
|
129
|
+
}
|
|
130
|
+
space() {
|
|
131
|
+
if (!silent) {
|
|
132
|
+
console.log("");
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function logTable(columns, data, footer) {
|
|
137
|
+
if (silent)
|
|
138
|
+
return;
|
|
139
|
+
const widths = {};
|
|
140
|
+
for (const col of columns) {
|
|
141
|
+
const headerLength = col.header.length;
|
|
142
|
+
const dataLengths = data.map((row) => row[col.header]?.length || 0);
|
|
143
|
+
const footerLength = footer ? footer[col.header]?.length || 0 : 0;
|
|
144
|
+
widths[col.header] = Math.max(headerLength, ...dataLengths, footerLength);
|
|
145
|
+
}
|
|
146
|
+
const pad = (str, width, align) => {
|
|
147
|
+
return align === "left" ? str.padEnd(width) : str.padStart(width);
|
|
148
|
+
};
|
|
149
|
+
const headerRow = columns.map((col) => pad(col.header, widths[col.header], col.align)).join(pc.gray(" | "));
|
|
150
|
+
console.log(pc.gray(headerRow));
|
|
151
|
+
const separator = columns.map((col) => "-".repeat(widths[col.header])).join(" | ");
|
|
152
|
+
console.log(pc.gray(separator));
|
|
153
|
+
for (const row of data) {
|
|
154
|
+
const rowStr = columns.map((col) => {
|
|
155
|
+
const value = row[col.header] || "";
|
|
156
|
+
const padded = pad(value, widths[col.header], col.align);
|
|
157
|
+
return col.color ? col.color(padded) : padded;
|
|
158
|
+
}).join(pc.gray(" | "));
|
|
159
|
+
console.log(rowStr);
|
|
160
|
+
}
|
|
161
|
+
console.log(pc.gray(separator));
|
|
162
|
+
if (footer) {
|
|
163
|
+
const footerRow = columns.map((col) => {
|
|
164
|
+
const value = footer[col.header] || "";
|
|
165
|
+
const padded = pad(value, widths[col.header], col.align);
|
|
166
|
+
return padded;
|
|
167
|
+
}).join(pc.gray(" | "));
|
|
168
|
+
console.log(footerRow);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
var silent = false, logger;
|
|
172
|
+
var init_logger = __esm(() => {
|
|
173
|
+
logger = Logger.getInstance();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// src/errors.ts
|
|
177
|
+
import pc2 from "picocolors";
|
|
178
|
+
var BunupError, BunupBuildError, BunupDTSBuildError, BunupCLIError, BunupWatchError, BunupPluginError, parseErrorMessage = (error) => {
|
|
179
|
+
if (error instanceof Error) {
|
|
180
|
+
return error.message;
|
|
181
|
+
}
|
|
182
|
+
return String(error);
|
|
183
|
+
}, KNOWN_ERRORS, handleError = (error, context) => {
|
|
184
|
+
const errorMessage = parseErrorMessage(error);
|
|
185
|
+
const contextPrefix = context ? `[${context}] ` : "";
|
|
186
|
+
let errorType = "UNKNOWN ERROR";
|
|
187
|
+
if (error instanceof BunupBuildError) {
|
|
188
|
+
errorType = "BUILD ERROR";
|
|
189
|
+
} else if (error instanceof BunupDTSBuildError) {
|
|
190
|
+
errorType = "DTS ERROR";
|
|
191
|
+
} else if (error instanceof BunupCLIError) {
|
|
192
|
+
errorType = "CLI ERROR";
|
|
193
|
+
} else if (error instanceof BunupWatchError) {
|
|
194
|
+
errorType = "WATCH ERROR";
|
|
195
|
+
} else if (error instanceof BunupPluginError) {
|
|
196
|
+
errorType = "PLUGIN ERROR";
|
|
197
|
+
} else if (error instanceof BunupError) {
|
|
198
|
+
errorType = "BUNUP ERROR";
|
|
199
|
+
}
|
|
200
|
+
const knownError = KNOWN_ERRORS.find((error2) => error2.pattern.test(errorMessage) && (error2.errorType === errorType || !error2.errorType));
|
|
201
|
+
if (!knownError && errorType) {
|
|
202
|
+
console.error(`${pc2.red(errorType)} ${contextPrefix}${errorMessage}`);
|
|
203
|
+
}
|
|
204
|
+
if (knownError) {
|
|
205
|
+
console.log(`
|
|
206
|
+
`);
|
|
207
|
+
knownError.logSolution(errorMessage);
|
|
208
|
+
console.log(`
|
|
209
|
+
`);
|
|
210
|
+
} else {
|
|
211
|
+
console.error(pc2.dim(pc2.white("If you think this is a bug, please open an issue at: ") + pc2.cyan("https://github.com/arshad-yaseen/bunup/issues/new")));
|
|
212
|
+
}
|
|
213
|
+
}, handleErrorAndExit = (error, context) => {
|
|
214
|
+
handleError(error, context);
|
|
215
|
+
process.exit(1);
|
|
216
|
+
};
|
|
217
|
+
var init_errors = __esm(() => {
|
|
218
|
+
init_logger();
|
|
219
|
+
BunupError = class BunupError extends Error {
|
|
220
|
+
constructor(message) {
|
|
221
|
+
super(message);
|
|
222
|
+
this.name = "BunupError";
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
BunupBuildError = class BunupBuildError extends BunupError {
|
|
226
|
+
constructor(message) {
|
|
227
|
+
super(message);
|
|
228
|
+
this.name = "BunupBuildError";
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
BunupDTSBuildError = class BunupDTSBuildError extends BunupError {
|
|
232
|
+
constructor(message) {
|
|
233
|
+
super(message);
|
|
234
|
+
this.name = "BunupDTSBuildError";
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
BunupCLIError = class BunupCLIError extends BunupError {
|
|
238
|
+
constructor(message) {
|
|
239
|
+
super(message);
|
|
240
|
+
this.name = "BunupCLIError";
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
BunupWatchError = class BunupWatchError extends BunupError {
|
|
244
|
+
constructor(message) {
|
|
245
|
+
super(message);
|
|
246
|
+
this.name = "BunupWatchError";
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
BunupPluginError = class BunupPluginError extends BunupError {
|
|
250
|
+
constructor(message) {
|
|
251
|
+
super(message);
|
|
252
|
+
this.name = "BunupPluginError";
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
KNOWN_ERRORS = [
|
|
256
|
+
{
|
|
257
|
+
pattern: /Could not resolve: "bun"/i,
|
|
258
|
+
errorType: "BUILD ERROR",
|
|
259
|
+
logSolution: () => {
|
|
260
|
+
logger.info(pc2.white("You're trying to build a project that uses Bun. ") + pc2.white("Please set the target option to ") + pc2.cyan("`bun`") + pc2.white(`.
|
|
261
|
+
`) + pc2.white("Example: ") + pc2.green("`bunup --target bun`") + pc2.white(" or in config: ") + pc2.green("{ target: 'bun' }"));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
];
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// src/loaders.ts
|
|
268
|
+
import path from "path";
|
|
269
|
+
import { loadConfig } from "coffi";
|
|
270
|
+
async function processLoadedConfigs(config, cwd, filter) {
|
|
271
|
+
return Array.isArray(config) && "root" in config[0] ? config.filter((c) => filter ? filter.includes(c.name) : true).map((c) => ({
|
|
272
|
+
rootDir: path.resolve(cwd, c.root),
|
|
273
|
+
options: addField(c.config, "name", c.name)
|
|
274
|
+
})) : [
|
|
275
|
+
{
|
|
276
|
+
rootDir: cwd,
|
|
277
|
+
options: config
|
|
278
|
+
}
|
|
279
|
+
];
|
|
280
|
+
}
|
|
281
|
+
function addField(objectOrArray, field, value) {
|
|
282
|
+
return Array.isArray(objectOrArray) ? objectOrArray.map((o) => ({ ...o, [field]: value })) : { ...objectOrArray, [field]: value };
|
|
283
|
+
}
|
|
284
|
+
async function loadPackageJson(cwd = process.cwd()) {
|
|
285
|
+
const { config, filepath } = await loadConfig({
|
|
286
|
+
name: "package",
|
|
287
|
+
cwd,
|
|
288
|
+
extensions: [".json"]
|
|
289
|
+
});
|
|
290
|
+
return {
|
|
291
|
+
data: config,
|
|
292
|
+
path: filepath
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
var init_loaders = () => {};
|
|
296
|
+
|
|
297
|
+
// src/utils.ts
|
|
298
|
+
import fsSync from "fs";
|
|
299
|
+
import fs from "fs/promises";
|
|
300
|
+
import path2, { normalize } from "path";
|
|
301
|
+
function ensureArray(value) {
|
|
302
|
+
return Array.isArray(value) ? value : [value];
|
|
303
|
+
}
|
|
304
|
+
function getDefaultOutputExtension(format, packageType) {
|
|
305
|
+
switch (format) {
|
|
306
|
+
case "esm":
|
|
307
|
+
return isModulePackage(packageType) ? ".js" : ".mjs";
|
|
308
|
+
case "cjs":
|
|
309
|
+
return isModulePackage(packageType) ? ".cjs" : ".js";
|
|
310
|
+
case "iife":
|
|
311
|
+
return ".global.js";
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function getDefaultDtsExtention(format, packageType) {
|
|
315
|
+
switch (format) {
|
|
316
|
+
case "esm":
|
|
317
|
+
return isModulePackage(packageType) ? ".d.ts" : ".d.mts";
|
|
318
|
+
case "cjs":
|
|
319
|
+
return isModulePackage(packageType) ? ".d.cts" : ".d.ts";
|
|
320
|
+
case "iife":
|
|
321
|
+
return ".global.d.ts";
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function isModulePackage(packageType) {
|
|
325
|
+
return packageType === "module";
|
|
326
|
+
}
|
|
327
|
+
function formatTime(ms) {
|
|
328
|
+
return ms >= 1000 ? `${(ms / 1000).toFixed(2)}s` : `${Math.round(ms)}ms`;
|
|
329
|
+
}
|
|
330
|
+
function getPackageDeps(packageJson) {
|
|
331
|
+
if (!packageJson)
|
|
332
|
+
return [];
|
|
333
|
+
return Array.from(new Set([
|
|
334
|
+
...Object.keys(packageJson.dependencies || {}),
|
|
335
|
+
...Object.keys(packageJson.peerDependencies || {})
|
|
336
|
+
]));
|
|
337
|
+
}
|
|
338
|
+
function formatFileSize(bytes) {
|
|
339
|
+
if (bytes === 0)
|
|
340
|
+
return "0 B";
|
|
341
|
+
const units = ["B", "KB", "MB", "GB"];
|
|
342
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
343
|
+
if (i === 0)
|
|
344
|
+
return `${bytes} ${units[i]}`;
|
|
345
|
+
return `${(bytes / 1024 ** i).toFixed(2)} ${units[i]}`;
|
|
346
|
+
}
|
|
347
|
+
function getShortFilePath(filePath, maxLength = 3) {
|
|
348
|
+
const fileParts = filePath.split("/");
|
|
349
|
+
const shortPath = fileParts.slice(-maxLength).join("/");
|
|
350
|
+
return shortPath;
|
|
351
|
+
}
|
|
352
|
+
async function cleanOutDir(rootDir, outDir) {
|
|
353
|
+
const outDirPath = path2.join(rootDir, outDir);
|
|
354
|
+
try {
|
|
355
|
+
await fs.rm(outDirPath, { recursive: true, force: true });
|
|
356
|
+
} catch (error) {
|
|
357
|
+
throw new BunupBuildError(`Failed to clean output directory: ${error}`);
|
|
358
|
+
}
|
|
359
|
+
await fs.mkdir(outDirPath, { recursive: true });
|
|
360
|
+
}
|
|
361
|
+
function cleanPath(path3) {
|
|
362
|
+
let cleaned = normalize(path3).replace(/\\/g, "/");
|
|
363
|
+
cleaned = cleaned.replace(/^[a-zA-Z]:\//, "");
|
|
364
|
+
cleaned = cleaned.replace(/^\/+/, "");
|
|
365
|
+
cleaned = cleaned.replace(/\/+/g, "/");
|
|
366
|
+
return cleaned;
|
|
367
|
+
}
|
|
368
|
+
function isDirectoryPath(filePath) {
|
|
369
|
+
return path2.extname(filePath) === "";
|
|
370
|
+
}
|
|
371
|
+
function pathExistsSync(filePath) {
|
|
372
|
+
try {
|
|
373
|
+
fsSync.accessSync(filePath);
|
|
374
|
+
return true;
|
|
375
|
+
} catch (error) {
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
function formatListWithAnd(arr) {
|
|
380
|
+
return new Intl.ListFormat("en", {
|
|
381
|
+
style: "long",
|
|
382
|
+
type: "conjunction"
|
|
383
|
+
}).format(arr);
|
|
384
|
+
}
|
|
385
|
+
async function getFilesFromGlobs(patterns, cwd) {
|
|
386
|
+
const includePatterns = patterns.filter((p) => !p.startsWith("!"));
|
|
387
|
+
const excludePatterns = patterns.filter((p) => p.startsWith("!")).map((p) => p.slice(1));
|
|
388
|
+
const includedFiles = new Set;
|
|
389
|
+
for (const pattern of includePatterns) {
|
|
390
|
+
const glob = new Bun.Glob(pattern);
|
|
391
|
+
for await (const file of glob.scan(cwd)) {
|
|
392
|
+
includedFiles.add(file);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (excludePatterns.length > 0) {
|
|
396
|
+
for (const pattern of excludePatterns) {
|
|
397
|
+
const glob = new Bun.Glob(pattern);
|
|
398
|
+
for await (const file of glob.scan(cwd)) {
|
|
399
|
+
includedFiles.delete(file);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return Array.from(includedFiles);
|
|
404
|
+
}
|
|
405
|
+
var init_utils = __esm(() => {
|
|
406
|
+
init_errors();
|
|
407
|
+
});
|
|
8
408
|
|
|
9
409
|
// src/define.ts
|
|
10
410
|
function defineConfig(options) {
|
|
@@ -13,6 +413,372 @@ function defineConfig(options) {
|
|
|
13
413
|
function defineWorkspace(options) {
|
|
14
414
|
return options;
|
|
15
415
|
}
|
|
416
|
+
// src/build.ts
|
|
417
|
+
init_errors();
|
|
418
|
+
init_loaders();
|
|
419
|
+
init_logger();
|
|
420
|
+
import path3 from "path";
|
|
421
|
+
import { generateDts, logIsolatedDeclarationErrors } from "bun-dts";
|
|
422
|
+
import pc5 from "picocolors";
|
|
423
|
+
|
|
424
|
+
// src/plugins/internal/report.ts
|
|
425
|
+
init_logger();
|
|
426
|
+
init_logger();
|
|
427
|
+
init_utils();
|
|
428
|
+
import pc3 from "picocolors";
|
|
429
|
+
function report() {
|
|
430
|
+
return {
|
|
431
|
+
type: "bunup",
|
|
432
|
+
name: "report",
|
|
433
|
+
hooks: {
|
|
434
|
+
onBuildDone: async ({ options, output }) => {
|
|
435
|
+
if (options.watch)
|
|
436
|
+
return;
|
|
437
|
+
const files = await Promise.all(output.files.map(async (file) => {
|
|
438
|
+
const name = file.relativePathToRootDir;
|
|
439
|
+
const size = Bun.file(file.fullPath).size;
|
|
440
|
+
const gzipSize = Bun.gzipSync(new Uint8Array(await Bun.file(file.fullPath).arrayBuffer())).length;
|
|
441
|
+
const formattedGzipSize = formatFileSize(gzipSize);
|
|
442
|
+
return {
|
|
443
|
+
name,
|
|
444
|
+
size,
|
|
445
|
+
formattedSize: formatFileSize(size),
|
|
446
|
+
gzipSize,
|
|
447
|
+
formattedGzipSize
|
|
448
|
+
};
|
|
449
|
+
}));
|
|
450
|
+
const totalSize = files.reduce((sum, file) => sum + file.size, 0);
|
|
451
|
+
const formattedTotalSize = formatFileSize(totalSize);
|
|
452
|
+
const totalGzipSize = files.reduce((sum, file) => sum + (file.gzipSize || 0), 0);
|
|
453
|
+
const formattedTotalGzipSize = formatFileSize(totalGzipSize);
|
|
454
|
+
const columns = [
|
|
455
|
+
{ header: "File", align: "left", color: pc3.blue },
|
|
456
|
+
{ header: "Size", align: "right", color: pc3.green },
|
|
457
|
+
{
|
|
458
|
+
header: "Gzip",
|
|
459
|
+
align: "right",
|
|
460
|
+
color: pc3.magenta
|
|
461
|
+
}
|
|
462
|
+
];
|
|
463
|
+
const data = files.map((file) => {
|
|
464
|
+
return {
|
|
465
|
+
File: file.name,
|
|
466
|
+
Size: file.formattedSize,
|
|
467
|
+
Gzip: file.formattedGzipSize
|
|
468
|
+
};
|
|
469
|
+
});
|
|
470
|
+
const footer = {
|
|
471
|
+
File: "Total",
|
|
472
|
+
Size: formattedTotalSize,
|
|
473
|
+
Gzip: formattedTotalGzipSize
|
|
474
|
+
};
|
|
475
|
+
logger.space();
|
|
476
|
+
logTable(columns, data, footer);
|
|
477
|
+
logger.space();
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/plugins/internal/use-client.ts
|
|
484
|
+
function useClient() {
|
|
485
|
+
return {
|
|
486
|
+
type: "bunup",
|
|
487
|
+
name: "use-client",
|
|
488
|
+
hooks: {
|
|
489
|
+
onBuildDone: async ({ output }) => {
|
|
490
|
+
for (const file of output.files) {
|
|
491
|
+
let text = await Bun.file(file.fullPath).text();
|
|
492
|
+
const hasUseClient = text.split(`
|
|
493
|
+
`).some((line) => line.trim().startsWith(`"use client";`));
|
|
494
|
+
if (hasUseClient) {
|
|
495
|
+
text = text.replaceAll(`"use client";`, "");
|
|
496
|
+
text = `"use client";
|
|
497
|
+
${text}`;
|
|
498
|
+
}
|
|
499
|
+
await Bun.write(file.fullPath, text);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// src/options.ts
|
|
507
|
+
init_utils();
|
|
508
|
+
var DEFAULT_OPTIONS = {
|
|
509
|
+
entry: [],
|
|
510
|
+
format: ["cjs"],
|
|
511
|
+
outDir: "dist",
|
|
512
|
+
target: "node",
|
|
513
|
+
clean: true
|
|
514
|
+
};
|
|
515
|
+
function createBuildOptions(partialOptions) {
|
|
516
|
+
const options = {
|
|
517
|
+
...DEFAULT_OPTIONS,
|
|
518
|
+
...partialOptions
|
|
519
|
+
};
|
|
520
|
+
return {
|
|
521
|
+
...options,
|
|
522
|
+
plugins: [
|
|
523
|
+
...options.plugins?.filter((p) => p.name !== "report") ?? [],
|
|
524
|
+
useClient(),
|
|
525
|
+
report()
|
|
526
|
+
]
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
function getResolvedMinify(options) {
|
|
530
|
+
const { minify, minifyWhitespace, minifyIdentifiers, minifySyntax } = options;
|
|
531
|
+
const defaultValue = minify === true;
|
|
532
|
+
return {
|
|
533
|
+
whitespace: minifyWhitespace ?? defaultValue,
|
|
534
|
+
identifiers: minifyIdentifiers ?? defaultValue,
|
|
535
|
+
syntax: minifySyntax ?? defaultValue
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
function getResolvedBytecode(bytecode, format) {
|
|
539
|
+
return format === "cjs" ? bytecode : undefined;
|
|
540
|
+
}
|
|
541
|
+
function getResolvedSourcemap(sourcemap) {
|
|
542
|
+
if (sourcemap === true) {
|
|
543
|
+
return "inline";
|
|
544
|
+
}
|
|
545
|
+
return typeof sourcemap === "string" ? sourcemap : undefined;
|
|
546
|
+
}
|
|
547
|
+
function getResolvedDefine(define, env) {
|
|
548
|
+
return {
|
|
549
|
+
...typeof env === "object" && Object.keys(env).reduce((acc, key) => {
|
|
550
|
+
const value = JSON.stringify(env[key]);
|
|
551
|
+
acc[`process.env.${key}`] = value;
|
|
552
|
+
acc[`import.meta.env.${key}`] = value;
|
|
553
|
+
return acc;
|
|
554
|
+
}, {}),
|
|
555
|
+
...define
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
function getResolvedSplitting(splitting, format) {
|
|
559
|
+
return splitting === undefined ? format === "esm" : splitting;
|
|
560
|
+
}
|
|
561
|
+
function getResolvedDtsSplitting(splitting) {
|
|
562
|
+
return splitting ?? true;
|
|
563
|
+
}
|
|
564
|
+
var DEFAULT_ENTRY_NAMING = "[dir]/[name].[ext]";
|
|
565
|
+
function getResolvedNaming(fmt, packageType) {
|
|
566
|
+
const replaceExt = (pattern) => pattern.replace(".[ext]", getDefaultOutputExtension(fmt, packageType));
|
|
567
|
+
return replaceExt(DEFAULT_ENTRY_NAMING);
|
|
568
|
+
}
|
|
569
|
+
function getResolvedEnv(env) {
|
|
570
|
+
return typeof env === "string" ? env : undefined;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// src/helpers/external.ts
|
|
574
|
+
init_utils();
|
|
575
|
+
function getPackageDepsPatterns(packageJson) {
|
|
576
|
+
return getPackageDeps(packageJson).map((dep) => new RegExp(`^${dep}($|\\/|\\\\)`));
|
|
577
|
+
}
|
|
578
|
+
function matchesPattern(path3, pattern) {
|
|
579
|
+
return typeof pattern === "string" ? pattern === path3 : pattern.test(path3);
|
|
580
|
+
}
|
|
581
|
+
function isExternal(path3, options, packageJson) {
|
|
582
|
+
const packageDepsPatterns = getPackageDepsPatterns(packageJson);
|
|
583
|
+
const matchesExternalPattern = packageDepsPatterns.some((pattern) => pattern.test(path3)) || options.external?.some((pattern) => matchesPattern(path3, pattern));
|
|
584
|
+
const isExcludedFromExternal = options.noExternal?.some((pattern) => matchesPattern(path3, pattern));
|
|
585
|
+
return matchesExternalPattern && !isExcludedFromExternal;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// src/plugins/internal/external-option.ts
|
|
589
|
+
function externalOptionPlugin(options, packageJson) {
|
|
590
|
+
return {
|
|
591
|
+
name: "bunup:external-option-plugin",
|
|
592
|
+
setup(build) {
|
|
593
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
594
|
+
const importPath = args.path;
|
|
595
|
+
if (isExternal(importPath, options, packageJson)) {
|
|
596
|
+
return {
|
|
597
|
+
path: importPath,
|
|
598
|
+
external: true
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
return null;
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// src/plugins/utils.ts
|
|
608
|
+
init_errors();
|
|
609
|
+
import pc4 from "picocolors";
|
|
610
|
+
function filterBunupBunPlugins(plugins) {
|
|
611
|
+
if (!plugins)
|
|
612
|
+
return [];
|
|
613
|
+
return plugins.filter((p) => p.type === "bun");
|
|
614
|
+
}
|
|
615
|
+
function filterBunupPlugins(plugins) {
|
|
616
|
+
if (!plugins)
|
|
617
|
+
return [];
|
|
618
|
+
return plugins.filter((p) => p.type === "bunup");
|
|
619
|
+
}
|
|
620
|
+
async function runPluginBuildStartHooks(bunupPlugins, options) {
|
|
621
|
+
if (!bunupPlugins)
|
|
622
|
+
return;
|
|
623
|
+
for (const plugin of bunupPlugins) {
|
|
624
|
+
if (plugin.hooks.onBuildStart) {
|
|
625
|
+
await plugin.hooks.onBuildStart(options);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
async function runPluginBuildDoneHooks(bunupPlugins, options, output, meta) {
|
|
630
|
+
if (!bunupPlugins)
|
|
631
|
+
return;
|
|
632
|
+
for (const plugin of bunupPlugins) {
|
|
633
|
+
if (plugin.hooks.onBuildDone) {
|
|
634
|
+
await plugin.hooks.onBuildDone({ options, output, meta });
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
async function getPackageForPlugin(name, pluginName) {
|
|
639
|
+
let pkg;
|
|
640
|
+
try {
|
|
641
|
+
pkg = await import(name);
|
|
642
|
+
} catch {
|
|
643
|
+
throw new BunupPluginError(`[${pc4.cyan(name)}] is required for the ${pluginName} plugin. Please install it with: ${pc4.blue(`bun add ${name} --dev`)}`);
|
|
644
|
+
}
|
|
645
|
+
return pkg;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// src/build.ts
|
|
649
|
+
init_utils();
|
|
650
|
+
async function build(partialOptions, rootDir = process.cwd()) {
|
|
651
|
+
const buildOutput = {
|
|
652
|
+
files: []
|
|
653
|
+
};
|
|
654
|
+
const options = createBuildOptions(partialOptions);
|
|
655
|
+
if (!options.entry || options.entry.length === 0 || !options.outDir) {
|
|
656
|
+
throw new BunupBuildError("Nothing to build. Please make sure you have provided a proper bunup configuration or cli arguments.");
|
|
657
|
+
}
|
|
658
|
+
if (options.clean) {
|
|
659
|
+
cleanOutDir(rootDir, options.outDir);
|
|
660
|
+
}
|
|
661
|
+
setSilent(options.silent);
|
|
662
|
+
const packageJson = await loadPackageJson(rootDir);
|
|
663
|
+
if (packageJson.data && packageJson.path) {
|
|
664
|
+
logger.info(`Using ${getShortFilePath(packageJson.path, 2)}`, {
|
|
665
|
+
muted: true,
|
|
666
|
+
identifier: options.name,
|
|
667
|
+
once: `${packageJson.path}:${options.name}`
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
const bunupPlugins = filterBunupPlugins(options.plugins);
|
|
671
|
+
await runPluginBuildStartHooks(bunupPlugins, options);
|
|
672
|
+
const packageType = packageJson.data?.type;
|
|
673
|
+
const plugins = [
|
|
674
|
+
externalOptionPlugin(options, packageJson.data),
|
|
675
|
+
...filterBunupBunPlugins(options.plugins).map((p) => p.plugin)
|
|
676
|
+
];
|
|
677
|
+
const entrypoints = await getFilesFromGlobs(ensureArray(options.entry), rootDir);
|
|
678
|
+
if (!entrypoints.length) {
|
|
679
|
+
throw new BunupBuildError("The entrypoints you provided do not exist. Please make sure the entrypoints point to valid files.");
|
|
680
|
+
}
|
|
681
|
+
const buildPromises = options.format.flatMap(async (fmt) => {
|
|
682
|
+
const result = await Bun.build({
|
|
683
|
+
entrypoints: entrypoints.map((file) => `${rootDir}/${file}`),
|
|
684
|
+
format: fmt,
|
|
685
|
+
naming: getResolvedNaming(fmt, packageType),
|
|
686
|
+
splitting: getResolvedSplitting(options.splitting, fmt),
|
|
687
|
+
bytecode: getResolvedBytecode(options.bytecode, fmt),
|
|
688
|
+
define: getResolvedDefine(options.define, options.env),
|
|
689
|
+
minify: getResolvedMinify(options),
|
|
690
|
+
outdir: `${rootDir}/${options.outDir}`,
|
|
691
|
+
target: options.target,
|
|
692
|
+
sourcemap: getResolvedSourcemap(options.sourcemap),
|
|
693
|
+
loader: options.loader,
|
|
694
|
+
drop: options.drop,
|
|
695
|
+
banner: options.banner,
|
|
696
|
+
footer: options.footer,
|
|
697
|
+
publicPath: options.publicPath,
|
|
698
|
+
env: getResolvedEnv(options.env),
|
|
699
|
+
ignoreDCEAnnotations: options.ignoreDCEAnnotations,
|
|
700
|
+
emitDCEAnnotations: options.emitDCEAnnotations,
|
|
701
|
+
throw: false,
|
|
702
|
+
plugins
|
|
703
|
+
});
|
|
704
|
+
for (const log of result.logs) {
|
|
705
|
+
if (log.level === "error") {
|
|
706
|
+
throw new BunupBuildError(log.message);
|
|
707
|
+
}
|
|
708
|
+
if (log.level === "warning")
|
|
709
|
+
logger.warn(log.message);
|
|
710
|
+
else if (log.level === "info")
|
|
711
|
+
logger.info(log.message);
|
|
712
|
+
}
|
|
713
|
+
for (const file of result.outputs) {
|
|
714
|
+
const relativePathToRootDir = getRelativePathToRootDir(file.path, rootDir);
|
|
715
|
+
const relativePathToOutputDir = getRelativePathToOutputDir(relativePathToRootDir, options.outDir);
|
|
716
|
+
if (file.kind === "entry-point") {
|
|
717
|
+
logger.success(`${pc5.dim(options.outDir)}/${relativePathToOutputDir}`, {
|
|
718
|
+
identifier: options.name
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
buildOutput.files.push({
|
|
722
|
+
fullPath: file.path,
|
|
723
|
+
relativePathToRootDir,
|
|
724
|
+
relativePathToOutputDir,
|
|
725
|
+
dts: false,
|
|
726
|
+
format: fmt,
|
|
727
|
+
kind: file.kind
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
await Promise.all(buildPromises);
|
|
732
|
+
if (options.dts) {
|
|
733
|
+
const { entry, splitting, ...dtsOptions } = typeof options.dts === "object" ? options.dts : {};
|
|
734
|
+
const dtsResult = await generateDts(ensureArray(entry ?? entrypoints), {
|
|
735
|
+
cwd: rootDir,
|
|
736
|
+
preferredTsConfigPath: options.preferredTsconfigPath,
|
|
737
|
+
splitting: getResolvedDtsSplitting(splitting),
|
|
738
|
+
...dtsOptions
|
|
739
|
+
});
|
|
740
|
+
if (dtsResult.errors.length) {
|
|
741
|
+
logIsolatedDeclarationErrors(dtsResult.errors, {
|
|
742
|
+
shouldExit: true
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
for (const fmt of options.format) {
|
|
746
|
+
for (const file of dtsResult.files) {
|
|
747
|
+
const dtsExtension = getDefaultDtsExtention(fmt, packageType);
|
|
748
|
+
const relativePathToOutputDir = cleanPath(`${file.pathInfo.outputPathWithoutExtension}${dtsExtension}`);
|
|
749
|
+
const relativePathToRootDir = cleanPath(`${options.outDir}/${relativePathToOutputDir}`);
|
|
750
|
+
if (file.kind === "entry-point") {
|
|
751
|
+
logger.success(`${pc5.dim(options.outDir)}/${relativePathToOutputDir}`, {
|
|
752
|
+
identifier: options.name
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
const fullPath = path3.join(rootDir, relativePathToRootDir);
|
|
756
|
+
await Bun.write(fullPath, file.dts);
|
|
757
|
+
buildOutput.files.push({
|
|
758
|
+
fullPath,
|
|
759
|
+
relativePathToRootDir,
|
|
760
|
+
relativePathToOutputDir,
|
|
761
|
+
dts: true,
|
|
762
|
+
format: fmt,
|
|
763
|
+
kind: file.kind
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
await runPluginBuildDoneHooks(bunupPlugins, options, buildOutput, {
|
|
769
|
+
packageJson,
|
|
770
|
+
rootDir
|
|
771
|
+
});
|
|
772
|
+
if (options.onSuccess) {
|
|
773
|
+
await options.onSuccess(options);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
function getRelativePathToRootDir(filePath, rootDir) {
|
|
777
|
+
return cleanPath(filePath).replace(`${cleanPath(rootDir)}/`, "");
|
|
778
|
+
}
|
|
779
|
+
function getRelativePathToOutputDir(relativePathToRootDir, outDir) {
|
|
780
|
+
return cleanPath(relativePathToRootDir).replace(`${cleanPath(outDir)}/`, "");
|
|
781
|
+
}
|
|
16
782
|
export {
|
|
17
783
|
defineWorkspace,
|
|
18
784
|
defineConfig,
|