UNPKG

@sanity/pkg-utils

Version:

Simple utilities for modern npm packages.

196 lines (195 loc) 6.75 kB
import path from "node:path"; import { up } from "empathic/package"; import { rimraf } from "rimraf"; import { loadConfig } from "./defaults.js"; import { buildTaskHandlers, resolveVanillaExtract, writeBundleCssExports, resolveVanillaExtractCssName } from "./index.js"; import { getTargetPaths, loadPkgWithReporting, resolveBuildContext } from "./resolveBuildContext.js"; import { createLogger, handleError } from "./handleError.js"; import { createSpinner } from "./spinner.js"; function resolveBuildTasks(ctx) { const { config, pkg, target } = ctx, bundles = config?.bundles || [], tasks = [], exports = Object.entries(ctx.exports || {}).map( ([_path, exp]) => Object.assign({}, exp, { _path }) ), 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 exp of exports) { const output = exp.node?.require; output && addRollupTaskEntry("commonjs", "node", { path: exp._path, source: exp.node?.source || exp.source, output }); } for (const exp of exports) { const output = exp.node?.import; output && addRollupTaskEntry("esm", "node", { path: exp._path, source: exp.node?.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), pkgPath = up({ cwd }); if (!pkgPath) throw new Error("no package.json found", { cause: { cwd } }); const config = await loadConfig({ cwd, pkgPath }), { parseStrictOptions } = await import("./resolveBuildContext.js").then(function(n) { return n.strict; }), strictOptions = parseStrictOptions(config?.strictOptions ?? {}), pkg = await loadPkgWithReporting({ pkgPath, logger, strict, strictOptions }), 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); } } const vanillaExtract = resolveVanillaExtract(config); vanillaExtract.compatMode && await writeBundleCssExports({ cwd, distPath: ctx.distPath, cssName: resolveVanillaExtractCssName(vanillaExtract.options, { compatMode: !0, runtime: "*" }), logger }); } async function buildAction(options) { try { await build({ cwd: process.cwd(), emitDeclarationOnly: options.emitDeclarationOnly, strict: options.strict, tsconfig: options.tsconfig, clean: options.clean, quiet: options.quiet }); } catch (err) { handleError(err); } } export { buildAction }; //# sourceMappingURL=buildAction.js.map