npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsc-prog

v2.3.0

Published

Build your TypeScript projects programmatically.

Downloads

12,816

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.