Skip to content

Commit c3bcd8f

Browse files
committed
more docs
1 parent 1cc266f commit c3bcd8f

File tree

2 files changed

+140
-13
lines changed

2 files changed

+140
-13
lines changed

README.md

Lines changed: 140 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,6 @@ In Node version 20.6 and higher, this will also attach the
117117
required loaders for ESM import support. In earlier Node
118118
versions, you _must_ use `--loader=tsimp/loader` for ESM support.
119119

120-
### File Extensions, Module Resolution, etc.
121-
122-
The same rules for file extensions, module resolution, and
123-
everything else apply when using `tsimp` as when using `tsc`.
124-
125-
That is, if you're running in ESM mode, you may need to write
126-
your imports ending in `.js` even though the actual file on disk
127-
is `.ts`, because that's how TS does it.
128-
129120
## Configuration
130121

131122
Most configuration is done by looking to the nearest
@@ -158,8 +149,8 @@ will override anything else in the file. For example:
158149
}
159150
"tsimp": {
160151
"compilerOptions": {
161-
"sourceMap": true,
162-
"skipLibCheck": true
152+
"skipLibCheck": true,
153+
"strict": false
163154
}
164155
}
165156
}
@@ -168,7 +159,21 @@ will override anything else in the file. For example:
168159
Sourcemaps are always enabled when using `tsimp`, so that errors
169160
reference the approriate call sites within TypeScript code.
170161

171-
## `"module"` and `"moduleResolution"`
162+
### Config File Changes and `extends` Options
163+
164+
If the `tsconfig.json` file used by tsimp changes, then it will
165+
automatically expire its memory and disk caches, because new
166+
options can result in very different results.
167+
168+
However, while `extends` is fully supported (if `tsc` can load
169+
it, so can `tsimp`, because that's how it loads config), any
170+
extended config files will _not_ be tracked for changes or cause
171+
the cache to expire.
172+
173+
When in doubt, `tsimp --restart` will reload everything as
174+
needed.
175+
176+
### `"module"`, `"moduleResolution"`, and other must-haves
172177

173178
The ultimate resulting module style for tsimp _must_ be something
174179
intelligible by Node, without any additional bundling or
@@ -178,7 +183,37 @@ Towards that end, the `module` and `moduleResolution` settings
178183
are both hard-coded to `NodeNext` in tsimp, regardless of what is
179184
in `tsconfig.json`.
180185

181-
## Compilation Diagnostics
186+
Also, the following fields are always hard-coded by tsimp:
187+
188+
- `outDir` Because tsimp isn't a build tool, but rather a module
189+
importer, it doesn't actually write the emitted JavaScript to
190+
disk. (Ok, technically it does, but only as a cache.) So, the
191+
`outDir` is hard-coded to `.tsimp-compiled`, but this is never
192+
used.
193+
- `sourceMap` This is always set to `undefined`, because:
194+
- `inlineSourceMap` is always set to `true`. It's just much
195+
simpler and faster to have the sourcemap inline with the
196+
generated JavaScript output.
197+
- `inlineSources` is always set to `false`. There is no need to
198+
bloat the output, when the input is definitely present on disk.
199+
- `declarationMap` and `declaration` are always set to `false`,
200+
because type declarations are not relevant.
201+
- `noEmit` is always set `false`, because the entire point is to
202+
get the JavaScript code for Node to run. That said, the "emit"
203+
is fully virtual, and nothing is written to disk (except to
204+
avoid compiling the same code multiple times).
205+
206+
### File Extensions, Module Resolution, etc.
207+
208+
The same rules for file extensions, module resolution, and
209+
everything else apply when using `tsimp` as when using `tsc`.
210+
211+
That means: if you're running in ESM mode, you need to write your
212+
imports ending in `.js` even though the actual file on disk is
213+
`.ts`, because that's how TS does it when `module` is set to
214+
`"NodeNext"` and the target dialect is ESM.
215+
216+
### Compilation Diagnostics
182217

183218
Set the `TSIMP_DIAG` environment variable to control what happens
184219
when there are compilation diagnostics.
@@ -190,3 +225,95 @@ when there are compilation diagnostics.
190225
- `TSIMP_DIAG=ignore` Just transpile the code, ignoring all
191226
diagnostics. (Similar to ts-node's `TS_NODE_TRANSPILE_ONLY=1`
192227
option.)
228+
229+
You can also set `diagnostics` as a top-level field in the
230+
`tsimp` object in `tsconfig.json`. For example:
231+
232+
```json
233+
{
234+
"compilerOptions": {
235+
"rootDir": "./src",
236+
"declaration": true,
237+
"esModuleInterop": true,
238+
"forceConsistentCasingInFileNames": true,
239+
"inlineSources": true,
240+
"jsx": "react",
241+
"module": "nodenext",
242+
"moduleResolution": "nodenext",
243+
"noUncheckedIndexedAccess": true,
244+
"resolveJsonModule": true,
245+
"skipLibCheck": false,
246+
"sourceMap": false,
247+
"strict": true,
248+
"target": "es2022"
249+
}
250+
"tsimp": {
251+
"diagnostics": "error"
252+
}
253+
}
254+
```
255+
256+
## How fast is it?
257+
258+
If the daemon is running, it's very fast, even if type checking
259+
is enabled. If the daemon is running and its previously compiled
260+
the file you're running, it's _zomg extremely_ fast, like "so fast
261+
you'll think it's broken" fast, outperforming TypeScript
262+
compilers written in Rust and Go, since it literally doesn't have
263+
to do anything except check some file stats and then hand the
264+
cached results to Node. (In fact, since it caches in memory as
265+
well as to disk, it might even be _faster_ in many cases than
266+
running plain old JavaScript, if the program is large.)
267+
268+
And, this is with full type checking, which is sort of the point
269+
of using TypeScript. No matter how fast your compiler is, if
270+
you're then running `tsc --noEmit` to check your types, then it's
271+
not actually gaining much.
272+
273+
If the daemon is _not_ running, and it's a cold start with no
274+
cache, it's pretty slow, comparable with ts-node, especially if
275+
type checking is enabled.
276+
277+
An exceptionally not scientific example comparison:
278+
279+
<pre style="color:#eeeeee;background:#222222;position:relative" title="tapjs/tsimp main - tapjs/tsimp">
280+
$ time node --loader @swc-node/register/esm hello.ts
281+
(node:89220) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
282+
--import 'data:text/javascript,import { register } from &quot;node:module&quot;; import { pathToFileURL } from &quot;node:url&quot;; register(&quot;%40swc-node/register/esm&quot;, pathToFileURL(&quot;./&quot;));'
283+
(Use `node --trace-warnings ...` to show where the warning was created)
284+
hello, world
285+
286+
real 0m0.268s
287+
user 0m0.255s
288+
sys 0m0.033s
289+
290+
$ time node --import=tsx hello.ts
291+
hello, world
292+
293+
real 0m0.135s
294+
user 0m0.126s
295+
sys 0m0.020s
296+
297+
$ time node --import=./dist/esm/hooks/import.mjs hello.ts
298+
<span style="color:#00ffff">hello.ts</span>:<span style="color:#ffff00">2</span>:<span style="color:#ffff00">18</span> - <span style="color:#ff3030">error</span><span style="color:#404040"> TS2322: </span>Type 'string' is not assignable to type 'boolean'.
299+
300+
<span style="color:#222222;background:#eeeeee">2</span> const f: Foo = { bar: 'hello' }
301+
<span style="color:#222222;background:#eeeeee"> </span> <span style="color:#ff3030"> ~~~
302+
303+
</span> <span style="color:#00ffff">hello.ts</span>:<span style="color:#ffff00">1</span>:<span style="color:#ffff00">14
304+
</span> <span style="color:#222222;background:#eeeeee">1</span> type Foo = { bar: boolean }
305+
<span style="color:#222222;background:#eeeeee"> </span> <span style="color:#00ffff"> ~~~
306+
</span> The expected type comes from property 'bar' which is declared here on type 'Foo'
307+
308+
hello, world
309+
310+
real 0m0.126s
311+
user 0m0.110s
312+
sys 0m0.022s
313+
</pre>
314+
315+
## How is it so fast?
316+
317+
![meme comic "We need this to run faster" "rewrite it in rust" "rewrite it in zig" "use basic caching and work skipping" guy gets thrown out window](./faster.jpg)
318+
319+
Basic caching and work skipping.

faster.jpg

145 KB
Loading

0 commit comments

Comments
 (0)