@tsimports/tsimports
v0.1.4
Published
A fast and opinionated imports organizer for ECMAScript and TypeScript.
Downloads
6
Readme
tsimports
✨ A fast and opinionated imports organizer for ECMAScript and TypeScript.
[!WARNING] This project is in alpha stage. Do not use in production.
Installation
npm install -g tsimports
[!TIP] If you want to manage the version of tsimports, use
-D
instead of-g
.
Usage
Basic
Format a file and write the result to stdout
tsimports ./src/foo.ts
Format a file and write in-place
tsimports --write ./src/foo.ts
Format all TypeScript files
tsimports --write ./src/**/*.ts
Advanced
Format a stdin input
[!WARNING] tsimports infers the language from the file extension. As the standard input has no extension, we cannot infer the language. Use
--language
to assume the input is JS, JSX, TS, or TSX.
cat ./src/foo.ts | tsimports --language ts
The rule
tsimports groups and sorts import statements in the file in a fixed rule. As tsimports offers you an opinionated rule, you cannot configure any of the rule.
Groups
tsimports splits the imports into several groups in the following order:
- Built-in modules (e.g.
node:assert
,fs
, orbun
) - External modules (e.g.
react
,@testing-library/react
, orhono/jwt
) - Internal modules (e.g.
~/foo
or@/foo
, if configured in bundler or somewhere) - Parent modules (e.g.
../foo
or../../foo
) - Sibling modules (e.g.
./foo
or./foo/bar
) - Index modules (e.g.
.
,./
,./index
, or./index.js
)
Ordering
tsimports sorts imports in each group in alphabetical order (case-sensitive).
Position
tsimports collects all imports at the top of the file. Any other statements are retained at the position and tsimports doesn't modify anything about them.
Acknowledgements
tsimports is built on top of the Biome infrastructure, including the JS syntax, parser, and other utils. If you like tsimports, please consider also supporting the Biome project.