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