Skip to content

Modules, and the whole loader

Relay’s config code has grown to a level parser, a flag parser, env helpers, exit codes and a loader. This lesson splits it into modules and covers the rules TypeScript adds to JavaScript’s import and export. Then the capstone puts every M1 lesson into one loader.

A file with import or export is a module. Its top-level names are private unless exported. With moduleDetection: "force" in the tsconfig from lesson 1, every file is a module even without imports, so two files can’t clash over a top-level name.

src/config/level.ts
export const LEVELS = ["debug", "info", "warn", "error"] as const
export type LogLevel = (typeof LEVELS)[number]
export function parseLevel(raw: string): LogLevel | undefined {
return LEVELS.find((l) => l === raw.toLowerCase())
}

Prefer named exports to export default. A named export has the same name at every import, so search and rename work across the codebase.

An import can bring in values (functions, classes, constants) and types (interfaces, type aliases). Types disappear when the code runs, so a runtime that strips them has to know which imports were type-only.

verbatimModuleSyntax makes that explicit. Every import is kept exactly as written, and an import used only as a type must say so with type:

// @ts-expect-error: 'BunFile' is a type and must be imported using a type-only import
import { BunFile } from "bun"
import type { Server } from "bun" // the whole import is erased
import { file, type S3File } from "bun" // only S3File is erased
export function size(f: S3File) {
return f.size
}
export const config = file("relay.json")

Without the flag, the compiler decides which imports to drop. The result can differ between tsc and a tool that compiles one file at a time, such as Bun, esbuild or Node’s type stripping. With it, the file says what is erased, and every tool agrees.

Bun and Node.js resolve the path you write. allowImportingTsExtensions lets you write the real file name, ./level.ts, which works in both and in Deno. Directory imports (./config meaning ./config/index.ts) depend on the bundler, so spell out the file.

import.meta.main is true only in the file Bun was started with. It lets one file be both a module that tests can import and a program you can run:

src/main.ts
export function main(argv: readonly string[]): number {
console.log("relay starting with", argv)
return 0
}
if (import.meta.main) {
process.exitCode = main(process.argv.slice(2))
}

Do this in the Relay project from lesson 1.

  • Directorysrc/
    • Directoryconfig/
      • cli.ts parseFlags, parseHostPort (lesson 3)
      • env.ts readEnv, requireEnv (lesson 7)
      • exit.ts ExitCode (lesson 9)
      • file.ts parseConfigFile (lesson 10)
      • level.ts LEVELS, LogLevel, parseLevel (lesson 2)
    • config.ts loadConfig, Config, ConfigError
    • main.ts the entry point
  • tsconfig.json
  • package.json
  1. Move each lesson’s solution into its file and export what other files use.

  2. Import types with import type or an inline type. bunx tsc reports any you miss.

  3. In main.ts, call loadConfig(process.env, process.argv.slice(2)). On a ConfigError, print each problem and set process.exitCode to its exitCode.

  4. Check the whole project, then try it:

    Terminal window
    bun run check
    PORT=abc LOG_LEVEL=verbose bun src/main.ts; echo "exit $?"

    Both problems should print, and the exit code should be 78.

The loader below type-checks and ignores almost every rule. Implement them all. The comment at the top lists them, and each failing test names one. Every M1 lesson is in here: literal unions, readonly tuples, optional keys without undefined, narrowing, find instead of as, and an error that carries an exit code.

  • Every file is a module (moduleDetection: "force"). Prefer named exports.
  • With verbatimModuleSyntax, type-only imports are marked with type, and every tool erases the same imports.
  • Write real file names in imports, ./level.ts.
  • import.meta.main makes a file both importable and runnable.