@b9g/libuild 0.1.11 → 0.1.13

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/src/libuild.cjs CHANGED
@@ -215088,6 +215088,21 @@ function externalEntrypointsPlugin(options) {
215088
215088
  };
215089
215089
  }
215090
215090
  });
215091
+ build3.onResolve({ filter: /^\.\.\/(?:src|bin)\// }, (args) => {
215092
+ const withoutExt = args.path.replace(/\.(ts|js)$/, "");
215093
+ const match = withoutExt.match(/^\.\.\/(?:src|bin)\/(.+)$/);
215094
+ if (match) {
215095
+ const entryName = match[1];
215096
+ if (externalEntries.includes(entryName)) {
215097
+ const dir = withoutExt.match(/^(\.\.\/(?:src|bin))\//)?.[1];
215098
+ return {
215099
+ path: `${dir}/${entryName}${outputExtension}`,
215100
+ external: true
215101
+ };
215102
+ }
215103
+ }
215104
+ return void 0;
215105
+ });
215091
215106
  }
215092
215107
  };
215093
215108
  }
@@ -215113,27 +215128,34 @@ function dtsPlugin(options) {
215113
215128
  let TS;
215114
215129
  try {
215115
215130
  TS = await Promise.resolve().then(() => __toESM(require_typescript(), 1));
215116
- } catch {
215131
+ } catch (error) {
215117
215132
  return;
215118
215133
  }
215119
215134
  const tsFiles = entryFiles.filter((file) => {
215120
215135
  if (!file.endsWith(".ts"))
215121
215136
  return false;
215122
215137
  return options.entryPoints.some((entryPoint) => {
215123
- const normalizedEntry = Path2.resolve(entryPoint);
215124
- const normalizedFile = Path2.resolve(file);
215125
- return normalizedEntry === normalizedFile;
215138
+ try {
215139
+ const fs = require("fs");
215140
+ const normalizedEntry = fs.realpathSync(Path2.resolve(entryPoint));
215141
+ const normalizedFile = fs.realpathSync(Path2.resolve(file));
215142
+ return normalizedEntry === normalizedFile;
215143
+ } catch {
215144
+ const normalizedEntry = Path2.resolve(entryPoint);
215145
+ const normalizedFile = Path2.resolve(file);
215146
+ return normalizedEntry === normalizedFile;
215147
+ }
215126
215148
  });
215127
215149
  });
215128
215150
  if (tsFiles.length === 0) {
215129
215151
  return;
215130
215152
  }
215131
215153
  try {
215154
+ await FS2.mkdir(options.outDir, { recursive: true });
215132
215155
  const compilerOptions = {
215133
215156
  declaration: true,
215134
215157
  emitDeclarationOnly: true,
215135
215158
  outDir: options.outDir,
215136
- rootDir: options.rootDir,
215137
215159
  skipLibCheck: true,
215138
215160
  esModuleInterop: true,
215139
215161
  target: TS.ScriptTarget.ES2020,
@@ -215208,6 +215230,38 @@ ${content}`;
215208
215230
  }
215209
215231
 
215210
215232
  // src/libuild.ts
215233
+ function generateRuntimeBanner(pkg) {
215234
+ const prefersBun = pkg.engines?.bun !== void 0;
215235
+ if (prefersBun) {
215236
+ return '//bin/true; exec "$({ [ "${npm_config_user_agent#bun/}" != "$npm_config_user_agent" ] || true; } && command -v bun || command -v node)" "$0" "$@"';
215237
+ } else {
215238
+ return '//bin/true; exec "$([ "${npm_config_user_agent#bun/}" != "$npm_config_user_agent" ] && command -v bun || command -v node)" "$0" "$@"';
215239
+ }
215240
+ }
215241
+ async function processJavaScriptExecutable(filePath, runtimeBanner) {
215242
+ try {
215243
+ const contents = await FS3.readFile(filePath, "utf8");
215244
+ const lines = contents.split("\n");
215245
+ let modified = false;
215246
+ if (lines[0]?.startsWith("#!")) {
215247
+ const existingShebang = lines[0];
215248
+ if (existingShebang.includes("python") || existingShebang.includes("bash") || !existingShebang.includes("node") && !existingShebang.includes("bun") && !existingShebang.includes("sh")) {
215249
+ console.warn(`\u26A0\uFE0F WARNING: ${filePath} has non-JavaScript shebang but is a JS file. Adding dual runtime support.`);
215250
+ }
215251
+ lines[0] = "#!/usr/bin/env sh";
215252
+ lines.splice(1, 0, runtimeBanner);
215253
+ modified = true;
215254
+ } else {
215255
+ lines.unshift("#!/usr/bin/env sh", runtimeBanner);
215256
+ modified = true;
215257
+ }
215258
+ if (modified) {
215259
+ await FS3.writeFile(filePath, lines.join("\n"));
215260
+ }
215261
+ } catch (error) {
215262
+ console.warn(`\u26A0\uFE0F WARNING: Could not process executable ${filePath}: ${error.message}`);
215263
+ }
215264
+ }
215211
215265
  function isValidEntrypoint(filename) {
215212
215266
  if (!filename.endsWith(".ts") && !filename.endsWith(".js"))
215213
215267
  return false;
@@ -215234,6 +215288,22 @@ async function findEntrypoints(srcDir) {
215234
215288
  return isValidEntrypoint(dirent.name);
215235
215289
  }).map((dirent) => Path3.basename(dirent.name, Path3.extname(dirent.name))).sort();
215236
215290
  }
215291
+ async function findBinEntrypoints(binDir) {
215292
+ try {
215293
+ const files = await FS3.readdir(binDir, { withFileTypes: true });
215294
+ return files.filter((dirent) => {
215295
+ if (dirent.isDirectory()) {
215296
+ return false;
215297
+ }
215298
+ return isValidEntrypoint(dirent.name);
215299
+ }).map((dirent) => Path3.basename(dirent.name, Path3.extname(dirent.name))).sort();
215300
+ } catch (error) {
215301
+ if (error.code === "ENOENT") {
215302
+ return [];
215303
+ }
215304
+ throw error;
215305
+ }
215306
+ }
215237
215307
  function detectMainEntry(pkg, entries) {
215238
215308
  function extractEntryFromPath(path) {
215239
215309
  const match = path.match(/\.\/src\/([^.]+)/);
@@ -215331,18 +215401,38 @@ function checkIfExportIsStale(exportKey, exportValue, entries) {
215331
215401
  }
215332
215402
  return entryName ? !entries.includes(entryName) : false;
215333
215403
  }
215334
- function generateExports(entries, mainEntry, options, existingExports = {}) {
215404
+ async function generateExports(entries, mainEntry, options, existingExports = {}, distDir, allBinEntries = []) {
215335
215405
  const exports2 = {};
215336
215406
  const staleExports = [];
215337
- function createExportEntry(entry) {
215338
- const exportEntry = {
215339
- types: `./src/${entry}.d.ts`,
215340
- import: `./src/${entry}.js`
215341
- };
215342
- if (options.formats.cjs) {
215343
- exportEntry.require = `./src/${entry}.cjs`;
215407
+ async function createExportEntry(entry) {
215408
+ if (entry.startsWith("bin/")) {
215409
+ const binEntry = entry.replace("bin/", "");
215410
+ const exportEntry = {
215411
+ import: `./bin/${binEntry}.js`
215412
+ };
215413
+ if (distDir) {
215414
+ const dtsPath = Path3.join(distDir, "bin", `${binEntry}.d.ts`);
215415
+ if (await fileExists(dtsPath)) {
215416
+ exportEntry.types = `./bin/${binEntry}.d.ts`;
215417
+ }
215418
+ }
215419
+ return exportEntry;
215420
+ } else {
215421
+ const exportEntry = {
215422
+ import: `./src/${entry}.js`
215423
+ };
215424
+ if (distDir) {
215425
+ const dtsPath = Path3.join(distDir, "src", `${entry}.d.ts`);
215426
+ const exists = await fileExists(dtsPath);
215427
+ if (exists) {
215428
+ exportEntry.types = `./src/${entry}.d.ts`;
215429
+ }
215430
+ }
215431
+ if (options.formats.cjs) {
215432
+ exportEntry.require = `./src/${entry}.cjs`;
215433
+ }
215434
+ return exportEntry;
215344
215435
  }
215345
- return exportEntry;
215346
215436
  }
215347
215437
  function expandExistingExport(existing, entryFromPath) {
215348
215438
  if (typeof existing === "string") {
@@ -215416,7 +215506,7 @@ function generateExports(entries, mainEntry, options, existingExports = {}) {
215416
215506
  }
215417
215507
  }
215418
215508
  if (!exports2["."]) {
215419
- exports2["."] = createExportEntry(mainEntry);
215509
+ exports2["."] = await createExportEntry(mainEntry);
215420
215510
  } else {
215421
215511
  exports2["."] = expandExistingExport(exports2["."], mainEntry);
215422
215512
  }
@@ -215425,7 +215515,7 @@ function generateExports(entries, mainEntry, options, existingExports = {}) {
215425
215515
  continue;
215426
215516
  const key = `./${entry}`;
215427
215517
  if (!exports2[key]) {
215428
- exports2[key] = createExportEntry(entry);
215518
+ exports2[key] = await createExportEntry(entry);
215429
215519
  } else {
215430
215520
  exports2[key] = expandExistingExport(exports2[key], entry);
215431
215521
  }
@@ -215478,13 +215568,30 @@ async function validateSingleBinPath(binPath, fieldName, cwd, save) {
215478
215568
  if (save) {
215479
215569
  return;
215480
215570
  }
215481
- if (binPath.startsWith("dist/src/") || binPath.startsWith("./dist/src/")) {
215571
+ if (binPath.startsWith("dist/src/") || binPath.startsWith("./dist/src/") || binPath.startsWith("dist/bin/") || binPath.startsWith("./dist/bin/")) {
215482
215572
  const fullPath2 = Path3.join(cwd, binPath);
215483
215573
  const distExists = await fileExists(fullPath2);
215484
215574
  if (distExists) {
215485
215575
  return;
215486
215576
  }
215487
- const srcPath = binPath.startsWith("./dist/src/") ? binPath.replace("./dist/src/", "src/") : binPath.replace("dist/src/", "src/");
215577
+ let srcPath;
215578
+ let sourceDir;
215579
+ if (binPath.startsWith("./dist/src/")) {
215580
+ srcPath = binPath.replace("./dist/src/", "src/");
215581
+ sourceDir = "src";
215582
+ } else if (binPath.startsWith("dist/src/")) {
215583
+ srcPath = binPath.replace("dist/src/", "src/");
215584
+ sourceDir = "src";
215585
+ } else if (binPath.startsWith("./dist/bin/")) {
215586
+ srcPath = binPath.replace("./dist/bin/", "bin/");
215587
+ sourceDir = "bin";
215588
+ } else if (binPath.startsWith("dist/bin/")) {
215589
+ srcPath = binPath.replace("dist/bin/", "bin/");
215590
+ sourceDir = "bin";
215591
+ } else {
215592
+ srcPath = binPath;
215593
+ sourceDir = "src";
215594
+ }
215488
215595
  const basePath = srcPath.replace(/\.(js|cjs|mjs)$/, "");
215489
215596
  const tsPath = Path3.join(cwd, basePath + ".ts");
215490
215597
  const jsPath = Path3.join(cwd, basePath + ".js");
@@ -215511,7 +215618,16 @@ async function validateSingleBinPath(binPath, fieldName, cwd, save) {
215511
215618
  if (pathExists) {
215512
215619
  return;
215513
215620
  }
215514
- if (binPath.startsWith("src/") || binPath.startsWith("./src/")) {
215621
+ if (binPath.startsWith("src/") || binPath.startsWith("./src/") || binPath.startsWith("bin/") || binPath.startsWith("./bin/")) {
215622
+ let normalizedPath = binPath.startsWith("./") ? binPath.slice(2) : binPath;
215623
+ if (normalizedPath.startsWith("src/")) {
215624
+ const srcRelativePath = normalizedPath.replace("src/", "");
215625
+ if (srcRelativePath.includes("/")) {
215626
+ console.warn(`\u26A0\uFE0F WARNING: ${fieldName} field points to "${binPath}" which is in a subdirectory of src/.`);
215627
+ console.warn(` libuild only auto-detects top-level src/ files as entrypoints.`);
215628
+ console.warn(` Consider moving the file to the top-level src/ directory or use the bin/ directory for executables.`);
215629
+ }
215630
+ }
215515
215631
  const basePath = fullPath.replace(/\.(js|cjs|mjs)$/, "");
215516
215632
  const tsPath = basePath + ".ts";
215517
215633
  const jsPath = basePath + ".js";
@@ -215536,10 +215652,19 @@ function transformBinPaths(value) {
215536
215652
  if (value.startsWith("dist/src/")) {
215537
215653
  return value.replace("dist/", "");
215538
215654
  }
215655
+ if (value.startsWith("./dist/bin/")) {
215656
+ return value.replace("./dist/", "");
215657
+ }
215658
+ if (value.startsWith("dist/bin/")) {
215659
+ return value.replace("dist/", "");
215660
+ }
215539
215661
  if (value.startsWith("./src/")) {
215540
215662
  return value.replace("./", "");
215541
215663
  }
215542
- if (value.startsWith("src/") || value === "src") {
215664
+ if (value.startsWith("./bin/")) {
215665
+ return value.replace("./", "");
215666
+ }
215667
+ if (value.startsWith("src/") || value === "src" || value.startsWith("bin/") || value === "bin") {
215543
215668
  return value;
215544
215669
  }
215545
215670
  return value;
@@ -215584,6 +215709,60 @@ function fixExportsForDist(obj) {
215584
215709
  }
215585
215710
  return obj;
215586
215711
  }
215712
+ async function setExecutablePermissions(filePath) {
215713
+ try {
215714
+ await FS3.chmod(filePath, 493);
215715
+ } catch (error) {
215716
+ console.warn(`\u26A0\uFE0F WARNING: Could not set executable permissions for ${filePath}: ${error.message}`);
215717
+ }
215718
+ }
215719
+ async function makeFilesExecutable(pkg, cwd, allBinEntries, runtimeBanner) {
215720
+ const filesToMakeExecutable = [];
215721
+ const filesToProcessForDualRuntime = [];
215722
+ const distDir = Path3.join(cwd, "dist");
215723
+ if (pkg.bin) {
215724
+ if (typeof pkg.bin === "string") {
215725
+ const fullPath = Path3.join(distDir, pkg.bin);
215726
+ filesToMakeExecutable.push(fullPath);
215727
+ if (pkg.bin.endsWith(".js")) {
215728
+ filesToProcessForDualRuntime.push(fullPath);
215729
+ }
215730
+ } else if (typeof pkg.bin === "object") {
215731
+ for (const binPath of Object.values(pkg.bin)) {
215732
+ if (typeof binPath === "string") {
215733
+ const fullPath = Path3.join(distDir, binPath);
215734
+ filesToMakeExecutable.push(fullPath);
215735
+ if (binPath.endsWith(".js")) {
215736
+ filesToProcessForDualRuntime.push(fullPath);
215737
+ }
215738
+ }
215739
+ }
215740
+ }
215741
+ }
215742
+ const binDirFiles = /* @__PURE__ */ new Set();
215743
+ for (const binEntryInfo of allBinEntries) {
215744
+ const baseDir = binEntryInfo.source === "src" ? "dist/src/bin" : "dist/bin";
215745
+ const jsPath = Path3.join(cwd, baseDir, `${binEntryInfo.name}.js`);
215746
+ const cjsPath = Path3.join(cwd, baseDir, `${binEntryInfo.name}.cjs`);
215747
+ binDirFiles.add(jsPath);
215748
+ if (!filesToMakeExecutable.includes(jsPath)) {
215749
+ filesToMakeExecutable.push(jsPath);
215750
+ }
215751
+ if (!filesToMakeExecutable.includes(cjsPath)) {
215752
+ filesToMakeExecutable.push(cjsPath);
215753
+ }
215754
+ }
215755
+ for (const filePath of filesToProcessForDualRuntime) {
215756
+ if (await fileExists(filePath) && !binDirFiles.has(filePath)) {
215757
+ await processJavaScriptExecutable(filePath, runtimeBanner);
215758
+ }
215759
+ }
215760
+ for (const filePath of filesToMakeExecutable) {
215761
+ if (await fileExists(filePath)) {
215762
+ await setExecutablePermissions(filePath);
215763
+ }
215764
+ }
215765
+ }
215587
215766
  async function resolveWorkspaceDependencies(dependencies, cwd) {
215588
215767
  if (!dependencies)
215589
215768
  return void 0;
@@ -215643,7 +215822,7 @@ async function resolveWorkspaceVersion(packageName, workspaceSpec, cwd) {
215643
215822
  return workspaceSpec;
215644
215823
  }
215645
215824
  }
215646
- async function cleanPackageJSON(pkg, mainEntry, options, cwd) {
215825
+ async function cleanPackageJSON(pkg, mainEntry, options, cwd, distDir) {
215647
215826
  const cleaned = {
215648
215827
  name: pkg.name,
215649
215828
  version: pkg.version
@@ -215708,7 +215887,13 @@ async function cleanPackageJSON(pkg, mainEntry, options, cwd) {
215708
215887
  cleaned.main = `src/${mainEntry}.cjs`;
215709
215888
  }
215710
215889
  cleaned.module = `src/${mainEntry}.js`;
215711
- cleaned.types = `src/${mainEntry}.d.ts`;
215890
+ if (distDir) {
215891
+ const dtsPath = Path3.join(distDir, "src", `${mainEntry}.d.ts`);
215892
+ const exists = await fileExists(dtsPath);
215893
+ if (exists) {
215894
+ cleaned.types = `src/${mainEntry}.d.ts`;
215895
+ }
215896
+ }
215712
215897
  return cleaned;
215713
215898
  }
215714
215899
  async function fileExists(filePath) {
@@ -215760,9 +215945,19 @@ async function build2(cwd, save = false) {
215760
215945
  console.warn(" Add 'dist/' to your .gitignore file");
215761
215946
  }
215762
215947
  }
215763
- const entries = await findEntrypoints(srcDir);
215764
- if (entries.length === 0) {
215765
- throw new Error("No entry points found in src/");
215948
+ const binDir = Path3.join(cwd, "bin");
215949
+ const srcEntries = await findEntrypoints(srcDir);
215950
+ const binEntries = await findBinEntrypoints(binDir);
215951
+ const allBinEntries = binEntries.map((entry) => ({ name: entry, source: "top-level" }));
215952
+ const entries = [
215953
+ ...srcEntries,
215954
+ ...allBinEntries.map((entry) => `bin/${entry.name}`)
215955
+ ];
215956
+ if (srcEntries.length === 0 && allBinEntries.length === 0) {
215957
+ throw new Error("No entry points found in src/ or bin/");
215958
+ }
215959
+ if (allBinEntries.length > 0) {
215960
+ console.info(` Found bin entries: ${allBinEntries.map((entry) => entry.name).join(", ")}`);
215766
215961
  }
215767
215962
  const options = {
215768
215963
  formats: {
@@ -215773,7 +215968,7 @@ async function build2(cwd, save = false) {
215773
215968
  umd: entries.includes("umd")
215774
215969
  }
215775
215970
  };
215776
- const mainEntry = detectMainEntry(pkg, entries);
215971
+ const mainEntry = detectMainEntry(pkg, srcEntries);
215777
215972
  console.info(" Found entries:", entries.join(", "));
215778
215973
  console.info(" Main entry:", mainEntry);
215779
215974
  if (options.formats.cjs) {
@@ -215804,11 +215999,29 @@ async function build2(cwd, save = false) {
215804
215999
  const entryPoints = [];
215805
216000
  const umdEntries = [];
215806
216001
  for (const entry of entries) {
215807
- const entryPath = Path3.join(srcDir, `${entry}.ts`);
215808
- const jsEntryPath = Path3.join(srcDir, `${entry}.js`);
216002
+ let entryPath;
216003
+ let jsEntryPath;
216004
+ let sourceDir;
216005
+ if (entry.startsWith("bin/")) {
216006
+ const binEntryName = entry.replace("bin/", "");
216007
+ const binEntryInfo = allBinEntries.find((binEntry) => binEntry.name === binEntryName);
216008
+ if (binEntryInfo?.source === "src") {
216009
+ entryPath = Path3.join(srcDir, "bin", `${binEntryName}.ts`);
216010
+ jsEntryPath = Path3.join(srcDir, "bin", `${binEntryName}.js`);
216011
+ sourceDir = "src/bin/";
216012
+ } else {
216013
+ entryPath = Path3.join(binDir, `${binEntryName}.ts`);
216014
+ jsEntryPath = Path3.join(binDir, `${binEntryName}.js`);
216015
+ sourceDir = "bin/";
216016
+ }
216017
+ } else {
216018
+ entryPath = Path3.join(srcDir, `${entry}.ts`);
216019
+ jsEntryPath = Path3.join(srcDir, `${entry}.js`);
216020
+ sourceDir = "src/";
216021
+ }
215809
216022
  const actualPath = await fileExists(entryPath) ? entryPath : jsEntryPath;
215810
216023
  if (!await fileExists(actualPath)) {
215811
- throw new Error(`Entry point file not found: ${actualPath}. Expected ${entry}.ts or ${entry}.js in src/ directory.`);
216024
+ throw new Error(`Entry point file not found: ${actualPath}. Expected ${entry.replace("bin/", "")}.ts or ${entry.replace("bin/", "")}.js in ${sourceDir} directory.`);
215812
216025
  }
215813
216026
  if (entry === "umd") {
215814
216027
  umdEntries.push(actualPath);
@@ -215816,58 +216029,107 @@ async function build2(cwd, save = false) {
215816
216029
  entryPoints.push(actualPath);
215817
216030
  }
215818
216031
  }
216032
+ const srcEntryPoints = entryPoints.filter((path) => path.includes(srcDir));
216033
+ const binEntryPoints = entryPoints.filter((path) => path.includes(binDir));
216034
+ const distBinDir = Path3.join(distDir, "bin");
216035
+ if (binEntryPoints.length > 0) {
216036
+ await FS3.mkdir(distBinDir, { recursive: true });
216037
+ }
215819
216038
  if (entryPoints.length > 0) {
215820
- const entryNames = entryPoints.map((path) => {
216039
+ console.info(` Building ${entryPoints.length} entries (ESM)...`);
216040
+ const srcEntryNames = srcEntryPoints.map((path) => {
215821
216041
  const name = Path3.basename(path, Path3.extname(path));
215822
216042
  return name;
215823
216043
  });
215824
- console.info(` Building ${entryPoints.length} entries (ESM)...`);
215825
- await ESBuild.build({
215826
- entryPoints,
215827
- outdir: distSrcDir,
215828
- format: "esm",
215829
- outExtension: { ".js": ".js" },
215830
- bundle: true,
215831
- minify: false,
215832
- sourcemap: false,
215833
- external: externalDeps,
215834
- platform: "node",
215835
- target: "node18",
215836
- packages: "external",
215837
- supported: { "import-attributes": true },
215838
- plugins: [
215839
- externalEntrypointsPlugin({
215840
- entryNames,
215841
- outputExtension: ".js"
215842
- }),
215843
- dtsPlugin({
215844
- outDir: distSrcDir,
215845
- rootDir: srcDir,
215846
- entryPoints
215847
- })
215848
- ]
216044
+ const binEntryNames = binEntryPoints.map((path) => {
216045
+ const name = Path3.basename(path, Path3.extname(path));
216046
+ return name;
215849
216047
  });
215850
- if (options.formats.cjs) {
215851
- console.info(` Building ${entryPoints.length} entries (CJS)...`);
216048
+ const allESMEntryPoints = [...srcEntryPoints, ...binEntryPoints];
216049
+ const allEntryNames = [...srcEntryNames, ...binEntryNames];
216050
+ if (allESMEntryPoints.length > 0) {
215852
216051
  await ESBuild.build({
215853
- entryPoints,
215854
- outdir: distSrcDir,
215855
- format: "cjs",
215856
- outExtension: { ".js": ".cjs" },
216052
+ entryPoints: allESMEntryPoints,
216053
+ outdir: distDir,
216054
+ outbase: cwd,
216055
+ // Preserve src/ and bin/ directory structure
216056
+ format: "esm",
216057
+ outExtension: { ".js": ".js" },
215857
216058
  bundle: true,
215858
216059
  minify: false,
215859
216060
  sourcemap: false,
215860
216061
  external: externalDeps,
215861
216062
  platform: "node",
215862
216063
  target: "node18",
216064
+ packages: "external",
215863
216065
  supported: { "import-attributes": true },
215864
216066
  plugins: [
215865
216067
  externalEntrypointsPlugin({
215866
- entryNames,
215867
- outputExtension: ".cjs"
215868
- })
216068
+ entryNames: allEntryNames,
216069
+ outputExtension: ".js"
216070
+ }),
216071
+ // Generate TypeScript declarations for src entries
216072
+ ...srcEntryPoints.length > 0 ? [dtsPlugin({
216073
+ outDir: distSrcDir,
216074
+ rootDir: srcDir,
216075
+ entryPoints: srcEntryPoints
216076
+ })] : [],
216077
+ // Generate TypeScript declarations for bin entries
216078
+ ...binEntryPoints.length > 0 ? [dtsPlugin({
216079
+ outDir: distBinDir,
216080
+ rootDir: binDir,
216081
+ entryPoints: binEntryPoints
216082
+ })] : []
215869
216083
  ]
215870
216084
  });
216085
+ if (binEntryPoints.length > 0) {
216086
+ const runtimeBanner = generateRuntimeBanner(pkg);
216087
+ for (const binEntryName of binEntryNames) {
216088
+ const outputPath = Path3.join(distBinDir, `${binEntryName}.js`);
216089
+ await processJavaScriptExecutable(outputPath, runtimeBanner);
216090
+ }
216091
+ }
216092
+ }
216093
+ if (options.formats.cjs) {
216094
+ console.info(` Building ${entryPoints.length} entries (CJS)...`);
216095
+ if (srcEntryPoints.length > 0) {
216096
+ try {
216097
+ await ESBuild.build({
216098
+ entryPoints: srcEntryPoints,
216099
+ outdir: distSrcDir,
216100
+ format: "cjs",
216101
+ outExtension: { ".js": ".cjs" },
216102
+ bundle: true,
216103
+ minify: false,
216104
+ sourcemap: false,
216105
+ external: externalDeps,
216106
+ platform: "node",
216107
+ target: "node18",
216108
+ supported: { "import-attributes": true },
216109
+ plugins: [
216110
+ externalEntrypointsPlugin({
216111
+ entryNames: srcEntryNames,
216112
+ outputExtension: ".cjs"
216113
+ })
216114
+ ]
216115
+ });
216116
+ } catch (error) {
216117
+ const errorMessage = error.message || "";
216118
+ const hasErrorsArray = error.errors && Array.isArray(error.errors);
216119
+ const isTLAError = errorMessage.includes('Top-level await is currently not supported with the "cjs" output format') || hasErrorsArray && error.errors.some((e) => e.text && e.text.includes('Top-level await is currently not supported with the "cjs" output format'));
216120
+ if (isTLAError) {
216121
+ console.info(`
216122
+ \u26A0\uFE0F Top-level await detected - CommonJS generation disabled`);
216123
+ console.info(` Top-level await is incompatible with CommonJS format`);
216124
+ console.info(` Building ESM-only (Node.js 14+ and modern bundlers supported)`);
216125
+ console.info(` To permanently disable CJS: remove "main" field from package.json
216126
+ `);
216127
+ options.formats.cjs = false;
216128
+ } else {
216129
+ throw error;
216130
+ }
216131
+ }
216132
+ }
215871
216133
  }
215872
216134
  }
215873
216135
  for (const umdPath of umdEntries) {
@@ -215890,8 +216152,8 @@ async function build2(cwd, save = false) {
215890
216152
  }
215891
216153
  const autoDiscoveredFiles = [];
215892
216154
  console.info(" Generating package.json...");
215893
- const cleanedPkg = await cleanPackageJSON(pkg, mainEntry, options, cwd);
215894
- const exportsResult = generateExports(entries, mainEntry, options, pkg.exports);
216155
+ const cleanedPkg = await cleanPackageJSON(pkg, mainEntry, options, cwd, distDir);
216156
+ const exportsResult = await generateExports(entries, mainEntry, options, pkg.exports, distDir, allBinEntries);
215895
216157
  cleanedPkg.exports = fixExportsForDist(exportsResult.exports);
215896
216158
  if (exportsResult.staleExports.length > 0) {
215897
216159
  console.warn(`\u26A0\uFE0F WARNING: Found ${exportsResult.staleExports.length} stale export(s) pointing to missing src/ files:`);
@@ -215992,7 +216254,10 @@ async function build2(cwd, save = false) {
215992
216254
  rootPkg2.main = `./dist/src/${mainEntry}.cjs`;
215993
216255
  }
215994
216256
  rootPkg2.module = `./dist/src/${mainEntry}.js`;
215995
- rootPkg2.types = `./dist/src/${mainEntry}.d.ts`;
216257
+ const dtsPath = Path3.join(distDir, "src", `${mainEntry}.d.ts`);
216258
+ if (await fileExists(dtsPath)) {
216259
+ rootPkg2.types = `./dist/src/${mainEntry}.d.ts`;
216260
+ }
215996
216261
  if (rootPkg2.typings && typeof rootPkg2.typings === "string") {
215997
216262
  rootPkg2.typings = rootPkg2.typings.startsWith("./dist/") ? rootPkg2.typings : "./" + Path3.join("dist", rootPkg2.typings);
215998
216263
  }
@@ -216023,6 +216288,27 @@ async function build2(cwd, save = false) {
216023
216288
  }
216024
216289
  }
216025
216290
  rootPkg2.exports = rootExports;
216291
+ if (allBinEntries.length > 0) {
216292
+ const generatedBin = {};
216293
+ for (const binEntryInfo of allBinEntries) {
216294
+ const binPath = binEntryInfo.source === "src" ? `./dist/src/bin/${binEntryInfo.name}.js` : `./dist/bin/${binEntryInfo.name}.js`;
216295
+ const fullPath = Path3.join(cwd, binPath);
216296
+ if (await fileExists(fullPath)) {
216297
+ generatedBin[binEntryInfo.name] = binPath;
216298
+ }
216299
+ }
216300
+ if (Object.keys(generatedBin).length > 0) {
216301
+ if (rootPkg2.bin) {
216302
+ if (typeof rootPkg2.bin === "string") {
216303
+ const existingName = pkg.name?.split("/").pop() || "cli";
216304
+ rootPkg2.bin = { [existingName]: rootPkg2.bin };
216305
+ }
216306
+ rootPkg2.bin = { ...rootPkg2.bin, ...generatedBin };
216307
+ } else {
216308
+ rootPkg2.bin = generatedBin;
216309
+ }
216310
+ }
216311
+ }
216026
216312
  if (rootPkg2.bin) {
216027
216313
  if (typeof rootPkg2.bin === "string") {
216028
216314
  const distPath = rootPkg2.bin.startsWith("./dist/") ? rootPkg2.bin : rootPkg2.bin.startsWith("dist/") ? "./" + rootPkg2.bin : "./" + Path3.join("dist", rootPkg2.bin);
@@ -216085,6 +216371,10 @@ async function build2(cwd, save = false) {
216085
216371
  if (save) {
216086
216372
  rootPkg = JSON.parse(await FS3.readFile(pkgPath, "utf-8"));
216087
216373
  }
216374
+ if (allBinEntries.length > 0 || fixedDistPkg.bin) {
216375
+ const runtimeBanner = generateRuntimeBanner(pkg);
216376
+ await makeFilesExecutable(fixedDistPkg, cwd, allBinEntries, runtimeBanner);
216377
+ }
216088
216378
  return { distPkg: fixedDistPkg, rootPkg };
216089
216379
  }
216090
216380
  async function publish(cwd, save = true, extraArgs = []) {