feat: implement phase one iceberg build tool

This commit is contained in:
2026-10-31 00:31:06 +01:00
commit af89662161
25 changed files with 949 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
import { access } from 'node:fs/promises';
import path from 'node:path';
export interface IcebergBuildOptions { outDir?: string; minify?: boolean; sourcemap?: boolean; emptyOutDir?: boolean; }
export interface IcebergConfig { root?: string; base?: string; build?: IcebergBuildOptions; }
export interface ResolvedIcebergConfig { root: string; base: string; build: Required<IcebergBuildOptions>; }
export function defineConfig(config: IcebergConfig): IcebergConfig { return config; }
export async function loadConfig(cwd: string, inline?: IcebergConfig): Promise<ResolvedIcebergConfig> {
const configPath = path.join(cwd, 'iceberg.config.js'); let fileConfig: IcebergConfig = {};
try { await access(configPath); const loaded = await import(`${new URL(`file://${configPath.replace(/\\/g, '/')}`).href}?t=${Date.now()}`); fileConfig = loaded.default ?? loaded; }
catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
const merged = { ...fileConfig, ...inline, build: { ...fileConfig.build, ...inline?.build } };
const root = path.resolve(cwd, merged.root ?? '.');
const build = { outDir: merged.build?.outDir ?? 'dist', minify: merged.build?.minify ?? true, sourcemap: merged.build?.sourcemap ?? false, emptyOutDir: merged.build?.emptyOutDir ?? true };
if (merged.base !== undefined && typeof merged.base !== 'string') throw new Error('Iceberg config: base must be a string.');
if (typeof build.outDir !== 'string' || !build.outDir) throw new Error('Iceberg config: build.outDir must be a non-empty string.');
for (const key of ['minify', 'sourcemap', 'emptyOutDir'] as const) if (typeof build[key] !== 'boolean') throw new Error(`Iceberg config: build.${key} must be a boolean.`);
return { root, base: normalizeBase(merged.base ?? '/'), build };
}
function normalizeBase(base: string) { const normalized = base.replace(/\\/g, '/'); return `/${normalized.replace(/^\/+|\/+$/g, '')}/`.replace(/^\/\/$/, '/'); }