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

ts-directly

v2.1.2

Published

Let Node execute TypeScript files directly with the compiler you installed, support SWC, esbuild, sucrase, tsc

Downloads

134

Readme

TS-Directly

NPM Version Node Current GitHub Actions Workflow Status

Let Node run TS files, or add to your app to give it the ability to execute TypeScript.

  • Tiny: 5 KB + 1 dependency (9 KB) minified.
  • Does not bundle a compiler, instead uses the compiler installed in the project.
  • Transform files based on closest tsconfig.json.
  • Support baseDir & paths alias.
  • Support .cts and .mts files, as well as module: "ESNext".

[!NOTE] Directory indexes and omit file extensions are only work for require(), and does not support alias.

Redirection of *.js imports to *.ts files is supported, but TS-Directly always tries the original file first.

Supported compilers:

Why not builder:

  • TS-Directly use ESM Loader Hooks that is more efficient than builder. After transpiling the code, builder will merge chunks and write the result to files, which takes more time and is redundant for Node.

Different with ts-node, tsx ...:

  • TS-Directly is intended to be an enhancement library for CLI apps, which adapts popular compilers but doesn't bundle them, so that users can use the compiler they already have in their project instead of introducing a new one.

VS --experimental-strip-types:

  • --experimental-strip-types currently is experimental and does not support enums, namespace, alias...
  • TS-Directly exposes the transform API that useful for integration.

Usage

Since TS-Directly does not have a compiler, you need to install one of the @swc/core, esbuild, sucrase, typescript. In the vast majority of cases TS-Directly works out-of-box:

  • Projects using TypeScript usually have typescript installed.
  • Compilers from other installed packages (e.g. vite has dependency esbuild) can also be used by TS-Directly.

If multiple compilers available, the fastest will be used (see Performance).

pnpm add ts-directly

You can register ts-directly with Node options:

node --import ts-directly/register main.ts

Or register in code:

import module from "module";

// Use nullable check for compatibility with runtimes other than Node.
module.register?.("ts-directly", import.meta.url);

// TS files can be imported after registration.
await import("./file/import/ts/modules.ts");

Use the API:

declare function transform(code: string, filename: string, format?: ScriptType): Promise<LoadFnOutput>;

Transform the module from TypeScript to JavaScript using a supported compiler, the compiler options is read from closest tsconfig.json.

  • code: TypeScript code to compile.
  • filename: The filename, must have a valid JS or TS extension.
  • format: Specify the output format commonjs or module, if omitted it will be determined automatically.

Returns a promise of object with properties:

  • format: module if the output module is ESM, commonjs for CJS.
  • source: The JS code.
  • shortCircuit: always true, make the object satisfies LoadFnOutput
import { readFileSync, writeFileSync } from "fs";
import { transform } from "ts-directly";

const file = "module.ts";
const tsCode = readFileSync(file, "utf8");

const { source, format } = await transform(tsCode, file);

Configuration

You can specify the compiler by set TS_COMPILER environment variable, possible values: swc, esbuild, sucrase and tsc.

TS_COMPILER=tsc && node --import ts-directly/register main.ts

Performance

Simulate importing 1322 files, see benchmark/loader.ts.

OS: Windows11, AMD Ryzen 5 5625U, PCIe 3.0 NVMe SSD.

| No. | compiler | time | time.SD | time.ratio | filesize | filesize.ratio | |----:|---------:|------------:|---------:|-----------:|---------:|---------------:| | 0 | swc | 344.24 ms | 1.25 ms | 0.00% | 8.45 MiB | 0.00% | | 1 | esbuild | 422.70 ms | 6.73 ms | +22.79% | 8.33 MiB | -1.49% | | 2 | sucrase | 481.72 ms | 7.07 ms | +39.94% | 8.93 MiB | +5.67% | | 3 | tsc | 2,844.11 ms | 22.32 ms | +726.21% | 8.74 MiB | +3.37% |

CONTRIBUTING

Download the latest version of this project, and build it:

git clone https://github.com/Kaciras/ts-directly.git
cd ts-directly
pnpm install
pnpm run build

Then you can use the loader, or run tests:

pnpm run test

Run benchmark (file in benchmark/):

pnpm exec esbench --file <filename.ts>