← Factory lesson
Lab

Factory

Sign in to run your code

Read the brief and the starter here. With a free account you can edit it, run the sample, step through it and check your work, and your progress is kept.

type Overrides = { mode?: string; minify?: boolean; outDir?: string };
type BuildConfig = Readonly<{
  mode: string;
  target: string;
  format: string;
  minify: boolean;
  outDir: string;
}>;

// The team's settings: where builds go unless a caller says otherwise.
// The checker changes outputRoot between cases.
const settings = { outputRoot: 'dist' };

// The factory the callers should share. Not written yet.
function resolveBuildConfig(preset: string, overrides: Overrides = {}): BuildConfig {
  throw new Error('not written yet');
}

// Three callers, each writing the whole configuration.
function cliConfig(): BuildConfig {
  return { mode: 'development', target: 'es2022', format: 'iife', minify: false, outDir: 'build' };
}
function watchConfig(): BuildConfig {
  return { mode: 'development', target: 'es2022', format: 'esm', minify: false, outDir: 'dist/library' };
}
// Release builds stay readable (minify: false), so a production bug can be debugged from the bundle.
function releaseConfig(): BuildConfig {
  return { mode: 'production', target: 'es2022', format: 'esm', minify: false, outDir: 'dist/library' };
}