TypeScript Cached Transpile
typescript-cached-transpile is a utility designed to accelerate TypeScript compilation in `transpileOnly` mode by monkey-patching the `typescript` compiler's `transpileModule` function to utilize a disk cache. It is currently at version 0.0.6, indicating an early development stage, and its release cadence is likely tied to the maintainer's personal monorepo updates. Its primary differentiator is its targeted optimization for `ts-node`'s `transpileOnly` mode, significantly reducing build times for repetitive compilations of unchanged files by persisting transpilation results to disk. It transparently integrates by being specified as `ts-node`'s compiler, and offers configuration via environment variables or a custom JS file for cache directory and portable cache settings. It explicitly states limitations, such as not working with transformers, diagnostics, or type-checking.
Common errors
-
Error: Cannot find module 'typescript-cached-transpile'
cause The package is not installed or not resolvable in the current environment.fixRun `npm install typescript-cached-transpile` or `yarn add typescript-cached-transpile`. -
TypeError: create is not a function (when using import { create } from 'typescript-cached-transpile')cause The package primarily uses CommonJS `module.exports` and `require` for its `create` function, not ESM named imports.fixUse CommonJS syntax: `const { create } = require('typescript-cached-transpile');` -
Builds are not getting faster even with TS_NODE_TRANSPILE_ONLY=true and TS_NODE_COMPILER=typescript-cached-transpile
cause The caching requirements are not met, causing the cache to be silently skipped.fixVerify that no transformers are used, no diagnostics are returned, sourcemaps are enabled, and the config object and source code remain consistent. Ensure you are not relying on type-checking, which this package does not accelerate.
Warnings
- gotcha Caching only works if specific requirements are met, including no transformers, no diagnostics returned, same compiler version, same filename, same source code, sourcemaps enabled, and same config object. If these are not met, caching is silently skipped, leading to no performance improvement.
- gotcha The package monkey-patches the TypeScript compiler. This can lead to unexpected behavior or conflicts with other tools that also modify TypeScript's internal functions.
- gotcha When using a portable cache, `TS_CACHED_TRANSPILE_PORTABLE=true` must be set. This changes how filenames are stored in cache keys from absolute to relative paths. Forgetting this will make pre-generated caches non-portable.
- deprecated The package is in early stages (v0.0.6) and is intended for a very specific use case with `ts-node`'s `transpileOnly` mode. Its direct integration method via `TS_NODE_COMPILER` is somewhat unconventional and might not be supported in future `ts-node` versions or other build tools.
Install
-
npm install typescript-cached-transpile -
yarn add typescript-cached-transpile -
pnpm add typescript-cached-transpile
Imports
- create
import { create } from 'typescript-cached-transpile';const { create } = require('typescript-cached-transpile'); - module.exports
export default create(/* options */);
module.exports = create(/* options */);
Quickstart
import { readFileSync } from 'fs';
import { join } from 'path';
// Create a dummy TypeScript file for demonstration
const tsContent = `
interface MyInterface {
name: string;
age: number;
}
const greeter = (person: MyInterface) => {
return `Hello, ${person.name}! You are ${person.age} years old.`;
};
console.log(greeter({ name: 'Alice', age: 30 }));
`;
// This script demonstrates how to use `typescript-cached-transpile` via ts-node
// For this to run, you'd typically save the above tsContent to a file (e.g., `src/index.ts`)
// and then execute a shell command.
console.log('To run with caching, execute in your terminal:');
console.log('\nTS_NODE_TRANSPILE_ONLY=true TS_NODE_COMPILER=typescript-cached-transpile ts-node src/index.ts');
console.log('\nOr for a custom compiler configuration:');
console.log('// ./my-cached-compiler.js');
console.log('// const { create } = require(\'typescript-cached-transpile\');');
console.log('// module.exports = create({ cacheDir: process.env.TS_CACHED_TRANSPILE_CACHE ?? \'/tmp/ts-cache\' });');
console.log('\nTS_NODE_TRANSPILE_ONLY=true TS_NODE_COMPILER=$PWD/my-cached-compiler.js TS_CACHED_TRANSPILE_CACHE=$PWD/.cache ts-node src/index.ts');
console.log('\nEnsure you have ts-node and typescript-cached-transpile installed:');
console.log('npm install -g ts-node typescript typescript-cached-transpile');