β‘Fast drop-in replacement for the JS-native import(..) that works with TypeScript and compiles it on-the-fly.
import-single-ts
Drop-in replacement for the JS-native import(..)
but works with TypeScript
files.
node.js
process, and you want to import .ts
file fromimport './myfile.ts'
,require('./myfile.ts')
or await import('./myfile.ts')
.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.
Make sure you've installed the esbuild
(it's a peer dependency)
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).
import()
β no learning curve, you can justawait import('./some.js')
calls withawait importSingleTs('./some.ts')
and it will just work as expected.esbuild
internally and learns from the best (vite
's &.ts
file being loaded may havenode
can run by itself, are not compiled. Instead,esbuild
esbuild
,await importSingleTs('./some.ts', { conditions: ['mycompany-dev'], alias: { a: "b" }, ... })
bun
βbun
, no need to use this package.Windows
β it's not tested on Windows
and I won't beIf this makes your work easier, consider becoming a sponsor.
.ts
file and use it as a type-safevite
'svite.config.ts
file. Beforeesbuild
I wasn't sure if there was a better way to doesbuild
node-resolve plugin.esbuild
plugin system. Sadly this reliesresolve
package whichpackage.json
"exports" at all.require.extensions
is deprecated.ts-import
β it internally usestsc
, so loading up a file is slow. Another problem of tsc
is it canenhanced-resolve
whencjs
but can be used in both CJS
and ESM
environments.mjs
can't use stack trace to figure out the directoryimportSingleTs
was called from: