-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
QUESTION: best way use esbuild to bundle TypeScript? #1984
Comments
This is the result of a few different things:
By far the easiest way to get the most compact code for CommonJS export is likely to just do this: // handler.ts
import {APIGatewayProxyHandler} from 'aws-lambda'
exports.handler = async (_evt, _ctx): APIGatewayProxyHandler => ({ statusCode: 200, body: 'HELLO' }) That compiles to the following code, which is even smaller than either of your outputs: // 61b :: after bundling handler.ts, using esbuild, directly
exports.handler=async(a,e)=>({statusCode:200,body:"HELLO"}); It also doesn't need any changes to esbuild. |
The |
I guess you're confused with these extra code injected into the final result. They are used to help reduce the interop with cjs and esm modules, like tslib. i.e. Both "importing esm code in cjs context" and "importing cjs code in esm context" will bring them in. Most of the typescript code is in esm (since you used "import", "export"). If you bundle them to cjs (--format=cjs), which means we have to do extra work for esm → cjs. That's why The smaller result you get (run esbuild on tsc result cjs code) is cjs → cjs, so there's no extra stuff. In addition the 61b result posted by evan is the same reason. But I think no one would write ts in cjs nowadays. |
hey, @evanw and @hyrious, thank you for your prompt response! ❤️ About evanw's suggestion, I'm avoind By the way, is there any normative orientation on these subleties on how to use |
The esbuild only does the same thing as babel to typescript that it removes type decoration then work on js directly. It doesn't (and won't) support all tsc features. You may have a look at the docs. Since the movement of native es modules (since node12) has been running for a while, IMHO, I suggest you to turn your code base to esm (i.e. |
Closing this issue because it's not tracking a problem with esbuild. |
First of all, THANK YOU! Esbuild is an amazing piece of work that greatly reduces the time required for my build pipelines! Today I found a behaviour I cannot understand, though: from the same TypeScript source file (below), the code generated by compiling with
tsc
and then bundling withesbuild
is significantly smaller than the code generated directly withesbuild
. May this be caused by some misconfiguration? What should be the preferred way to build a TypeScript code base withesbuild
?The text was updated successfully, but these errors were encountered: