← Factory lesson
Lab

Factory

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

// The factory: the preset's rules, the defaults and the caller's overrides, in one place.
function resolveBuildConfig(preset: string, overrides: Overrides = {}): BuildConfig {
  const mode = overrides.mode ?? 'development';
  return {
    mode,
    target: 'es2022',
    format: preset === 'library' ? 'esm' : 'iife',
    minify: overrides.minify ?? mode === 'production',
    outDir: overrides.outDir ?? `dist/${preset}`,
  };
}

// Three callers, each saying only what it knows.
const cli = resolveBuildConfig('app', { outDir: 'build' });
const watch = resolveBuildConfig('library');
const release = resolveBuildConfig('library', { mode: 'production', minify: false });

const show = (name: string, config: BuildConfig) =>
  console.log(`${name}: ${config.mode} ${config.target} ${config.format} minify=${config.minify} ${config.outDir}`);
show('cli', cli);
show('watch', watch);
show('release', release);
CallerCLI No result observed yet
CallerWatch No result observed yet
CallerRelease No result observed yet
Configuration factoryresolveBuildConfig
Observed request · captured on entrySelected request → —
Waiting for a request
Rules owned here · lesson annotation

Preset → target and format

Mode → minify default

Output root → directory default

An explicit choice can replace a default.

Factory calls observed0
Observed configurationLatest observed values
ModeCaller or mode default —
TargetShared / preset rule —
FormatPreset rule —
MinifyCaller or mode default —
Output directoryCaller or output-root default —

Outlined rows: a field was supplied in the request. Values can be partial during creation or rejection; they show what was recorded.

Run or Play to inspect this scenario. Shapes name lesson roles; values need a recording.