Skip to content
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

How can I use esbuild to replace tsc when developing backend code #1519

Closed
ZYinMD opened this issue Aug 12, 2021 · 7 comments
Closed

How can I use esbuild to replace tsc when developing backend code #1519

ZYinMD opened this issue Aug 12, 2021 · 7 comments

Comments

@ZYinMD
Copy link

ZYinMD commented Aug 12, 2021

This is probably a stupid question, and probably not the intended use of esbuild, but I have a very simple goal:

  1. Build an express server, write my code in src, which includes many .ts files and sub folders.
  2. Mirror src into dist, turning all .ts files into .js, but retaining folder structures and filenames, without any bundling.
  3. Turn on watch mode during development, run the express server from the dist folder.

This is exactly what tsc does, but I want to use esbuild instead of tsc for speed. I might run tsc --noEmit once before deploy, but that's all.

Since this goal is very common and very simple, I wonder what the "standard recommendation" is to achieve it. I didn't find it in the docs. Thanks!

@evanw
Copy link
Owner

evanw commented Aug 12, 2021

You didn’t find it in the docs because esbuild doesn’t do this. It’s a bundler, not a tsc replacement. It’s probably possible to use esbuild to build someone like a tsc replacement but it’s not built in.

@gozmanyoni
Copy link

You can use estrella - https://github.com/rsms/estrella, which uses esbuild and provides the functionality you require.

Or build your own FS listener that run esbuild on changed files outputting them to dist and re-running your process.

@ZYinMD
Copy link
Author

ZYinMD commented Aug 15, 2021

To people who found this issue:

I don't know why I had some previous memory that tsc --watch was slow, but the reality is it's very fast. If you're developing server code and don't need bundling, just use tsc --watch. The proposed use of esbuild in OP was really unnecessary.

@ZYinMD ZYinMD closed this as completed Aug 15, 2021
@osdevisnot
Copy link
Contributor

👋 @ZYinMD I don't think tsc --watch meets your third requirement.

3. Turn on watch mode during development, run the express server from the dist folder.

FWIW, I've been doing it this way

Etf0THvVkAIk9XA

This works with bundling enabled, unless no-bundle is a hard requirement.

@gozmanyoni
Copy link

To people who found this issue:

I don't know why I had some previous memory that tsc --watch was slow, but the reality is it's very fast. If you're developing server code and don't need bundling, just use tsc --watch. The proposed use of esbuild in OP was really unnecessary.

That is not true for large codebases.
esbuild does provide a significant benefits.

swc is another interesting option.

Offroaders123 added a commit to Offroaders123/NBTify that referenced this issue Feb 20, 2023
Added a build script to run the TypeScript compiler, rather than running it myself.

Looking into some other setups to help automate the library setup a bit more. Looking into adding a `rm -rf ./dist` (`npx rimraf ./dist`) call before `npx tsc`, since sometimes some ghost files are there after removing old files that are no longer in the codebase (they are still in `./dist` though). But, thinking about it, I also don't want to accidentally remove files that are meant to be there, so I probably won't add it. I will have to remember to reset the `./dist` folder myself, if I removed a file from `./src`.

https://stackoverflow.com/questions/45082648/npm-package-json-os-specific-script
https://github.com/isaacs/rimraf

While looking into that, I also found an article about making your npm package into a shell script. I realized that could be a great thing for NBTify to have too! Hadn't even thought about that before. Being able to simply install NBTify globally to your machine, then running it as a command, `nbtify`, and it can manipulate NBT files directly on your file system. Then you wouldn't even need to write a JavaScript script to work with NBTify! I'm gonna go make an issue over on GitHub for that! A really cool thing to look into next.

#25
https://blog.deepgram.com/npx-script/
https://2ality.com/2022/07/nodejs-esm-shell-scripts.html#node.js-esm-modules-as-standalone-shell-scripts-on-unix
https://github.com/nodejs/modules/issues/152

Not too familiar with all of the different ways to add scripts to your npm package, so I'm looking to find out what most libraries do, since it doesn't always seem to be the same thing (`npm run dev` vs `npm start`).

https://docs.npmjs.com/cli/v9/using-npm/scripts
https://stackoverflow.com/questions/69400243/whats-the-difference-between-npm-run-dev-and-npm-run-start-in-next-js
https://stackoverflow.com/questions/51358235/difference-between-npm-start-and-npm-run-start

Relating to the previous thing, it sounds like some TypeScript libraries will run their dev server using `tsc` for the type checking, then instead use `esbuild` for the build process, since it tends to be much quicker, and it doesn't perform any type checking. I think it's because you don't need to worry about type errors during the build process, but instead during the dev process, when you're actively working on the project. I do like that crossover to make things a bit lighter to build for production.

evanw/esbuild#1519
@ianmuninio
Copy link

For AWS Lambda deployment, we tried to make the cold start faster and esbuild made it possible for us. It's true that you don't need this most of the time but not for lambda. We only use it for some of our critical lambda like custom API Gateway Lambda Authorizer.

@khokm
Copy link

khokm commented Apr 23, 2024

I want to use esbuild for transpiling not for speed in first case, but to be sure that generated code will be same - esbuild and tsc output may differ sometimes. Also i can't use bundling, because some modules are importing dynamically .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants