histoire
Version:
A fast story book powered by Vite
40 lines (39 loc) • 1.58 kB
JavaScript
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { build } from '../build/index.js';
import { resolveBuildTarget } from '../build/node/target.js';
import { closeContext, createContext } from '../context.js';
import { hasUnconfirmedCleanup } from '../runtime/cleanup.js';
import { claimOutput } from './output-ownership.js';
/** Uses existing build target/hooks with fresh root-owned context and output claim. */
export async function buildProject(root, configFile, options = {}) {
const context = await createContext({ root, configFile, mode: 'build' });
let release;
let failure;
let failed = false;
let result;
try {
if (options.outDir !== undefined)
context.config.outDir = resolve(root, options.outDir);
release = await claimOutput(context.config.outDir, 'build');
const target = resolveBuildTarget(context.config);
await build(context, { nodeEntryFile: fileURLToPath(new URL('../deploy/entry.js', import.meta.url)), middlewareMode: true });
result = { outDir: context.config.outDir, target };
}
catch (error) {
failure = error;
failed = true;
}
try {
await closeContext(context);
}
catch (cleanupError) {
// Output ownership stays claimed when resource release cannot be confirmed.
throw failed ? new AggregateError([failure, cleanupError], String(failure)) : cleanupError;
}
if (!hasUnconfirmedCleanup(failure))
release?.();
if (failed)
throw failure;
return result;
}