ts-flat
v0.0.12
Published
Flattens a TypeScript project into a single output file by inlining the contents of all imported modules. Useful for creating single-file distributions, analyzing dependencies, or preparing code for environments that require a single file.
Downloads
20
Readme
TS Flat
Flattens a TypeScript project into a single output file by inlining the contents of all imported modules. Useful for creating single-file distributions, analyzing dependencies, or preparing code for environments that require a single file.
Installation
npm install -g ts-flat
# or
npm install --save-dev ts-flatUsage
CLI Commands
Single file flattening:
ts-flat src/index.ts dist/output.tsMultiple files with glob patterns:
ts-flat-dir "src/**/*.ts" "!src/**/*.test.ts" dist/Programmatic API
import { flat } from 'ts-flat';
const flattened = await flat('src/index.ts');
// Returns the flattened code as a stringHow It Works
- Parses your entry file using SWC's TypeScript parser
- Recursively follows and inlines all local imports
- Removes import statements and converts exports to regular declarations
- Orders dependencies correctly to maintain execution order
- Outputs a single concatenated file
Limitations
- TypeScript only: All imported modules must be TypeScript files (
.ts,.mts,.cts,.tsx) - Named exports only: Only handles named export declarations (not
export defaultorexport * from) - Local imports only: External package imports are not processed
- ESM imports required: Uses explicit file paths as per ESM specification (no directory imports)
License
License The MIT License Copyright (c) 2025 Ivan Zakharchanka
