Programmatic TypeScript Compiler

2.3.0 · active · verified Tue Apr 21

`tsc-prog` is a JavaScript/TypeScript library designed to programmatically build TypeScript projects. It offers a flexible API for invoking the TypeScript compiler (`tsc`) with granular control over the build process, going beyond simple CLI execution. This library is particularly suited for complex production build pipelines where custom logic, pre-build steps, or post-build steps are required. Key features include a simplified `build` function, direct access to TypeScript's `Program` creation and `emit` steps, and powerful addons. These addons address common TypeScript build pain points like cleaning output directories (`clean` option), copying non-TypeScript assets to the output directory (`copyOtherToOutDir`), and bundling type definitions into a single `.d.ts` file (`bundleDeclaration`). As of version 2.3.0, `tsc-prog` primarily operates as a CommonJS module and requires `typescript@>=4` as a peer dependency. While it doesn't specify a strict release cadence, updates appear as needed to support new TypeScript features or address build complexities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `tsc-prog.build` to compile a simple TypeScript project programmatically, including cleaning the output directory and configuring compiler options from a `tsconfig.json` file. It sets up a temporary project, compiles it, and outputs to a `dist` folder.

const tsc = require('tsc-prog');
const path = require('path');
const fs = require('fs');

const basePath = path.resolve(__dirname, 'temp_project');
const tsconfigPath = path.join(basePath, 'tsconfig.json');
const srcDir = path.join(basePath, 'src');
const outDir = path.join(basePath, 'dist');

// Ensure directories exist
fs.mkdirSync(srcDir, { recursive: true });
fs.mkdirSync(outDir, { recursive: true });

// Create a dummy tsconfig.json
fs.writeFileSync(tsconfigPath, JSON.stringify({
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true
  },
  "include": ["src/**/*"]
}, null, 2));

// Create a dummy TypeScript file
fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export function greet(name: string) { return `Hello, ${name}!`; }');
fs.writeFileSync(path.join(srcDir, 'main.ts'), 'import { greet } from "./index"; console.log(greet("World"));');

console.log('Building TypeScript project with tsc-prog...');

tsc.build({
  basePath: basePath,
  configFilePath: 'tsconfig.json',
  compilerOptions: {
    rootDir: 'src',
    outDir: 'dist',
    declaration: true,
    skipLibCheck: true,
  },
  include: ['src/**/*'],
  exclude: ['**/*.test.ts', '**/*.spec.ts'],
  clean: ['dist'], // Clean previous build artifacts
  copyOtherToOutDir: true, // Copy non-TS files if any (e.g., package.json)
});

console.log('Build complete. Check the temp_project/dist directory.');

// Cleanup (optional)
// fs.rmSync(basePath, { recursive: true, force: true });

view raw JSON →