egoist / ama

Ask me anything (tech related).
20 stars 0 forks source link

Should I use building tools to build my js/ts library? #3

Closed colgin closed 2 years ago

colgin commented 2 years ago

There are so many building tools for lib(eg. bili, tsup, tsdx, ncc, bunchee, rollup), what's the differences between them, when i wanna make a lib, which tool should i choose?

BTW, Should I really need a "building tool" to bundle all js files into one js file for pure js/ts library? Using transpile tool (such as tsc, babel) ONLY works in most use cases(no banner, no global var replacement, no umd output). What do you think about transpile + bundle VS transpile only

egoist commented 2 years ago

bili/tsup/tsdx/bunchee are basically rollup/esbuild wrappers with sane defaults, so you don't need to care much about configurations. it wou'd be biased for me to say just use tsup, you should try them and choose what you like, but tsup is definitely the fastest among all because it uses esbuild. It also has a unique feature that allows you to bundle type packages as well, for example I use utility types from ts-essentials a lot but I don't want the users to install a whole bundle of types.

if you feel those tools too opinionated for your use case, you can use esbuild and rollup directly.

BTW, Should I really need a "building tool" to bundle all js files into one js file for pure js/ts library? Using transpile tool (such as tsc, babel) ONLY works in most use cases(no banner, no global var replacement, no umd output). What do you think about transpile + bundle VS transpile only

Nope, I use a bundler when:

a. it's for browser b. bundle some dependencies to make the package smaller, also get a faster installation time c. bundle types d. easier to build in multiple formats at once, many node packages now ship in both cjs and esm format e. like you said, customizations like adding footer or banner, tho it's a rare use case for me

colgin commented 2 years ago

Thanks for your explanation.

b bundle some dependencies to make the package smaller, also get a faster installation time

I still don't understand this. If my lib use a bundler to bundle all things into one file, all deps specified in my lib will be installed as well when others install my lib in their own apps. So why using a bundler can get a faster installation time ad smaller package?

egoist commented 2 years ago

some packages ship both cjs and esm code, and even test files and irrelevant text files, so you can bundle what you actually used in your package to reduce the size (also thanks to tree shaking)

egoist commented 2 years ago

Vite adopted a similar strategy https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md#notes-on-dependencies

colgin commented 2 years ago

Thanks a lot, Your project and explanation are helpful for me