@b9g/libuild 0.1.0
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/README.md +266 -0
- package/package.json +78 -0
- package/src/chunk-EVLPGECD.js +71 -0
- package/src/chunk-EVLPGECD.js.map +7 -0
- package/src/chunk-V5JOSLNS.js +699 -0
- package/src/chunk-V5JOSLNS.js.map +7 -0
- package/src/cli.cjs +948 -0
- package/src/cli.cjs.map +7 -0
- package/src/cli.d.ts +2 -0
- package/src/cli.js +73 -0
- package/src/cli.js.map +7 -0
- package/src/libuild.cjs +800 -0
- package/src/libuild.cjs.map +7 -0
- package/src/libuild.d.ts +22 -0
- package/src/libuild.js +13 -0
- package/src/libuild.js.map +7 -0
- package/src/package-TBRVMOT7.js +132 -0
- package/src/package-TBRVMOT7.js.map +7 -0
- package/src/umd-plugin.cjs +105 -0
- package/src/umd-plugin.cjs.map +7 -0
- package/src/umd-plugin.d.ts +8 -0
- package/src/umd-plugin.js +8 -0
- package/src/umd-plugin.js.map +7 -0
package/src/cli.cjs
ADDED
|
@@ -0,0 +1,948 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
|
|
29
|
+
// package.json
|
|
30
|
+
var require_package = __commonJS({
|
|
31
|
+
"package.json"(exports2, module2) {
|
|
32
|
+
module2.exports = {
|
|
33
|
+
name: "@b9g/libuild",
|
|
34
|
+
version: "0.1.0",
|
|
35
|
+
description: "Zero-config library builds",
|
|
36
|
+
main: "./dist/src/libuild.cjs",
|
|
37
|
+
private: true,
|
|
38
|
+
bin: {
|
|
39
|
+
libuild: "./dist/src/cli.js"
|
|
40
|
+
},
|
|
41
|
+
type: "module",
|
|
42
|
+
scripts: {
|
|
43
|
+
build: "bun run src/cli.ts",
|
|
44
|
+
dev: "bun run src/cli.ts",
|
|
45
|
+
publish: "bun run src/cli.ts publish",
|
|
46
|
+
test: "bun test",
|
|
47
|
+
"test-src": "node test.js"
|
|
48
|
+
},
|
|
49
|
+
dependencies: {
|
|
50
|
+
esbuild: "^0.19.0",
|
|
51
|
+
"magic-string": "^0.30.0",
|
|
52
|
+
typescript: "^5.0.0"
|
|
53
|
+
},
|
|
54
|
+
devDependencies: {
|
|
55
|
+
"@types/bun": "latest"
|
|
56
|
+
},
|
|
57
|
+
keywords: [
|
|
58
|
+
"build",
|
|
59
|
+
"library",
|
|
60
|
+
"bundler",
|
|
61
|
+
"bun",
|
|
62
|
+
"zero-config",
|
|
63
|
+
"typescript"
|
|
64
|
+
],
|
|
65
|
+
engines: {
|
|
66
|
+
bun: ">=1.0.0"
|
|
67
|
+
},
|
|
68
|
+
repository: {
|
|
69
|
+
type: "git",
|
|
70
|
+
url: "https://github.com/bikeshaving/libuild.git"
|
|
71
|
+
},
|
|
72
|
+
bugs: {
|
|
73
|
+
url: "https://github.com/bikeshaving/libuild/issues"
|
|
74
|
+
},
|
|
75
|
+
license: "MIT",
|
|
76
|
+
module: "./dist/src/libuild.js",
|
|
77
|
+
types: "./dist/src/libuild.d.ts",
|
|
78
|
+
exports: {
|
|
79
|
+
".": {
|
|
80
|
+
types: "./dist/src/libuild.d.ts",
|
|
81
|
+
import: "./dist/src/libuild.js",
|
|
82
|
+
require: "./dist/src/libuild.cjs"
|
|
83
|
+
},
|
|
84
|
+
"./cli": {
|
|
85
|
+
types: "./dist/src/cli.d.ts",
|
|
86
|
+
import: "./dist/src/cli.js",
|
|
87
|
+
require: "./dist/src/cli.cjs"
|
|
88
|
+
},
|
|
89
|
+
"./cli.js": {
|
|
90
|
+
types: "./dist/src/cli.d.ts",
|
|
91
|
+
import: "./dist/src/cli.js",
|
|
92
|
+
require: "./dist/src/cli.cjs"
|
|
93
|
+
},
|
|
94
|
+
"./libuild": {
|
|
95
|
+
types: "./dist/src/libuild.d.ts",
|
|
96
|
+
import: "./dist/src/libuild.js",
|
|
97
|
+
require: "./dist/src/libuild.cjs"
|
|
98
|
+
},
|
|
99
|
+
"./libuild.js": {
|
|
100
|
+
types: "./dist/src/libuild.d.ts",
|
|
101
|
+
import: "./dist/src/libuild.js",
|
|
102
|
+
require: "./dist/src/libuild.cjs"
|
|
103
|
+
},
|
|
104
|
+
"./umd-plugin": {
|
|
105
|
+
types: "./dist/src/umd-plugin.d.ts",
|
|
106
|
+
import: "./dist/src/umd-plugin.js",
|
|
107
|
+
require: "./dist/src/umd-plugin.cjs"
|
|
108
|
+
},
|
|
109
|
+
"./umd-plugin.js": {
|
|
110
|
+
types: "./dist/src/umd-plugin.d.ts",
|
|
111
|
+
import: "./dist/src/umd-plugin.js",
|
|
112
|
+
require: "./dist/src/umd-plugin.cjs"
|
|
113
|
+
},
|
|
114
|
+
"./package.json": "./dist/package.json"
|
|
115
|
+
},
|
|
116
|
+
files: [
|
|
117
|
+
"dist/",
|
|
118
|
+
"README.md"
|
|
119
|
+
]
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// src/cli.ts
|
|
125
|
+
var import_util = require("util");
|
|
126
|
+
|
|
127
|
+
// src/libuild.ts
|
|
128
|
+
var FS2 = __toESM(require("fs/promises"), 1);
|
|
129
|
+
var Path2 = __toESM(require("path"), 1);
|
|
130
|
+
var import_child_process = require("child_process");
|
|
131
|
+
var ESBuild = __toESM(require("esbuild"), 1);
|
|
132
|
+
var TS = __toESM(require("typescript"), 1);
|
|
133
|
+
|
|
134
|
+
// src/umd-plugin.ts
|
|
135
|
+
var FS = __toESM(require("fs/promises"), 1);
|
|
136
|
+
var Path = __toESM(require("path"), 1);
|
|
137
|
+
var import_magic_string = __toESM(require("magic-string"), 1);
|
|
138
|
+
function umdPlugin(options) {
|
|
139
|
+
return {
|
|
140
|
+
name: "umd-wrapper",
|
|
141
|
+
setup(build3) {
|
|
142
|
+
build3.onEnd(async (result) => {
|
|
143
|
+
if (result.errors.length > 0)
|
|
144
|
+
return;
|
|
145
|
+
const outputDir = build3.initialOptions.outdir;
|
|
146
|
+
if (outputDir) {
|
|
147
|
+
const files = await FS.readdir(outputDir);
|
|
148
|
+
for (const file of files) {
|
|
149
|
+
if (file.endsWith(".js") && !file.endsWith(".js.map")) {
|
|
150
|
+
await wrapWithUmd(Path.join(outputDir, file), options.globalName);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
async function wrapWithUmd(filePath, globalName) {
|
|
159
|
+
const code = await FS.readFile(filePath, "utf-8");
|
|
160
|
+
const ms = new import_magic_string.default(code);
|
|
161
|
+
const sourcemapComment = code.match(/\/\/# sourceMappingURL=.+$/m);
|
|
162
|
+
if (sourcemapComment) {
|
|
163
|
+
const index = code.lastIndexOf(sourcemapComment[0]);
|
|
164
|
+
ms.remove(index, index + sourcemapComment[0].length);
|
|
165
|
+
}
|
|
166
|
+
const exportsMatch = code.match(/\nmodule\.exports\s*=\s*([^;]+);?\s*$/);
|
|
167
|
+
if (exportsMatch) {
|
|
168
|
+
const index = code.lastIndexOf(exportsMatch[0]);
|
|
169
|
+
ms.overwrite(index, index + exportsMatch[0].length, `
|
|
170
|
+
return ${exportsMatch[1]};`);
|
|
171
|
+
}
|
|
172
|
+
const umdHeader = `(function (root, factory) {
|
|
173
|
+
if (typeof define === 'function' && define.amd) {
|
|
174
|
+
// AMD
|
|
175
|
+
define([], factory);
|
|
176
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
177
|
+
// CommonJS
|
|
178
|
+
module.exports = factory();
|
|
179
|
+
} else {
|
|
180
|
+
// Browser globals
|
|
181
|
+
root.${globalName} = factory();
|
|
182
|
+
}
|
|
183
|
+
}(typeof self !== 'undefined' ? self : this, function () {
|
|
184
|
+
`;
|
|
185
|
+
const umdFooter = `
|
|
186
|
+
}));`;
|
|
187
|
+
ms.prepend(umdHeader);
|
|
188
|
+
ms.append(umdFooter);
|
|
189
|
+
const result = ms.toString();
|
|
190
|
+
const map = ms.generateMap({
|
|
191
|
+
file: Path.basename(filePath),
|
|
192
|
+
source: Path.basename(filePath).replace(".js", ".ts"),
|
|
193
|
+
includeContent: true
|
|
194
|
+
});
|
|
195
|
+
await FS.writeFile(filePath, result);
|
|
196
|
+
await FS.writeFile(filePath + ".map", map.toString());
|
|
197
|
+
await FS.writeFile(filePath, result + `
|
|
198
|
+
//# sourceMappingURL=${Path.basename(filePath)}.map`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/libuild.ts
|
|
202
|
+
function isValidEntrypoint(filename) {
|
|
203
|
+
if (!filename.endsWith(".ts") && !filename.endsWith(".js"))
|
|
204
|
+
return false;
|
|
205
|
+
if (filename.endsWith(".d.ts"))
|
|
206
|
+
return false;
|
|
207
|
+
if (filename.startsWith("_"))
|
|
208
|
+
return false;
|
|
209
|
+
if (filename.startsWith("."))
|
|
210
|
+
return false;
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
async function findEntryPoints(srcDir) {
|
|
214
|
+
const files = await FS2.readdir(srcDir);
|
|
215
|
+
return files.filter(isValidEntrypoint).map((file) => Path2.basename(file, Path2.extname(file))).sort();
|
|
216
|
+
}
|
|
217
|
+
async function detectMainEntry(pkg, entries) {
|
|
218
|
+
if (pkg.exports && pkg.exports["."]) {
|
|
219
|
+
const dotExport = pkg.exports["."];
|
|
220
|
+
let importPath;
|
|
221
|
+
if (typeof dotExport === "string") {
|
|
222
|
+
importPath = dotExport;
|
|
223
|
+
} else if (typeof dotExport === "object" && dotExport.import) {
|
|
224
|
+
importPath = dotExport.import;
|
|
225
|
+
}
|
|
226
|
+
if (importPath) {
|
|
227
|
+
const match = importPath.match(/\.\/src\/([^.]+)/);
|
|
228
|
+
if (match && entries.includes(match[1])) {
|
|
229
|
+
return match[1];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (pkg.main && typeof pkg.main === "string") {
|
|
234
|
+
const mainBase = Path2.basename(pkg.main, Path2.extname(pkg.main));
|
|
235
|
+
if (entries.includes(mainBase)) {
|
|
236
|
+
return mainBase;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (entries.includes("index")) {
|
|
240
|
+
return "index";
|
|
241
|
+
}
|
|
242
|
+
if (entries.length === 1) {
|
|
243
|
+
return entries[0];
|
|
244
|
+
}
|
|
245
|
+
const pkgNameParts = pkg.name.split("/");
|
|
246
|
+
const pkgName = pkgNameParts[pkgNameParts.length - 1];
|
|
247
|
+
if (!pkgName) {
|
|
248
|
+
throw new Error(`Invalid package name: ${pkg.name}`);
|
|
249
|
+
}
|
|
250
|
+
if (entries.includes(pkgName)) {
|
|
251
|
+
return pkgName;
|
|
252
|
+
}
|
|
253
|
+
return entries[0];
|
|
254
|
+
}
|
|
255
|
+
function checkIfExportIsStale(exportKey, exportValue, entries) {
|
|
256
|
+
if (exportKey === "./package.json" || typeof exportValue === "string" && (exportValue === "./package.json" || exportValue.endsWith("/package.json"))) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
let entryName;
|
|
260
|
+
let hasInvalidPath = false;
|
|
261
|
+
if (typeof exportValue === "string") {
|
|
262
|
+
if (exportValue.match(/\.\/src\/.*\/.*\.(ts|js)$/)) {
|
|
263
|
+
hasInvalidPath = true;
|
|
264
|
+
} else {
|
|
265
|
+
const match = exportValue.match(/\.\/src\/([^/]+)\.(ts|js)$/);
|
|
266
|
+
if (match) {
|
|
267
|
+
const filename = match[1] + "." + match[2];
|
|
268
|
+
if (!isValidEntrypoint(filename)) {
|
|
269
|
+
hasInvalidPath = true;
|
|
270
|
+
} else {
|
|
271
|
+
entryName = match[1];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
} else if (typeof exportValue === "object" && exportValue !== null) {
|
|
276
|
+
const importPath = exportValue.import || exportValue.require;
|
|
277
|
+
if (typeof importPath === "string") {
|
|
278
|
+
if (importPath.match(/\.\/src\/.*\/.*\.(ts|js|cjs)$/)) {
|
|
279
|
+
hasInvalidPath = true;
|
|
280
|
+
} else {
|
|
281
|
+
const match = importPath.match(/\.\/src\/([^/]+)\.(ts|js|cjs)$/);
|
|
282
|
+
if (match) {
|
|
283
|
+
const filename = match[1] + "." + match[2].replace("cjs", "js");
|
|
284
|
+
if (!isValidEntrypoint(filename)) {
|
|
285
|
+
hasInvalidPath = true;
|
|
286
|
+
} else {
|
|
287
|
+
entryName = match[1];
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (!entryName && !hasInvalidPath && exportKey.startsWith("./") && exportKey !== ".") {
|
|
294
|
+
const keyName = exportKey.slice(2).replace(/\.js$/, "");
|
|
295
|
+
if (keyName && !keyName.includes("/")) {
|
|
296
|
+
entryName = keyName;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (hasInvalidPath) {
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
return entryName ? !entries.includes(entryName) : false;
|
|
303
|
+
}
|
|
304
|
+
function generateExports(entries, mainEntry, options, existingExports = {}) {
|
|
305
|
+
const exports2 = {};
|
|
306
|
+
const staleExports = [];
|
|
307
|
+
function createExportEntry(entry) {
|
|
308
|
+
const exportEntry = {
|
|
309
|
+
types: `./src/${entry}.d.ts`,
|
|
310
|
+
import: `./src/${entry}.js`
|
|
311
|
+
};
|
|
312
|
+
if (options.formats.cjs) {
|
|
313
|
+
exportEntry.require = `./src/${entry}.cjs`;
|
|
314
|
+
}
|
|
315
|
+
return exportEntry;
|
|
316
|
+
}
|
|
317
|
+
function expandExistingExport(existing, entryFromPath) {
|
|
318
|
+
var _a;
|
|
319
|
+
if (typeof existing === "string") {
|
|
320
|
+
if (existing === "./package.json" || existing.endsWith("/package.json")) {
|
|
321
|
+
return existing;
|
|
322
|
+
}
|
|
323
|
+
const match = existing.match(/\.\/src\/([^/]+\.(?:ts|js))$/);
|
|
324
|
+
if (match) {
|
|
325
|
+
const filename = match[1];
|
|
326
|
+
if (!isValidEntrypoint(filename)) {
|
|
327
|
+
throw new Error(`Export path '${existing}' references '${filename}' which is not a valid entrypoint. Valid entrypoints cannot start with '_' or '.' and must be .ts/.js files in src/.`);
|
|
328
|
+
}
|
|
329
|
+
const entry = Path2.basename(filename, Path2.extname(filename));
|
|
330
|
+
return options.formats.cjs ? {
|
|
331
|
+
types: `./src/${entry}.d.ts`,
|
|
332
|
+
import: existing,
|
|
333
|
+
require: `./src/${entry}.cjs`
|
|
334
|
+
} : {
|
|
335
|
+
types: `./src/${entry}.d.ts`,
|
|
336
|
+
import: existing
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
if (entryFromPath) {
|
|
340
|
+
return options.formats.cjs ? {
|
|
341
|
+
types: `./src/${entryFromPath}.d.ts`,
|
|
342
|
+
import: existing,
|
|
343
|
+
require: `./src/${entryFromPath}.cjs`
|
|
344
|
+
} : {
|
|
345
|
+
types: `./src/${entryFromPath}.d.ts`,
|
|
346
|
+
import: existing
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
throw new Error(`Export path '${existing}' must point to a valid entrypoint in src/ (e.g., './src/utils.js'). Nested directories and internal files (starting with '_' or '.') are not allowed.`);
|
|
350
|
+
} else if (typeof existing === "object" && existing !== null) {
|
|
351
|
+
if (options.formats.cjs && !existing.require && existing.import) {
|
|
352
|
+
const match = (_a = existing.import) == null ? void 0 : _a.match(/\.\/src\/([^/]+\.(?:ts|js))$/);
|
|
353
|
+
if (match) {
|
|
354
|
+
const filename = match[1];
|
|
355
|
+
if (!isValidEntrypoint(filename)) {
|
|
356
|
+
throw new Error(`Export import path '${existing.import}' references '${filename}' which is not a valid entrypoint. Valid entrypoints cannot start with '_' or '.' and must be .ts/.js files in src/.`);
|
|
357
|
+
}
|
|
358
|
+
const entry = Path2.basename(filename, Path2.extname(filename));
|
|
359
|
+
return {
|
|
360
|
+
...existing,
|
|
361
|
+
types: existing.types || `./src/${entry}.d.ts`,
|
|
362
|
+
require: `./src/${entry}.cjs`
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
if (entryFromPath) {
|
|
366
|
+
return {
|
|
367
|
+
...existing,
|
|
368
|
+
types: existing.types || `./src/${entryFromPath}.d.ts`,
|
|
369
|
+
require: `./src/${entryFromPath}.cjs`
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
throw new Error(`Export import path '${existing.import}' must point to a valid entrypoint in src/ (e.g., './src/utils.js'). Nested directories and internal files are not allowed.`);
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
types: existing.types || `./src/${entryFromPath}.d.ts`,
|
|
376
|
+
...existing
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
return existing;
|
|
380
|
+
}
|
|
381
|
+
for (const [key, value] of Object.entries(existingExports)) {
|
|
382
|
+
const isStale = checkIfExportIsStale(key, value, entries);
|
|
383
|
+
if (isStale) {
|
|
384
|
+
staleExports.push(key);
|
|
385
|
+
} else {
|
|
386
|
+
exports2[key] = expandExistingExport(value);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (!exports2["."]) {
|
|
390
|
+
exports2["."] = createExportEntry(mainEntry);
|
|
391
|
+
} else {
|
|
392
|
+
exports2["."] = expandExistingExport(exports2["."], mainEntry);
|
|
393
|
+
}
|
|
394
|
+
for (const entry of entries) {
|
|
395
|
+
if (entry === "umd")
|
|
396
|
+
continue;
|
|
397
|
+
const key = `./${entry}`;
|
|
398
|
+
if (!exports2[key]) {
|
|
399
|
+
exports2[key] = createExportEntry(entry);
|
|
400
|
+
} else {
|
|
401
|
+
exports2[key] = expandExistingExport(exports2[key], entry);
|
|
402
|
+
}
|
|
403
|
+
if (!exports2[`${key}.js`]) {
|
|
404
|
+
exports2[`${key}.js`] = exports2[key];
|
|
405
|
+
}
|
|
406
|
+
if (entry === "jsx-runtime") {
|
|
407
|
+
if (!exports2["./jsx-dev-runtime"]) {
|
|
408
|
+
exports2["./jsx-dev-runtime"] = exports2[key];
|
|
409
|
+
}
|
|
410
|
+
if (!exports2["./jsx-dev-runtime.js"]) {
|
|
411
|
+
exports2["./jsx-dev-runtime.js"] = exports2[key];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (options.formats.umd && !exports2["./umd"]) {
|
|
416
|
+
exports2["./umd"] = {
|
|
417
|
+
require: "./src/umd.js"
|
|
418
|
+
};
|
|
419
|
+
if (!exports2["./umd.js"]) {
|
|
420
|
+
exports2["./umd.js"] = exports2["./umd"];
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (!exports2["./package.json"]) {
|
|
424
|
+
exports2["./package.json"] = "./package.json";
|
|
425
|
+
}
|
|
426
|
+
return { exports: exports2, staleExports };
|
|
427
|
+
}
|
|
428
|
+
function transformSrcToDist(value) {
|
|
429
|
+
if (typeof value === "string") {
|
|
430
|
+
if (value.startsWith("src/") || value === "src") {
|
|
431
|
+
return "./" + value;
|
|
432
|
+
}
|
|
433
|
+
return value;
|
|
434
|
+
} else if (Array.isArray(value)) {
|
|
435
|
+
return value.map(transformSrcToDist);
|
|
436
|
+
} else if (typeof value === "object" && value !== null) {
|
|
437
|
+
const transformed = {};
|
|
438
|
+
for (const [key, val] of Object.entries(value)) {
|
|
439
|
+
transformed[key] = transformSrcToDist(val);
|
|
440
|
+
}
|
|
441
|
+
return transformed;
|
|
442
|
+
}
|
|
443
|
+
return value;
|
|
444
|
+
}
|
|
445
|
+
function fixExportsForDist(obj) {
|
|
446
|
+
if (typeof obj === "string") {
|
|
447
|
+
if (obj.includes("/dist/") && obj.includes("/src/")) {
|
|
448
|
+
const match = obj.match(/.*\/src\/(.*)$/);
|
|
449
|
+
if (match) {
|
|
450
|
+
return `./src/${match[1]}`;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (obj.startsWith("./dist/src/")) {
|
|
454
|
+
return obj.replace("./dist/src/", "./src/");
|
|
455
|
+
}
|
|
456
|
+
if (obj.includes("/dist/") && obj.endsWith("/package.json")) {
|
|
457
|
+
return "./package.json";
|
|
458
|
+
}
|
|
459
|
+
return obj;
|
|
460
|
+
} else if (Array.isArray(obj)) {
|
|
461
|
+
return obj.map(fixExportsForDist);
|
|
462
|
+
} else if (typeof obj === "object" && obj !== null) {
|
|
463
|
+
const fixed = {};
|
|
464
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
465
|
+
if (key === "files" && Array.isArray(val)) {
|
|
466
|
+
fixed[key] = val.filter((file) => file !== "dist/" && file !== "dist").concat(val.includes("dist/") || val.includes("dist") ? ["src/"] : []);
|
|
467
|
+
} else {
|
|
468
|
+
fixed[key] = fixExportsForDist(val);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return fixed;
|
|
472
|
+
}
|
|
473
|
+
return obj;
|
|
474
|
+
}
|
|
475
|
+
function cleanPackageJSON(pkg, mainEntry, options) {
|
|
476
|
+
const cleaned = {
|
|
477
|
+
name: pkg.name,
|
|
478
|
+
version: pkg.version
|
|
479
|
+
};
|
|
480
|
+
const fieldsToKeep = [
|
|
481
|
+
"description",
|
|
482
|
+
"keywords",
|
|
483
|
+
"author",
|
|
484
|
+
"contributors",
|
|
485
|
+
"maintainers",
|
|
486
|
+
"license",
|
|
487
|
+
"repository",
|
|
488
|
+
"bugs",
|
|
489
|
+
"homepage",
|
|
490
|
+
"funding",
|
|
491
|
+
"bin",
|
|
492
|
+
"scripts",
|
|
493
|
+
"dependencies",
|
|
494
|
+
"peerDependencies",
|
|
495
|
+
"optionalDependencies",
|
|
496
|
+
"bundledDependencies",
|
|
497
|
+
"engines",
|
|
498
|
+
"cpu",
|
|
499
|
+
"os",
|
|
500
|
+
"type",
|
|
501
|
+
"types",
|
|
502
|
+
"files",
|
|
503
|
+
"sideEffects",
|
|
504
|
+
"browserslist"
|
|
505
|
+
];
|
|
506
|
+
const pathFields = ["files", "types", "scripts"];
|
|
507
|
+
for (const field of fieldsToKeep) {
|
|
508
|
+
if (pkg[field] !== void 0) {
|
|
509
|
+
if (field === "scripts") {
|
|
510
|
+
const npmLifecycleScripts = ["postinstall", "preinstall", "install", "preuninstall", "postuninstall", "shrinkwrap"];
|
|
511
|
+
const filteredScripts = {};
|
|
512
|
+
for (const [scriptName, scriptValue] of Object.entries(pkg[field] || {})) {
|
|
513
|
+
if (npmLifecycleScripts.includes(scriptName)) {
|
|
514
|
+
filteredScripts[scriptName] = scriptValue;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (Object.keys(filteredScripts).length > 0) {
|
|
518
|
+
cleaned[field] = transformSrcToDist(filteredScripts);
|
|
519
|
+
}
|
|
520
|
+
} else if (field === "bin") {
|
|
521
|
+
cleaned[field] = transformSrcToDist(pkg[field]);
|
|
522
|
+
} else if (pathFields.includes(field)) {
|
|
523
|
+
cleaned[field] = transformSrcToDist(pkg[field]);
|
|
524
|
+
} else {
|
|
525
|
+
cleaned[field] = pkg[field];
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (!cleaned.type) {
|
|
530
|
+
cleaned.type = "module";
|
|
531
|
+
}
|
|
532
|
+
if (options.formats.cjs) {
|
|
533
|
+
cleaned.main = `src/${mainEntry}.cjs`;
|
|
534
|
+
}
|
|
535
|
+
cleaned.module = `src/${mainEntry}.js`;
|
|
536
|
+
cleaned.types = `src/${mainEntry}.d.ts`;
|
|
537
|
+
return cleaned;
|
|
538
|
+
}
|
|
539
|
+
async function fileExists(filePath) {
|
|
540
|
+
try {
|
|
541
|
+
await FS2.access(filePath);
|
|
542
|
+
return true;
|
|
543
|
+
} catch {
|
|
544
|
+
return false;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
async function build2(cwd, save = false) {
|
|
548
|
+
console.log("Building with libuild...");
|
|
549
|
+
const srcDir = Path2.join(cwd, "src");
|
|
550
|
+
const distDir = Path2.join(cwd, "dist");
|
|
551
|
+
const distSrcDir = Path2.join(distDir, "src");
|
|
552
|
+
if (!await fileExists(srcDir)) {
|
|
553
|
+
throw new Error("No src/ directory found");
|
|
554
|
+
}
|
|
555
|
+
const pkgPath = Path2.join(cwd, "package.json");
|
|
556
|
+
const pkg = JSON.parse(await FS2.readFile(pkgPath, "utf-8"));
|
|
557
|
+
if (!pkg.private) {
|
|
558
|
+
console.warn("\u26A0\uFE0F WARNING: Root package.json is not private - this could lead to accidental publishing of development package.json");
|
|
559
|
+
console.warn(" Consider setting 'private: true' in your root package.json");
|
|
560
|
+
}
|
|
561
|
+
const gitignorePath = Path2.join(cwd, ".gitignore");
|
|
562
|
+
if (await fileExists(gitignorePath)) {
|
|
563
|
+
const gitignoreContent = await FS2.readFile(gitignorePath, "utf-8");
|
|
564
|
+
const isDistIgnored = gitignoreContent.includes("dist/") || gitignoreContent.includes("/dist") || gitignoreContent.includes("dist\n") || gitignoreContent.includes("dist\r\n");
|
|
565
|
+
if (!isDistIgnored) {
|
|
566
|
+
console.warn("\u26A0\uFE0F WARNING: dist/ directory is not in .gitignore - built files should not be committed");
|
|
567
|
+
console.warn(" Add 'dist/' to your .gitignore file");
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
const entries = await findEntryPoints(srcDir);
|
|
571
|
+
if (entries.length === 0) {
|
|
572
|
+
throw new Error("No entry points found in src/");
|
|
573
|
+
}
|
|
574
|
+
const options = {
|
|
575
|
+
formats: {
|
|
576
|
+
esm: true,
|
|
577
|
+
// Always build ESM
|
|
578
|
+
cjs: !!pkg.main,
|
|
579
|
+
// Generate CJS only if main field exists
|
|
580
|
+
umd: entries.includes("umd")
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
const mainEntry = await detectMainEntry(pkg, entries);
|
|
584
|
+
console.log(" Found entries:", entries.join(", "));
|
|
585
|
+
console.log(" Main entry:", mainEntry);
|
|
586
|
+
if (options.formats.cjs) {
|
|
587
|
+
console.log(" Formats: ESM, CJS" + (options.formats.umd ? ", UMD" : ""));
|
|
588
|
+
} else {
|
|
589
|
+
console.log(" Formats: ESM" + (options.formats.umd ? ", UMD" : "") + " (no main field - CJS disabled)");
|
|
590
|
+
}
|
|
591
|
+
if (await fileExists(distDir)) {
|
|
592
|
+
await FS2.rm(distDir, { recursive: true });
|
|
593
|
+
}
|
|
594
|
+
await FS2.mkdir(distDir, { recursive: true });
|
|
595
|
+
const entryPoints = [];
|
|
596
|
+
const umdEntries = [];
|
|
597
|
+
for (const entry of entries) {
|
|
598
|
+
const entryPath = Path2.join(srcDir, `${entry}.ts`);
|
|
599
|
+
const jsEntryPath = Path2.join(srcDir, `${entry}.js`);
|
|
600
|
+
const actualPath = await fileExists(entryPath) ? entryPath : jsEntryPath;
|
|
601
|
+
if (!await fileExists(actualPath)) {
|
|
602
|
+
throw new Error(`Entry point file not found: ${actualPath}. Expected ${entry}.ts or ${entry}.js in src/ directory.`);
|
|
603
|
+
}
|
|
604
|
+
if (entry === "umd") {
|
|
605
|
+
umdEntries.push(actualPath);
|
|
606
|
+
} else {
|
|
607
|
+
entryPoints.push(actualPath);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (entryPoints.length > 0) {
|
|
611
|
+
console.log(` Building ${entryPoints.length} entries (ESM)...`);
|
|
612
|
+
await ESBuild.build({
|
|
613
|
+
entryPoints,
|
|
614
|
+
outdir: distSrcDir,
|
|
615
|
+
format: "esm",
|
|
616
|
+
entryNames: "[name]",
|
|
617
|
+
outExtension: { ".js": ".js" },
|
|
618
|
+
bundle: true,
|
|
619
|
+
splitting: true,
|
|
620
|
+
// Enable shared chunk extraction
|
|
621
|
+
minify: false,
|
|
622
|
+
sourcemap: true,
|
|
623
|
+
external: Object.keys(pkg.dependencies || {}),
|
|
624
|
+
platform: "node",
|
|
625
|
+
target: "node16"
|
|
626
|
+
});
|
|
627
|
+
if (options.formats.cjs) {
|
|
628
|
+
console.log(` Building ${entryPoints.length} entries (CJS)...`);
|
|
629
|
+
await ESBuild.build({
|
|
630
|
+
entryPoints,
|
|
631
|
+
outdir: distSrcDir,
|
|
632
|
+
format: "cjs",
|
|
633
|
+
entryNames: "[name]",
|
|
634
|
+
outExtension: { ".js": ".cjs" },
|
|
635
|
+
bundle: true,
|
|
636
|
+
minify: false,
|
|
637
|
+
sourcemap: true,
|
|
638
|
+
external: Object.keys(pkg.dependencies || {}),
|
|
639
|
+
platform: "node",
|
|
640
|
+
target: "node16"
|
|
641
|
+
// Note: CJS doesn't support splitting, but building together still helps with consistency
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
for (const umdPath of umdEntries) {
|
|
646
|
+
const entry = Path2.basename(umdPath, Path2.extname(umdPath));
|
|
647
|
+
console.log(` Building ${entry} (UMD)...`);
|
|
648
|
+
const globalName = pkg.name.includes("/") ? pkg.name.split("/").pop().replace(/-/g, "").charAt(0).toUpperCase() + pkg.name.split("/").pop().replace(/-/g, "").slice(1) : pkg.name.replace(/-/g, "").charAt(0).toUpperCase() + pkg.name.replace(/-/g, "").slice(1);
|
|
649
|
+
await ESBuild.build({
|
|
650
|
+
entryPoints: [umdPath],
|
|
651
|
+
outdir: distSrcDir,
|
|
652
|
+
format: "cjs",
|
|
653
|
+
entryNames: "[name]",
|
|
654
|
+
bundle: true,
|
|
655
|
+
minify: false,
|
|
656
|
+
sourcemap: true,
|
|
657
|
+
external: Object.keys(pkg.dependencies || {}),
|
|
658
|
+
platform: "node",
|
|
659
|
+
target: "node16",
|
|
660
|
+
plugins: [umdPlugin({ globalName })]
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
console.log(" Generating TypeScript declarations...");
|
|
664
|
+
const allTsFiles = [...entryPoints, ...umdEntries].filter((file) => file.endsWith(".ts"));
|
|
665
|
+
if (allTsFiles.length > 0) {
|
|
666
|
+
const compilerOptions = {
|
|
667
|
+
declaration: true,
|
|
668
|
+
emitDeclarationOnly: true,
|
|
669
|
+
outDir: distSrcDir,
|
|
670
|
+
rootDir: srcDir,
|
|
671
|
+
skipLibCheck: true,
|
|
672
|
+
esModuleInterop: true,
|
|
673
|
+
target: TS.ScriptTarget.ES2020,
|
|
674
|
+
module: TS.ModuleKind.ESNext
|
|
675
|
+
};
|
|
676
|
+
const program = TS.createProgram(allTsFiles, compilerOptions);
|
|
677
|
+
const emitResult = program.emit();
|
|
678
|
+
if (emitResult.diagnostics.length > 0) {
|
|
679
|
+
const diagnostics = TS.formatDiagnosticsWithColorAndContext(emitResult.diagnostics, {
|
|
680
|
+
getCanonicalFileName: (path) => path,
|
|
681
|
+
getCurrentDirectory: () => cwd,
|
|
682
|
+
getNewLine: () => "\n"
|
|
683
|
+
});
|
|
684
|
+
throw new Error(`TypeScript declaration generation failed:
|
|
685
|
+
${diagnostics}`);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
for (const entry of entries) {
|
|
689
|
+
if (entry === "umd")
|
|
690
|
+
continue;
|
|
691
|
+
const jsPath = Path2.join(distSrcDir, `${entry}.js`);
|
|
692
|
+
const dtsPath = Path2.join(distSrcDir, `${entry}.d.ts`);
|
|
693
|
+
if (await fileExists(jsPath) && await fileExists(dtsPath)) {
|
|
694
|
+
const content = await FS2.readFile(jsPath, "utf-8");
|
|
695
|
+
await FS2.writeFile(jsPath, `/// <reference types="./${entry}.d.ts" />
|
|
696
|
+
${content}`);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
const autoDiscoveredFiles = [];
|
|
700
|
+
console.log(" Generating package.json...");
|
|
701
|
+
const cleanedPkg = cleanPackageJSON(pkg, mainEntry, options);
|
|
702
|
+
const exportsResult = generateExports(entries, mainEntry, options, pkg.exports);
|
|
703
|
+
cleanedPkg.exports = fixExportsForDist(exportsResult.exports);
|
|
704
|
+
if (exportsResult.staleExports.length > 0) {
|
|
705
|
+
console.warn(`\u26A0\uFE0F WARNING: Found ${exportsResult.staleExports.length} stale export(s) pointing to missing src/ files:`);
|
|
706
|
+
for (const staleExport of exportsResult.staleExports) {
|
|
707
|
+
console.warn(` - ${staleExport}`);
|
|
708
|
+
}
|
|
709
|
+
if (save) {
|
|
710
|
+
console.log(" Removing stale exports from root package.json (--save mode)");
|
|
711
|
+
} else {
|
|
712
|
+
console.warn(" Use --save to remove these from root package.json");
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
if (cleanedPkg.files && Array.isArray(cleanedPkg.files)) {
|
|
716
|
+
for (const autoFile of autoDiscoveredFiles) {
|
|
717
|
+
if (!cleanedPkg.files.includes(autoFile)) {
|
|
718
|
+
cleanedPkg.files.push(autoFile);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
const fixedDistPkg = fixExportsForDist(cleanedPkg);
|
|
723
|
+
await FS2.writeFile(
|
|
724
|
+
Path2.join(distDir, "package.json"),
|
|
725
|
+
JSON.stringify(fixedDistPkg, null, 2) + "\n"
|
|
726
|
+
);
|
|
727
|
+
const defaultFilesToCopy = ["README.md", "LICENSE", "CHANGELOG.md"];
|
|
728
|
+
for (const file of defaultFilesToCopy) {
|
|
729
|
+
const srcPath = Path2.join(cwd, file);
|
|
730
|
+
if (await fileExists(srcPath)) {
|
|
731
|
+
console.log(` Copying ${file}...`);
|
|
732
|
+
await FS2.copyFile(srcPath, Path2.join(distDir, file));
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
const commonFiles = ["README.md", "LICENSE", "CHANGELOG.md", "COPYING", "AUTHORS"];
|
|
736
|
+
if (pkg.files && Array.isArray(pkg.files)) {
|
|
737
|
+
for (const commonFile of commonFiles) {
|
|
738
|
+
const commonPath = Path2.join(cwd, commonFile);
|
|
739
|
+
if (await fileExists(commonPath) && !pkg.files.includes(commonFile)) {
|
|
740
|
+
console.log(` Auto-discovered ${commonFile}, adding to files field...`);
|
|
741
|
+
pkg.files.push(commonFile);
|
|
742
|
+
autoDiscoveredFiles.push(commonFile);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (pkg.files && Array.isArray(pkg.files)) {
|
|
747
|
+
console.log(" Copying files from package.json files field...");
|
|
748
|
+
for (const pattern of pkg.files) {
|
|
749
|
+
if (typeof pattern === "string" && (pattern.includes("src") || pattern.includes("dist"))) {
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
if (typeof pattern === "string") {
|
|
753
|
+
const srcPath = Path2.join(cwd, pattern);
|
|
754
|
+
const destPath = Path2.join(distDir, pattern);
|
|
755
|
+
if (await fileExists(srcPath)) {
|
|
756
|
+
const stat2 = await FS2.stat(srcPath);
|
|
757
|
+
if (stat2.isDirectory()) {
|
|
758
|
+
console.log(` Copying directory ${pattern}/...`);
|
|
759
|
+
await FS2.mkdir(Path2.dirname(destPath), { recursive: true });
|
|
760
|
+
await FS2.cp(srcPath, destPath, { recursive: true });
|
|
761
|
+
} else {
|
|
762
|
+
console.log(` Copying ${pattern}...`);
|
|
763
|
+
await FS2.mkdir(Path2.dirname(destPath), { recursive: true });
|
|
764
|
+
await FS2.copyFile(srcPath, destPath);
|
|
765
|
+
}
|
|
766
|
+
} else if (pattern.includes("*")) {
|
|
767
|
+
const baseDir = pattern.split("*")[0].replace(/\/$/, "");
|
|
768
|
+
if (baseDir && await fileExists(Path2.join(cwd, baseDir))) {
|
|
769
|
+
console.log(` Copying pattern ${pattern}...`);
|
|
770
|
+
const baseSrcPath = Path2.join(cwd, baseDir);
|
|
771
|
+
const baseDestPath = Path2.join(distDir, baseDir);
|
|
772
|
+
await FS2.mkdir(Path2.dirname(baseDestPath), { recursive: true });
|
|
773
|
+
await FS2.cp(baseSrcPath, baseDestPath, { recursive: true });
|
|
774
|
+
} else {
|
|
775
|
+
throw new Error(`Pattern base directory not found for "${pattern}". Expected directory: ${Path2.join(cwd, baseDir)}`);
|
|
776
|
+
}
|
|
777
|
+
} else {
|
|
778
|
+
throw new Error(`File specified in files field not found: ${srcPath}. Remove "${pattern}" from package.json files field or create the file.`);
|
|
779
|
+
}
|
|
780
|
+
} else {
|
|
781
|
+
throw new Error(`Invalid files field entry: ${JSON.stringify(pattern)}. Files field entries must be strings.`);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
if (save) {
|
|
786
|
+
console.log(" Updating root package.json...");
|
|
787
|
+
const rootPkg2 = { ...pkg };
|
|
788
|
+
rootPkg2.private = true;
|
|
789
|
+
if (options.formats.cjs) {
|
|
790
|
+
rootPkg2.main = `./dist/src/${mainEntry}.cjs`;
|
|
791
|
+
}
|
|
792
|
+
rootPkg2.module = `./dist/src/${mainEntry}.js`;
|
|
793
|
+
rootPkg2.types = `./dist/src/${mainEntry}.d.ts`;
|
|
794
|
+
if (rootPkg2.typings && typeof rootPkg2.typings === "string") {
|
|
795
|
+
rootPkg2.typings = rootPkg2.typings.startsWith("./dist/") ? rootPkg2.typings : "./" + Path2.join("dist", rootPkg2.typings);
|
|
796
|
+
}
|
|
797
|
+
const rootExports = {};
|
|
798
|
+
for (const [key, value] of Object.entries(cleanedPkg.exports)) {
|
|
799
|
+
if (typeof value === "string") {
|
|
800
|
+
rootExports[key] = value.startsWith("./dist/") ? value : `./dist${value.startsWith(".") ? value.slice(1) : value}`;
|
|
801
|
+
} else if (typeof value === "object" && value !== null) {
|
|
802
|
+
rootExports[key] = {};
|
|
803
|
+
for (const [subKey, subValue] of Object.entries(value)) {
|
|
804
|
+
if (typeof subValue === "string") {
|
|
805
|
+
rootExports[key][subKey] = subValue.startsWith("./dist/") ? subValue : `./dist${subValue.startsWith(".") ? subValue.slice(1) : subValue}`;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
rootPkg2.exports = rootExports;
|
|
811
|
+
if (rootPkg2.bin) {
|
|
812
|
+
if (typeof rootPkg2.bin === "string") {
|
|
813
|
+
if (!rootPkg2.bin.startsWith("./dist/")) {
|
|
814
|
+
rootPkg2.bin = "./" + Path2.join("dist", rootPkg2.bin);
|
|
815
|
+
}
|
|
816
|
+
} else {
|
|
817
|
+
for (const [name, binPath] of Object.entries(rootPkg2.bin)) {
|
|
818
|
+
if (typeof binPath === "string" && !binPath.startsWith("./dist/")) {
|
|
819
|
+
rootPkg2.bin[name] = "./" + Path2.join("dist", binPath);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
if (pkg.files !== void 0) {
|
|
825
|
+
if (!rootPkg2.files) {
|
|
826
|
+
rootPkg2.files = [];
|
|
827
|
+
} else if (!Array.isArray(rootPkg2.files)) {
|
|
828
|
+
rootPkg2.files = [rootPkg2.files];
|
|
829
|
+
}
|
|
830
|
+
for (const autoFile of autoDiscoveredFiles) {
|
|
831
|
+
if (!rootPkg2.files.includes(autoFile)) {
|
|
832
|
+
rootPkg2.files.push(autoFile);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
if (!rootPkg2.files.includes("dist/") && !rootPkg2.files.includes("dist")) {
|
|
836
|
+
rootPkg2.files.push("dist/");
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
await FS2.writeFile(pkgPath, JSON.stringify(rootPkg2, null, 2) + "\n");
|
|
840
|
+
} else {
|
|
841
|
+
console.log(" Skipping root package.json update (use --save to enable)");
|
|
842
|
+
}
|
|
843
|
+
console.log("\nBuild complete!");
|
|
844
|
+
console.log(`
|
|
845
|
+
Output: ${distDir}`);
|
|
846
|
+
console.log(`
|
|
847
|
+
Entries: ${entries.length}`);
|
|
848
|
+
if (options.formats.cjs) {
|
|
849
|
+
console.log(`
|
|
850
|
+
Formats: ESM, CJS${options.formats.umd ? ", UMD" : ""}`);
|
|
851
|
+
} else {
|
|
852
|
+
console.log(`
|
|
853
|
+
Formats: ESM${options.formats.umd ? ", UMD" : ""}`);
|
|
854
|
+
}
|
|
855
|
+
let rootPkg = pkg;
|
|
856
|
+
if (save) {
|
|
857
|
+
rootPkg = JSON.parse(await FS2.readFile(pkgPath, "utf-8"));
|
|
858
|
+
}
|
|
859
|
+
return { distPkg: fixedDistPkg, rootPkg };
|
|
860
|
+
}
|
|
861
|
+
async function publish(cwd, save = true) {
|
|
862
|
+
await build2(cwd, save);
|
|
863
|
+
console.log("\nPublishing to npm...");
|
|
864
|
+
const distDir = Path2.join(cwd, "dist");
|
|
865
|
+
const distPkgPath = Path2.join(distDir, "package.json");
|
|
866
|
+
if (!await fileExists(distPkgPath)) {
|
|
867
|
+
throw new Error("No dist/package.json found. Run 'libuild build' first.");
|
|
868
|
+
}
|
|
869
|
+
const proc = (0, import_child_process.spawn)("npm", ["publish"], {
|
|
870
|
+
cwd: distDir,
|
|
871
|
+
stdio: "inherit"
|
|
872
|
+
});
|
|
873
|
+
const exitCode = await new Promise((resolve) => {
|
|
874
|
+
proc.on("close", resolve);
|
|
875
|
+
});
|
|
876
|
+
if (exitCode === 0) {
|
|
877
|
+
const distPkg = JSON.parse(await FS2.readFile(distPkgPath, "utf-8"));
|
|
878
|
+
console.log(`
|
|
879
|
+
Published ${distPkg.name}@${distPkg.version}!`);
|
|
880
|
+
} else {
|
|
881
|
+
throw new Error("npm publish failed");
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// src/cli.ts
|
|
886
|
+
var { values, positionals } = (0, import_util.parseArgs)({
|
|
887
|
+
args: process.argv.slice(2),
|
|
888
|
+
options: {
|
|
889
|
+
help: { type: "boolean", short: "h" },
|
|
890
|
+
version: { type: "boolean", short: "v" },
|
|
891
|
+
save: { type: "boolean" },
|
|
892
|
+
"no-save": { type: "boolean" }
|
|
893
|
+
},
|
|
894
|
+
allowPositionals: true
|
|
895
|
+
});
|
|
896
|
+
var HELP_TEXT = `
|
|
897
|
+
libuild - Zero-config library builds
|
|
898
|
+
|
|
899
|
+
Usage:
|
|
900
|
+
libuild [command]
|
|
901
|
+
|
|
902
|
+
Commands:
|
|
903
|
+
build Build the library (default command)
|
|
904
|
+
publish Build and publish the library
|
|
905
|
+
|
|
906
|
+
Options:
|
|
907
|
+
--save Update root package.json to point to dist files
|
|
908
|
+
--no-save Skip package.json updates (for publish command)
|
|
909
|
+
|
|
910
|
+
Examples:
|
|
911
|
+
libuild # Build the library
|
|
912
|
+
libuild build --save # Build and update package.json for npm link
|
|
913
|
+
libuild publish # Build and publish to npm
|
|
914
|
+
`;
|
|
915
|
+
async function main() {
|
|
916
|
+
if (values.help) {
|
|
917
|
+
console.log(HELP_TEXT);
|
|
918
|
+
process.exit(0);
|
|
919
|
+
}
|
|
920
|
+
if (values.version) {
|
|
921
|
+
const pkg = await Promise.resolve().then(() => __toESM(require_package(), 1));
|
|
922
|
+
console.log(pkg.version);
|
|
923
|
+
process.exit(0);
|
|
924
|
+
}
|
|
925
|
+
const command = positionals[0] || "build";
|
|
926
|
+
const cwd = process.cwd();
|
|
927
|
+
const shouldSave = values.save || command === "publish" && !values["no-save"];
|
|
928
|
+
try {
|
|
929
|
+
switch (command) {
|
|
930
|
+
case "build":
|
|
931
|
+
await build2(cwd, shouldSave);
|
|
932
|
+
break;
|
|
933
|
+
case "publish":
|
|
934
|
+
case "ship":
|
|
935
|
+
await publish(cwd, shouldSave);
|
|
936
|
+
break;
|
|
937
|
+
default:
|
|
938
|
+
console.error(`Unknown command: ${command}`);
|
|
939
|
+
console.log(HELP_TEXT);
|
|
940
|
+
process.exit(1);
|
|
941
|
+
}
|
|
942
|
+
} catch (error) {
|
|
943
|
+
console.error("Error:", error.message);
|
|
944
|
+
process.exit(1);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
main();
|
|
948
|
+
//# sourceMappingURL=cli.cjs.map
|