Package detail

tsc-prog

jeremyben22.1kMIT2.3.0

Build your TypeScript projects programmatically.

typescript, tsc, compiler, programmatic

readme

tsc-prog

Build your TypeScript projects programmatically.

tsc-prog offers flexiblity and convenient options for your more complex production builds (less suited for development builds).

Getting started

npm i -D tsc-prog
yarn add -D tsc-prog

tsc-prog has no dependency. You just need typescript as a peer dependency.

You simply need to build 👷‍

Use tsc.build. Specify the basePath first, and either inherit from a tsconfig file or create a config from scratch.

const tsc = require('tsc-prog')

tsc.build({
    basePath: __dirname, // always required, used for relative paths
    configFilePath: 'tsconfig.json', // config to inherit from (optional)
    compilerOptions: {
        rootDir: 'src',
        outDir: 'dist',
        declaration: true,
        skipLibCheck: true,
    },
    include: ['src/**/*'],
    exclude: ['**/*.test.ts', '**/*.spec.ts'],
})

You can have a look at all the parameters here.

You need more access 👨‍🏭

The tsc.build function is made of the two following steps, which you can have access to :

  • Program creation with tsc.createProgramFromConfig.
  • Emitting files from program with tsc.emit.
const tsc = require('tsc-prog')

// Create the program
const program = tsc.createProgramFromConfig({
    basePath: process.cwd(),
    configFilePath: 'tsconfig.json',
})

// Do what you want with the program

// Actually compile typescript files
tsc.emit(program, { copyOtherToOutDir: true })

Addons

Clean 🧹

Helps to address this issue.

We frequently need to delete the emitted files from a previous build, so a clean option recursively removes folders and files :

tsc.build({
    basePath: __dirname,
    configFilePath: 'tsconfig.json',
    clean: ['dist'], // accepts relative paths to `basePath` or absolute paths
})

You can also directly specify common targets from your compiler options :

tsc.build({
    basePath: __dirname,
    configFilePath: 'tsconfig.json',
    clean: { outDir: true, declarationDir: true },
})
Protections

The clean option protects you against deleting the following folders :

  • the specified basePath and all its parents (up to the root folder).
  • the current working directory and all its parents (up to the root folder).
  • the rootDir path if specified in the compiler options and all its parents (up to the root folder).

Copy non-typescript files 🗂️

Helps to address this issue.

The copyOtherToOutDir option allows you to copy other files to outDir (well it says so) :

tsc.build({
    basePath: __dirname,
    configFilePath: 'tsconfig.json',
    compilerOptions: {
        outDir: 'dist', // must be set
    },
    copyOtherToOutDir: true,
    exclude: ['**/somedir'], // taken into account
})

This option is protected against overwriting files emitted by the compiler, like same name .js files (could happen).

Bundle type definitions 🛍️

Helps to address this issue.

Rollup your emitted .d.ts files into a single one with bundleDeclaration option.

tsc.build({
    basePath: __dirname,
    configFilePath: 'tsconfig.json',
    compilerOptions: {
        rootDir: 'src',
        outDir: 'dist',
        declaration: true // must be set
    },
    bundleDeclaration: {
        entryPoint: 'index.d.ts', // relative to the OUTPUT directory ('dist' here)
    },
})

Bundling options

tsc.build({
    // ...
    bundleDeclaration: {
        entryPoint: 'index.d.ts',
        fallbackOnError: false, // default: true
        globals: false // default: true
        augmentations: false // default: true
    }
})
  • fallbackOnError option is a safety mecanism that generates the original unbundled definition files if any error happens during the bundling process.

  • globals option can be switched to false to discard global declarations.

  • augmentations option can be switched to false to discard external library augmentations.

Notes on bundling 🗣️

I recommend you still check the final .d.ts output, declaration bundling being very complex, with a lot of edge cases and issues such as name conflict and handling of external libraries.

tsc-prog does its best to acknowledge every edge case. It covers ones that similar tools don't and probably vice versa. Don't hesitate to review API Extractor to see if it works better with your program.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.3.0 (2023-07-29)

Features

  • option in bundle to add extra declarations (0872e6b)

Bug Fixes

  • copy: more straightforward and reliable way to prevent copy overwriting (1cd622f)
  • stop showing warning for references to globalThis which has no declaration (f67bed7)
  • typings break with new typescript version (7f0f774)
  • update tsconfig interface (fedd99e)

2.2.1 (2020-04-19)

Bug Fixes

  • bundle: infinite loop due to circular reference (3ca2cb8)

2.2.0 (2020-04-04)

Features

  • bundle globals and external library augmentations with options to switch off (644784e)

2.1.1 (2020-02-16)

Bug Fixes

  • bundle: properly hide non exported declarations (ec1a760)

2.1.0 (2019-11-27)

Features

  • expose custom compiler host option (209470b)
  • option to bundle declaration files (35b6fd9)

Bug Fixes

  • bundle: global name conflicts are more accurately handled (fd2935b)
  • accept absolute path in bundle entrypoint (ad19633)
  • copy options takes previous exclude pattern into account (a1c8f07)
  • overwrite protection in copy option did not work without listEmittedFiles (a1f07b3)
  • throw on errors and failures instead of just logging (ac1c876)
  • use colors in logs only if the output is TTY (1b43895)

2.0.3 (2019-10-13)

Bug Fixes

  • expose interfaces (b3d550d)
  • increase pause on windows platform after clean (150813b)

2.0.2 (2019-08-15)

Bug Fixes

  • do not copy declarationDir into outDir (06de1a1)

2.0.1 (2019-08-06)

Bug Fixes

  • outDir was recursively copied into itself (4b9550c)

2.0.0 (2019-07-26)

Bug Fixes

  • use pretty compiler option for diagnostics (3c8db99)

BREAKING CHANGES

  • betterDiagnostics option has been removed

1.3.0 (2019-07-26)

Bug Fixes

  • pause on windows after cleaning to help with file handles (584c0c3)
  • show some colors in logs (06d8bbd)

Features

  • copy non typescript files to outdir (332f0f0)

1.2.2 (2019-07-22)

Bug Fixes

  • compiler list files options (pre-compile and emitted) (e882ef8)
  • protect parents of rootDir in clean option (c7131f5)

1.2.1 (2019-07-21)

Bug Fixes

  • use compiler options from tsconfig.json schema, not ts module (c651fcc)

1.2.0 (2019-07-20)

Bug Fixes

  • correctly assign compiler options with the right interfaces from ts module (5e09382)

Features

  • clean option is protected against deleting sensitive folders (cba911d)

1.1.0 (2019-07-17)

Features

  • option to recursively clean files and folders before emitting (9d406b8)

1.0.0 (2019-07-17)

Features

  • program creation, files emitting, diagnostics logging and formatting (a27dc50)