UNPKG

beesbuild

Version:

构建工具链

189 lines (188 loc) 8.5 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var stdin_exports = {}; __export(stdin_exports, { _buildRollup: () => _buildRollup, buildRollup: () => buildRollup, writeBundles: () => writeBundles }); module.exports = __toCommonJS(stdin_exports); var import_path = __toESM(require("path")); var import_chalk_unified = __toESM(require("chalk-unified")); var import_utils = require("@beesbuild/utils"); var import_lodash_unified = require("lodash-unified"); var import_rollup = require("../../builder/rollup.cjs"); var import_stub = require("../../builder/stub.cjs"); var import_untyped = require("../../builder/untyped.cjs"); var import_variables = require("../../variables.cjs"); var import_utils2 = require("../../builder/utils.cjs"); var import_getBuildContext = require("../utils/getBuildContext.cjs"); 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 (0, import_getBuildContext.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 = (0, import_utils2.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 = `${import_chalk_unified.default.bold("Root dir:")} ${options.rootDir} ${import_chalk_unified.default.bold("Entries:")} ${options.entries.map(entry => entry == null ? void 0 : entry.input).join("\n ")} `; import_utils.consola.info(msg); return; } options.entries = entries; for (const entry of entries) { if (!entry) continue; if (typeof entry.name !== "string" && (0, import_lodash_unified.isString)(entry.input)) { let relativeInput = entry.input.startsWith(rootDir) ? import_path.default.relative(rootDir, entry.input) : import_path.default.normalize(entry.input); if (relativeInput.startsWith("./")) { relativeInput = relativeInput.slice(2); } entry.name = (0, import_utils.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 = import_path.default.resolve(options.rootDir, entry.input); entry.outDir = import_path.default.resolve(options.rootDir, entry.outDir || options.outDir); } for (const key of ["dependencies", "peerDependencies", "devDependencies"]) { options[key] = (0, import_lodash_unified.uniq)(Object.keys(pkg[key] || {}).concat(options[key])); } options.externals = (0, import_lodash_unified.uniq)([...options.dependencies, ...options.peerDependencies].concat((_b = options.externals) != null ? _b : [])); await ctx.hooks.callHook("build:before", ctx); import_utils.consola.info(import_chalk_unified.default.cyan(`${options.stub ? "Stubbing" : "Building"} ${name}`)); if (options.debug) { const msg = [`${import_chalk_unified.default.bold("Root dir:")} ${rootDir}`, `${import_chalk_unified.default.bold("Entries:")}`, `${entries.map((entry, index) => { var _a2; return ` ${index + 1}\uFF1A${(_a2 = entry.input) != null ? _a2 : ""}`; }).join("\n")}`].join("\n"); import_utils.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 => import_path.default.dirname(value)); for (const dir of dirs) { const buildPath = import_path.default.resolve(rootDir, dir); if (buildPath && buildPath !== rootDir) { await (0, import_utils.rmdir)(buildPath); import_utils.consola.success(import_chalk_unified.default.green(`Cleaning dist directory: ${buildPath}`)); } } const buildCacheDir = import_path.default.resolve(ctx.options.rootDir, import_variables.BUILD_CACHE_DIR); if (buildCacheDir) { await (0, import_utils.rmdir)(buildCacheDir); import_utils.consola.success(import_chalk_unified.default.green(`Cleaning dist directory: ${buildCacheDir}`)); } } switch (true) { case options.stub && options.runEnv === "Node": // Node 包 case options.stub && options.monorepo: // monorepo 仓库 case options.dev: await (0, import_stub.rollupStub)(ctx); return; } const { build: startBuild, watch, copy } = (_i = await (0, import_rollup.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 (0, import_stub.runBrowserRollupStub)(["dts", "cjs"], ctx); } }); } else if (startBuild) { try { await (0, import_untyped.typesBuild)(ctx); } catch (e) { import_utils.consola.error(e); const error = new Error(`Error: types \u6253\u5305\u51FA\u9519`); import_utils.consola.error(error); } finally { await startBuild(); if (copy) await copy(); } } import_utils.consola.success(import_chalk_unified.default.green(`Build succeeded for ${name}`)); await ctx.hooks.callHook("build:done", ctx); } async function buildRollup(manifests, options) { import_utils.consola.info(import_chalk_unified.default.yellow(`\u6267\u884C\u5305\u987A\u5E8F\u4E4B\u524D\uFF1A${manifests.map(value => [value.name]).join(" | ")}`)); const pkgs = (0, import_utils.upgradeManifests)(manifests); import_utils.consola.info(import_chalk_unified.default.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); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { _buildRollup, buildRollup, writeBundles });