feat(build): support multi-entry output in shared package build script

This commit is contained in:
Jim Liu 宝玉
2026-05-24 21:27:28 -05:00
parent e63e21786d
commit 613bee6bb8
+23 -3
View File
@@ -9,20 +9,25 @@ import { pathToFileURL } from "node:url";
async function main() { async function main() {
const options = parseArgs(process.argv.slice(2)); const options = parseArgs(process.argv.slice(2));
const packageDir = path.resolve(options.packageDir); const packageDir = path.resolve(options.packageDir);
const entryPath = path.resolve(packageDir, options.entry);
const outDir = path.resolve(packageDir, options.outDir); const outDir = path.resolve(packageDir, options.outDir);
await fs.rm(outDir, { recursive: true, force: true }); await fs.rm(outDir, { recursive: true, force: true });
await fs.mkdir(outDir, { recursive: true }); await fs.mkdir(outDir, { recursive: true });
const bun = resolveBunRuntime(); const bun = resolveBunRuntime();
const entries = options.entries.length > 0
? options.entries
: [{ source: options.entry, name: "index" }];
for (const entry of entries) {
const entryPath = path.resolve(packageDir, entry.source);
runBuild(bun, { runBuild(bun, {
entryPath, entryPath,
external: options.external, external: options.external,
format: "esm", format: "esm",
outfile: path.join(outDir, "index.js"), outfile: path.join(outDir, `${entry.name}.js`),
}); });
const cjsOutfile = path.join(outDir, "index.cjs"); const cjsOutfile = path.join(outDir, `${entry.name}.cjs`);
runBuild(bun, { runBuild(bun, {
entryPath, entryPath,
external: options.external, external: options.external,
@@ -30,6 +35,7 @@ async function main() {
outfile: cjsOutfile, outfile: cjsOutfile,
}); });
await scrubCommonJsSourceFileUrls(cjsOutfile, packageDir); await scrubCommonJsSourceFileUrls(cjsOutfile, packageDir);
}
for (const asset of options.assets) { for (const asset of options.assets) {
const source = path.resolve(packageDir, asset.source); const source = path.resolve(packageDir, asset.source);
@@ -45,6 +51,7 @@ function parseArgs(argv) {
outDir: "dist", outDir: "dist",
external: [], external: [],
assets: [], assets: [],
entries: [],
}; };
for (let index = 0; index < argv.length; index += 1) { for (let index = 0; index < argv.length; index += 1) {
@@ -57,6 +64,10 @@ function parseArgs(argv) {
options.entry = readArgValue(argv, ++index, arg); options.entry = readArgValue(argv, ++index, arg);
continue; continue;
} }
if (arg === "--entry-out") {
options.entries.push(parseEntryOutArg(readArgValue(argv, ++index, arg)));
continue;
}
if (arg === "--out-dir") { if (arg === "--out-dir") {
options.outDir = readArgValue(argv, ++index, arg); options.outDir = readArgValue(argv, ++index, arg);
continue; continue;
@@ -79,6 +90,14 @@ function parseArgs(argv) {
return options; return options;
} }
function parseEntryOutArg(value) {
const [source, name] = value.split(":");
if (!source || !name) {
throw new Error(`Invalid --entry-out value: ${value} (expected <source>:<name>)`);
}
return { source, name };
}
function readArgValue(argv, index, flag) { function readArgValue(argv, index, flag) {
const value = argv[index]; const value = argv[index];
if (!value) { if (!value) {
@@ -155,6 +174,7 @@ function printUsage() {
Options: Options:
--package-dir <dir> Package root (default: current directory) --package-dir <dir> Package root (default: current directory)
--entry <file> Entry file relative to package root (default: src/index.ts) --entry <file> Entry file relative to package root (default: src/index.ts)
--entry-out <src:name> Add an entry that emits <name>.js / <name>.cjs (repeatable)
--out-dir <dir> Output directory relative to package root (default: dist) --out-dir <dir> Output directory relative to package root (default: dist)
--external <name> Leave a module external (can be repeated) --external <name> Leave a module external (can be repeated)
--asset <src[:dst]> Copy an asset directory/file into out-dir (can be repeated) --asset <src[:dst]> Copy an asset directory/file into out-dir (can be repeated)