UNPKG

@sanity/pkg-utils

Version:

Simple utilities for modern npm packages.

149 lines (148 loc) 5.2 kB
import path from "node:path"; import { rimraf } from "rimraf"; import { getTargetPaths, loadConfig, loadPkgWithReporting, resolveBuildContext } from "./consoleSpy.js"; import { createLogger } from "./isRecord.js"; import { createSpinner } from "./spinner.js"; import { buildTaskHandlers } from "./index.js"; function resolveBuildTasks(ctx) { const { config, pkg, target } = ctx, bundles = config?.bundles || [], tasks = [], exports = Object.entries(ctx.exports || {}).map( ([_path, exp]) => ({ _path, ...exp }) ), dtsTask = { type: "build:dts", entries: [] }, rolldownDtsTask = {}, rollupTasks = {}; function addRollupTaskEntry(format, runtime, entry) { const buildId = `${format}:${runtime}`; ctx.dts === "rolldown" && (rolldownDtsTask[buildId] ? rolldownDtsTask[buildId].entries.push(entry) : rolldownDtsTask[buildId] = { type: "rolldown:dts", buildId, entries: [entry], runtime, format, target: target[runtime] }), ctx.emitDeclarationOnly || (rollupTasks[buildId] ? rollupTasks[buildId].entries.push(entry) : rollupTasks[buildId] = { type: "build:js", buildId, entries: [entry], runtime, format, target: target[runtime] }); } if (ctx.dts === "api-extractor") { for (const exp of exports) { const importId = path.join(pkg.name, exp._path); exp.source?.endsWith(".ts") && dtsTask.entries.push({ importId, exportPath: exp._path, sourcePath: exp.source, targetPaths: getTargetPaths(pkg.type, exp) }), exp.browser?.source?.endsWith(".ts") && dtsTask.entries.push({ importId, exportPath: exp._path, sourcePath: exp.browser.source, targetPaths: getTargetPaths(pkg.type, exp.browser) }), exp.node?.source?.endsWith(".ts") && dtsTask.entries.push({ importId, exportPath: exp._path, sourcePath: exp.node.source, targetPaths: getTargetPaths(pkg.type, exp.node) }); } for (const bundle of bundles) if (bundle.source?.endsWith(".ts")) { const exportPath = (bundle.import || bundle.require).replace(/\.[mc]?js$/, ""), importId = path.join(pkg.name, exportPath); dtsTask.entries.push({ importId, exportPath, sourcePath: bundle.source, targetPaths: getTargetPaths(pkg.type, bundle) }); } dtsTask.entries.length && tasks.push(dtsTask); } for (const exp of exports) { const output = exp.require; output && addRollupTaskEntry("commonjs", ctx.runtime, { path: exp._path, source: exp.source, output }); } for (const exp of exports) { const output = exp.import; output && addRollupTaskEntry("esm", ctx.runtime, { path: exp._path, source: exp.source, output }); } for (const exp of exports) { const output = exp.browser?.require; output && addRollupTaskEntry("commonjs", "browser", { path: exp._path, source: exp.browser?.source || exp.source, output }); } for (const exp of exports) { const output = exp.browser?.import; output && addRollupTaskEntry("esm", "browser", { path: exp._path, source: exp.browser?.source || exp.source, output }); } for (const bundle of bundles) { const idx = bundles.indexOf(bundle); bundle.require && addRollupTaskEntry("commonjs", bundle.runtime || ctx.runtime, { path: `__$$bundle_cjs_${idx}$$__`, source: bundle.source, output: bundle.require }), bundle.import && addRollupTaskEntry("esm", bundle.runtime || ctx.runtime, { path: `__$$bundle_esm_${idx}$$__`, source: bundle.source, output: bundle.import }); } return tasks.push(...Object.values(rolldownDtsTask)), tasks.push(...Object.values(rollupTasks)), tasks; } async function build(options) { const { cwd, emitDeclarationOnly, strict = !1, tsconfig: tsconfigOption, clean = !1, quiet = !1 } = options, logger = createLogger(quiet), config = await loadConfig({ cwd }), pkg = await loadPkgWithReporting({ cwd, logger, strict }), tsconfig = tsconfigOption || config?.tsconfig || "tsconfig.json", ctx = await resolveBuildContext({ config, cwd, emitDeclarationOnly, logger, pkg, strict, tsconfig }); clean && (quiet || logger.log( `Deleting the \`dist\` folder: './${path.relative(cwd, ctx.distPath)}' before building...` ), await rimraf(ctx.distPath)); const buildTasks = resolveBuildTasks(ctx); for (const task of buildTasks) { const handler = buildTaskHandlers[task.type], taskName = handler.name(ctx, task), spinner = createSpinner(taskName, quiet); try { const result = await handler.exec(ctx, task).toPromise(); spinner.complete(), ctx.logger.log(), handler.complete(ctx, task, result); } catch (err) { if (spinner.error(), err instanceof Error) { const RE_CWD = new RegExp(`${cwd}`, "g"); ctx.logger.error(err.message.replace(RE_CWD, ".")), ctx.logger.log(); } handler.error(ctx, task, err), process.exit(1); } } } export { build }; //# sourceMappingURL=build.js.map