1.0.3 â€ĸ Published 5 months ago

import-single-ts v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

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().

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.
  • 🚀 Single dependency (enhanced-resolve) + 1 peer dependency of esbuild — It's using enhanced-resolve since it's the only package out there that supports up-to-date node resolution mechanisms like "exports" in package.json which are needed to determine which file can be executed by node itself
  • 🧩ī¸ Customizable import resolution — it exposes options that are used in both esbuild and enhanced-resolve, so you can provide things like custom conditions for conditional exports. 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.

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

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

6 months ago

1.0.0

6 months ago