beesbuild
Version:
构建工具链
157 lines (156 loc) • 6.15 kB
JavaScript
import path from "path";
import chalk from "chalk-unified";
import { consola, parseManifest, rmdir, upgradeManifests } from "@beesbuild/utils";
import { isString, uniq } from "lodash-unified";
import { rollupBuild } from "../../builder/rollup.mjs";
import { rollupStub, runBrowserRollupStub } from "../../builder/stub.mjs";
import { typesBuild } from "../../builder/untyped.mjs";
import { BUILD_CACHE_DIR } from "../../variables.mjs";
import { handleBuildFiles } from "../../builder/utils.mjs";
import { getBuildContext } from "../utils/getBuildContext.mjs";
function writeBundles(bundle, options) {
return Promise.allSettled(options.map((option) => bundle.write(option)));
}
async function _buildRollup(pkg, buildConfig) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
const ctx = await getBuildContext(buildConfig, pkg);
const options = ctx.options;
const rootDir = ctx.options.rootDir;
const name = ctx.options.name;
ctx.caches[ctx.uid] = /* @__PURE__ */ new Set();
await ctx.hooks.callHook("build:prepare", ctx);
const _entries = handleBuildFiles(ctx);
const entries = _entries.map((entry) => {
switch (true) {
case typeof entry === "string":
return { input: entry };
case (entry && typeof (entry == null ? void 0 : entry.input) === "string"):
return entry;
default:
return false;
}
}).filter(Boolean);
if (!_entries || _entries.length === 0) {
const msg = `${chalk.bold("Root dir:")} ${options.rootDir}
${chalk.bold("Entries:")}
${options.entries.map((entry) => entry == null ? void 0 : entry.input).join("\n ")}
`;
consola.info(msg);
return;
}
options.entries = entries;
for (const entry of entries) {
if (!entry) continue;
if (typeof entry.name !== "string" && isString(entry.input)) {
let relativeInput = entry.input.startsWith(rootDir) ? path.relative(rootDir, entry.input) : path.normalize(entry.input);
if (relativeInput.startsWith("./")) {
relativeInput = relativeInput.slice(2);
}
entry.name = parseManifest(relativeInput, ctx.options.rootDir).name;
}
if (!entry.input) {
throw new Error(`Missing entry input: ${entry.input}`);
}
if (!entry.builder) {
entry.builder = entry.input.endsWith("/") ? "mkdist" : "rollup";
}
entry.declaration = (_a = entry.declaration) != null ? _a : options.declaration;
entry.input = path.resolve(options.rootDir, entry.input);
entry.outDir = path.resolve(options.rootDir, entry.outDir || options.outDir);
}
for (const key of ["dependencies", "peerDependencies", "devDependencies"]) {
options[key] = uniq(Object.keys(pkg[key] || {}).concat(options[key]));
}
options.externals = uniq(
[...options.dependencies, ...options.peerDependencies].concat(
(_b = options.externals) != null ? _b : []
)
);
await ctx.hooks.callHook("build:before", ctx);
consola.info(chalk.cyan(`${options.stub ? "Stubbing" : "Building"} ${name}`));
if (options.debug) {
const msg = [
`${chalk.bold("Root dir:")} ${rootDir}`,
`${chalk.bold("Entries:")}`,
`${entries.map((entry, index) => {
var _a2;
return ` ${index + 1}\uFF1A${(_a2 = entry.input) != null ? _a2 : ""}`;
}).join("\n")}`
].join("\n");
consola.info(msg);
}
if (options.clean) {
const dirs = [
(_d = (_c = pkg == null ? void 0 : pkg.publishConfig) == null ? void 0 : _c.module) != null ? _d : pkg.module,
(_f = (_e = pkg == null ? void 0 : pkg.publishConfig) == null ? void 0 : _e.main) != null ? _f : pkg.main,
(_h = (_g = pkg == null ? void 0 : pkg.publishConfig) == null ? void 0 : _g.types) != null ? _h : pkg.types
].filter(Boolean).map((value) => path.dirname(value));
for (const dir of dirs) {
const buildPath = path.resolve(rootDir, dir);
if (buildPath && buildPath !== rootDir) {
await rmdir(buildPath);
consola.success(chalk.green(`Cleaning dist directory: ${buildPath}`));
}
}
const buildCacheDir = path.resolve(ctx.options.rootDir, BUILD_CACHE_DIR);
if (buildCacheDir) {
await rmdir(buildCacheDir);
consola.success(chalk.green(`Cleaning dist directory: ${buildCacheDir}`));
}
}
switch (true) {
case (options.stub && options.runEnv === "Node"):
// Node 包
case (options.stub && options.monorepo):
// monorepo 仓库
case options.dev:
await rollupStub(ctx);
return;
}
const { build: startBuild, watch, copy } = (_i = await rollupBuild(ctx)) != null ? _i : {};
if (options.stub && options.runEnv === "Browser" && watch) {
const watcher = await watch();
watcher == null ? void 0 : watcher.on("event", async (e) => {
if (e.code === "END" && copy) {
await runBrowserRollupStub(["dts", "cjs"], ctx);
}
});
} else if (startBuild) {
try {
await typesBuild(ctx);
} catch (e) {
consola.error(e);
const error = new Error(`Error: types \u6253\u5305\u51FA\u9519`);
consola.error(error);
} finally {
await startBuild();
if (copy) await copy();
}
}
consola.success(chalk.green(`Build succeeded for ${name}`));
await ctx.hooks.callHook("build:done", ctx);
}
async function buildRollup(manifests, options) {
consola.info(chalk.yellow(`\u6267\u884C\u5305\u987A\u5E8F\u4E4B\u524D\uFF1A${manifests.map((value) => [value.name]).join(" | ")}`));
const pkgs = upgradeManifests(manifests);
consola.info(
chalk.yellow(
`\u6267\u884C\u5305\u987A\u5E8F\uFF1A${pkgs.map((pkg) => [pkg.name, pkg.upgrades ? "\u5347" : ""]).join(" | ")}`
)
);
const upgrades = pkgs.filter((value) => value.upgrades === true);
for (const manifest of upgrades) {
if (!manifest.name) continue;
await _buildRollup(manifest, options);
}
const manifestPromises = pkgs.filter((value) => value.upgrades !== true).reduce((previousValue, manifest) => {
if (!manifest.name) return previousValue;
return previousValue.concat([_buildRollup(manifest, options)]);
}, []);
return Promise.all(manifestPromises);
}
export {
_buildRollup,
buildRollup,
writeBundles
};