Skip to content

Latest commit

 

History

History
114 lines (91 loc) · 5.1 KB

README.md

File metadata and controls

114 lines (91 loc) · 5.1 KB

import-single-ts

Static Badge NPM GitHub issues GitHub Sponsors

Description

Drop-in replacement for the JS-native import(..) but works with TypeScript files.

Use-case

  1. You have a running node.js process, and you want to import .ts file from it
  2. BUT you realize you can't do import './myfile.ts', require('./myfile.ts') or await import('./myfile.ts').
  3. And you DON'T want an additional compilation step before the process starts

This is where import-single-ts comes in. It allows you do await importSingleTs('./myfile.ts') with no extra steps needed.

A common example would be defining things like configuration or setup in a type-safe (ts) environment. Think vite.config.ts, webpack.config.ts, etc.

Usage

  1. Make sure you've installed the esbuild (it's a peer dependency)

  2. import { importSingleTs } from 'import-single-ts';
    // or for cjs: const { importSingleTs } = require('import-single-ts');
    // ...
    await importSingleTs('./some.ts'); // place where you need it

    It has the same API as the native dynamic import — import()*.

    * With some optional extras (see below).

Features & Limitations

  • 🔄 Drop-in replacement for import() — no learning curve, you can just replace the dynamic await import('./some.js') calls with await importSingleTs('./some.ts') and it will just work as expected.
  • Fast — uses esbuild internally and learns from the best (vite's & esbuild plugins' source code)
  • 📐 Only compiles the minimum — The .ts file being loaded may have multiple imports, which can, in turn, have more imports at any depth. Any imported files, which node can run by itself, are not compiled. Instead, they are loaded from their original path which means they are kept in the internal node module cache and there won't be duplicate module executions if they are imported again.
  • 🚀 No dependencies + 1 peer dependency of esbuild
  • 🧩️ Customizable import resolution — it exposes options used in esbuild, so you can provide things like custom conditions for conditional exports, aliases, etc. Simply pass in a second argument like so:
    await importSingleTs('./some.ts', { conditions: ['mycompany-dev'], alias: { a: "b" }, ... })
  • 💻️ Node.js REPL is supported as well
  • ⛔️ Not intended for bun — TypeScript is supported out of the box in bun, no need to use this package.
  • ⛔️ Not intended for Windows — it's not tested on Windows and I won't be able to dedicate extra time to debug problems there.

Funding GitHub Sponsors

If this makes your work easier, consider becoming a sponsor.

Inspiration

  • I wanted to load up the exports from .ts file and use it as a type-safe config. There didn't seem to be an easy way to do that.
  • I looked into vite's internal logic that deals with loading up vite.config.ts file. Before settling on using esbuild I wasn't sure if there was a better way to do compile on-the-fly. When I saw vite's approach I was relieved that looks like the only way, and I've also adapted pieces of their config handling code
  • I also researched esbuild node-resolve plugin. This was helpful to quickly glance esbuild plugin system. Sadly this relies on the resolve package which doesn't support package.json "exports" at all.
  • I noticed that node require.extensions is deprecated. They actually recommend compiling ahead of time which is what this package does.

Prior art

  • ts-import — it internally uses tsc, so loading up a file is slow. Another problem of tsc is it can run only for a single project, so if you are in a monorepo environment, and you depend on typescript files from other projects it won't work as expected.

Possible improvements

Development notes

  • It's build as cjs but can be used in both CJS and ESM environments. That's because in mjs can't use stack trace to figure out the directory where importSingleTs was called from: nodejs/node#46992