5.0.2 • Published 9 months ago

@ts-tools/esm v5.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@ts-tools/esm

npm version

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 --loader @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

5.0.2

9 months ago

5.0.1

10 months ago

5.0.0

10 months ago

4.0.0

2 years ago

3.0.1

3 years ago