@ts-tools/esm
v6.0.0
Published
TypeScript support for Node.js.
Downloads
8
Readme
@ts-tools/esm
TypeScript support for Node.js.
This package exposes an esm loader, adding support for running .ts
and .tsx
files directly from source, in native esm mode.
Features:
- Fast! Uses
ts.transpileModule
. Leaves type checking to other flows. - Searches for the closest
tsconfig.json
, and adjusts it as necessary for native esm execution. - Source maps work with Node's
--enable-source-maps
, and when setting breakpoints directly on.ts/.tsx
sources.
Getting started
Install the library as a dev dependency in an existing TypeScript project:
npm i @ts-tools/esm --save-dev
Usage with Node.js:
node --import @ts-tools/esm ./my-script.ts
Default Loader
@ts-tools/esm
main entrypoint exposes the default loader, which searches for the closest tsconfig.json
to the current working directory.
If found, it is loaded and adjusted for direct esm execution.
If a tsconfig.json
file is not found, the following compilerOptions
are used:
const defaultCompilerOptions: ts.CompilerOptions = {
target: ts.ScriptTarget.ES2020,
inlineSourceMap: true,
module: ts.ModuleKind.Node16,
moduleResolution: ts.ModuleResolutionKind.Node16,
jsx: ts.JsxEmit.ReactJSX,
};
Custom Loader
@ts-tools/esm/lib
(notice the /lib
) exposes a programmatic API, which can be used to create a custom loader:
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createLoader, loadTsconfig } from '@ts-tools/esm/lib';
const tsconfigPath = fileURLToPath(new URL('./my-custom-tsconfig.json', import.meta.url));
const { resolve, getFormat, transformSource } = createLoader({
compilerOptions: loadTsconfig(tsconfigPath),
cwd: dirname(tsconfigPath),
});
export { resolve, getFormat, transformSource };
License
MIT