-
-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disk caching in transpile-only via new project "typescript-cached-transpile" #908
Comments
@cspotcode For |
@blakeembrey Oh, excellent! Are there any other features needed to get this merged?
|
@cspotcode So the code was removed in https://github.com/TypeStrong/ts-node/releases/tag/v8.0.0, didn't have any issues with race conditions at the time either. Specially, here's the commit that removed it in case you want to work backward: b61c745.
It used to live in
No, it should just error on bad diagnostics and we don't need to worry about anything else.
Sounds reasonable. We can just throw an exception when
It's reasonable, what I had before was checking the Base64 output looked valid. This should do the same job so feel free to revive that code. |
Re caching with transformersI wonder if we can embrace how this works with Users who want transformers will set https://github.com/cevek/ttypescript#tsconfigjson Adding disk caching to the existing in-memory cacheI see there's already an in-memory cache. Can I extend the API of |
Should caching obey the |
I'd like to see if we can support the use-case where ts-node is the only compile step a team needs. So you can use ts-node for local execution, testing, etc. And somehow ts-node also emits the result of compilation to disk, so you know that the ts-node-ed code used in testing is the same code used at runtime. You get sourcemap support that "just works", etc. It could be that the cache is portable, so you can bundle it into an npm module or docker image, and it'll be loaded on another machine at another filesystem path. Or it could be that ts-node emits compiled code. Technically this compiled code would have different paths and you'd need to enable source-map support manually so I kinda like the former better. I know there's a performance hit to load ts-node and the compiler at startup, but I think teams might be ok with that. The former solution -- portable cache -- is pretty simple. Make sure that all paths in the cache key are relative paths, relative from the cache directory to the tsconfig. That's what I implemented in typescript-cached-transpile. We could have a "--portable-cache" flag. |
I'd be interested in this feature too as we're currently running ts-node in production while migrating our JS codebase to TS gradually. Starting the server with transpile-only takes currently 3 seconds, but eventually when everything will be transpiled it would take 20 seconds. The cache would greatly reduce that time. I'd also be interested if it's possible to have the cache inside cwd so we can put a warm cache into a docker image for deployment. I know I could compile the project before putting it into a docker image, but I'd like to keep production and dev environments as close as possible. @blakeembrey @cspotcode Can I help you here in any way? |
@despairblue Portable, pre-computed cache is one of the design goals of typescript-cached-transpile Specifically: https://www.npmjs.com/package/typescript-cached-transpile#portable--pre-compiled-cache Please let me know if it does or doesn't work for you. |
Happy to support this, I think it's like how it used to work with
Possibly, the in-memory cache was mostly scoped to support source maps and the cache to read from disk. I'm not sure it'll cover what you want today.
The way this previously worked would support this. It used hashes of the content as cache keys (instead of paths). We could do that, but if we'd prefer to skip reading anything from the file system I think you're correct - we should ignore this feature and just use the cache directly. |
Ok, I'll have to familiarize myself with the code. Do you know if a
I think we'll have to read the source |
It would, good point. It would have the path in the source map for debugging.
Good point 👍 |
I've been toying around with a way to avoid loading the typescript compiler until it's needed, since this adds at least 100ms to startup time. The solution may be more complex than we want in ts-node, but I think it's do-able. The upshot is a fully populated cache completely avoids loading typescript. Typescript only uses When compiling individual The only issue is where to store the cache. We need to discover and load it before discovering the We could use Roughly, a project would look like this:
When you run a) search upwards and find The question is whether all of the above will be appreciably faster than the 100ms burned on |
I was looking at the code here and it seems like Which means, if I run |
@thetutlage I haven't look at the code in a while, but I think you're correct. In development it would probably take a long time before this becomes an issue, and then you'd need to delete the cache directory. I suppose you could do something like this to auto-clean the cache every 10 minutes:
If we receive a pull request which re-adds caching to ts-node natively, then we'll be able to merge and release pretty quickly. Just depends on if/when someone does the work. |
Yeah, I have been thinking about it lately and there are many things to consider. Lemme just share them here for brainstorming mainly Process dies while writing the fileThe behavior of nodemon or many other file watchers is to restart the node process on every change. If there are too many quick changes, I suspect the cache will get corrupted. Consider the following example:
Clear cache every x minsYes, this can be one solution. But will lead to in-consistent load times and this may or may not be a big deal. Have to think more... |
I'm pretty sure it already handles partial writes. |
@cspotcode Is that possible to make caching using |
@sgtpep yes, it should work. You can pass ts-node options in various ways, but probably the cleanest is to specify them all in your tsconfig file. Once that's set up, it should work. |
@cspotcode Unfortunately, doesn't seem to work when spawning this way: |
@sgtpep are you running in transpileOnly mode? It'll only work in transpileOnly mode. The function being called on the compiler is |
Hi, I was created similar to ts-node package, postcss-node , also started to think about the disk caching. What do you think about generic disk caching mechanism for the Eg
Then the simple usage will be like
What do you think ? |
Closing, since |
I believe typescript-cached-transpile no longer works on the latest ts-node. We had to rollback. |
Makes sense since it is unmaintained and superceded by ts-node's swc mode.
…On Thu, Jun 16, 2022, 11:36 PM Roger Qiu ***@***.***> wrote:
I believe typescript-cached-transpile
<https://www.npmjs.com/package/typescript-cached-transpile> no longer
works on the latest ts-node. We had to rollback.
—
Reply to this email directly, view it on GitHub
<#908 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAC35OFMNSMME5SBM7QDKETVPPXDDANCNFSM4JL37X4Q>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
@cspotcode, if I start to use I don't really want to maintain both a |
No new config required. |
I'm sharing this here in case anyone else is having speed issues even with transpile-only.
I wrote a new npm module, typescript-cached-transpile, which monkey-patches typescript's
transpileModule
function to use a disk cache.Here's the relevant code: https://github.com/cspotcode/personal-monorepo/blob/master/packages/typescript-cached-transpile/src/create.ts#L21-L88
It's very conservative. It doesn't look at filesystem atimes or mtimes. The cache key is computed from sha1 sums of both the configuration (typescript version, typescript-cached-transpile version, and all compiler flags) and the filename and source text. It doesn't work with transformers, and it doesn't cache when the compiler returns diagnostics, because I don't cache the diagnostics. (theoretically we could cache them as JSON pretty easily)
I tested on my team's codebase, where
ts-node --transpile-only
was adding a whopping 13 second(!) to startup. Even with the overhead of computing sha1 sums, caching reduced the overhead to about a second.I understand the reasons for not introducing an incomplete caching solution to ts-node. However, in my case, I need things to be fast, which means I already need transpile-only mode, so it makes perfect sense to use a caching solution that requires it.
If you use this, and it works well or you find bugs, please let me know.
The text was updated successfully, but these errors were encountered: