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

import-single-ts

v1.1.5

Published

![Static Badge](https://img.shields.io/badge/version-1.1.5-green) ![NPM](https://img.shields.io/npm/l/import-single-ts) ![GitHub issues](https://img.shields.io/github/issues/antitoxic/import-single-ts) ![GitHub Sponsors](https://img.shields.io/github/spon

Downloads

48,460

Readme

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: https://github.com/nodejs/node/issues/46992