-
Notifications
You must be signed in to change notification settings - Fork 94
Backend typescript improvements #3720
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
base: integration
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for looking into all of this!
I am 100% on board with this:
This PR would help keep the codebase in a state where switching over to the built-in Node.js support would be very easy.
And I think being able to import with .ts would make it possible to add type declarations to JS files if necessary, too, which might come in handy.
As for absolute imports, I've never personally had any issues on the backend -- the file structure is so predictable and flat (compared to the mess that is the frontend) that I don't think I've even once run into a pathing issue when moving code around.
So I'm not sure I would want to bundle and minify server.ts just for absolute imports, but I'm not against it, as long as --enable-source-maps means our Sentry issue contexts are still correct with full code (e.g. as below) and this would be the main thing I would want to check on beta after merging:
It does still feel a little bit weird to bundle and minify code that's not sent over the network, but if Sentry checks out, I can't think of any particular reasons why it should be problematic. Curious what @antsgar thinks, though!
|
@kathyavini Yeah its not necessary to bundle the server side code, and its only really there for the absolute imports. The only benefit I can think of is that it uses less space and is faster to start the server but the difference is very negligible. If we don't care about the absolute imports then |
|
@navDhammu @kathyavini I do agree it seems overkill to bundle and minify just to have the absolute imports working. That being said, I don't know of any cons to it other than I assume the build step will take a bit longer, and potential issues with debugging (which should hopefully be fixed by the source maps flag). Have you by chance looked into https://github.com/LeDDGroup/typescript-transform-paths or similar plugins? I haven't tested it so not sure if it applies to this scenario, but it seems like it could be an option. |
|
@antsgar I did come across similar plugins like that one, didn't try them out though. But I think as @kathyavini said the backend structure is mostly flat so absolute imports might not be that important. Being able to import with |
|
Ok thanks @navDhammu! We did get a chance to discuss this in tech daily today, and we're all pretty happy with relative pathing on the backend (the imports are already working as we like), so if you don't mind setting up just the |
This improves backend typescript imports in 2 ways:
1. Allows using absolute imports
In relative imports sometimes you get unreadable imports like
import controller from '../../../src/controllers/something.js'. Changing the location of an imported module means changing all the imports as well. With absolute imports it can be simplified to:import controller from '@/controllers/something.js'. This is achieved using thepathsproperty of tsconfig which allows setting up absolute imports.2. Allows importing typescript files with the .ts extension
Previously when using tsc to emit javascript code, it wasn't possible to have imports with .ts extensions. But recently in typescript 5.7 they added support for Path Rewriting. This means that its possible to use
tsextensions and tsc will rewrite them tojs, but its limited to relative imports and won't work with absolute imports.So in order in to have both of these features together (1 and 2 above), I used esbuild to bundle the output into a single file (could also use some other bundler like parcel). Esbuild has support for absolute imports and is able to resolve modules in respect to the tsconfig file, the final output is one big minified file that can be directly run using
node dist/server.js. The--enable-source-mapsallows easy debugging incase an error happens in production.Some other benefits: