ts-import

Import (compile and cache on the fly) TypeScript files dynamically with ease.

MIT 89 个版本
安装
npm install ts-import
yarn add ts-import
pnpm add ts-import
bun add ts-import
README

ts-import

Importing TypeScript files dynamically into JavaScript requires additional compilation step, which is troublesome to write for many. Popular **typescript-require** package seems to be obsolete and doesn't allow much customization. Typed alternative to https://github.com/theblacksmith/typescript-require written in TypeScript.

Table of contents

  1. Getting Started

  2. Usage

  3. Features

Getting Started

npm i ts-import

Usage

import { tsImport } from 'ts-import';

const bootstrap = async () => {
    const filePath = `/home/user/file.ts`;
    const compiled = await tsImport.compile(filePath);
};

bootstrap();

Features

  • Asynchronous - uses import over require.
  • Caches JavaScript files into directory inside node_modules/ts-import/cache (pretty much like typescript-require). Removing node_modules removes cache as well.
  • Compiler class - allows making multiple instances of compiler with different configurations and overriding default settings to all of them (i.e. logger) via static "defaults" property: Compiler.defaults = { ...customDefaults }. tsImport object is a default instance of Compiler class suitable for majority of use-cases.
  • Fast - I've benchmarked ways to compare detecting file changes with fs module and checking mtimeMs turned out to be fastest (https://jsperf.com/fs-stat-mtime-vs-mtimems).
  • Highly flexible and configurable - all tsc flags are available for customization. By default uses: --module commonjs, --target es2015, --downlevelIteration, --emitDecoratorMetadata, --experimentalDecorators, --resolveJsonModule.
  • No interference - doesn't interfere with native import, require etc. changing their behavior or impacting their performance.
  • Only 1 dependency - uses only 1 package maintained by myself (which has 0 dependencies).
版本列表
5.0.0-beta.1 2025-02-19
5.0.0-beta.0 2023-04-11
4.0.0-beta.10 2022-12-08
4.0.0-beta.9 2022-12-07
4.0.0-beta.8 2022-11-26
4.0.0-beta.7 2022-11-26
4.0.0-beta.6 2022-07-09
4.0.0-beta.5 2022-07-09
4.0.0-beta.4 2022-07-09
4.0.0-beta.3 2022-07-09
4.0.0-beta.2 2022-06-17
4.0.0-beta.1 2022-06-15
4.0.0-beta.0 2022-06-15
3.0.0-beta.4 2022-06-15
3.0.0-beta.3 2022-06-08
3.0.0-beta.2 2022-06-08
3.0.0-beta.1 2022-05-24
3.0.0-beta.0 2022-05-23
2.0.40 2021-12-08
2.0.39 2021-04-09
2.0.32 2021-03-22
2.0.31 2021-03-17
2.0.30 2021-03-17
2.0.29 2021-03-16
2.0.28 2021-01-11
2.0.27 2021-01-11
2.0.25 2021-01-08
2.0.24 2020-12-21
2.0.23 2020-12-03
2.0.22 2020-12-03
2.0.21 2020-11-25
2.0.18 2020-11-23
2.0.17 2020-11-17
2.0.16 2020-11-16
2.0.15-alpha.0 2020-10-26
2.0.12-alpha.0 2020-10-23
2.0.10-alpha.0 2020-10-23
2.0.9 2020-10-23
2.0.8-2 2020-09-10
2.0.8-1 2020-09-10
2.0.8-0 2020-09-10
2.0.8 2020-09-10
2.0.7 2020-09-10
2.0.6 2020-09-02
2.0.5 2020-09-02
2.0.4-1 2020-09-01
2.0.4-0 2020-09-01
2.0.4 2020-09-02
2.0.3-0 2020-09-01
2.0.3 2020-09-01
2.0.2 2020-09-01
2.0.1 2020-08-26
0.0.72-1 2020-08-25
0.0.72 2020-08-25
0.0.48 2020-08-25
0.0.47 2020-08-25
0.0.46 2020-08-25
0.0.45 2020-08-25
0.0.44 2020-08-25
0.0.43 2020-08-25
0.0.42 2020-08-25
0.0.41 2020-08-25
0.0.40 2020-08-25
0.0.39 2020-08-25
0.0.38 2020-08-25
0.0.34-4 2020-08-15
0.0.34-3 2020-08-15
0.0.33 2020-08-13
0.0.32-1 2020-08-13
0.0.30 2020-07-21
0.0.10 2020-07-20
0.0.9-8 2020-07-11
0.0.9-7 2020-07-10
0.0.9-6 2020-07-10
0.0.9-5 2020-07-10
0.0.9-4 2020-07-10
0.0.9-3 2020-07-10
0.0.9-2 2020-07-10
0.0.9-1 2020-07-10
0.0.9-0 2020-07-10
0.0.8 2020-07-08
0.0.7 2020-07-08
0.0.6 2020-07-08
0.0.5 2020-07-08
0.0.4 2020-07-08
0.0.3 2020-07-08
0.0.2 2020-07-08
0.0.1 2020-07-08
0.0.0 2020-07-21