ai / size-limit

Calculate the real cost to run your JS app or lib to keep good performance. Show error in pull request if the cost exceeds the limit.
MIT License
6.52k stars 1.82k forks source link

Error checking package import bundle size for node packages with size-limit-preset-node-lib #306

Open slavik-chapelskyi opened 1 year ago

slavik-chapelskyi commented 1 year ago

I have tried to implement this tool in our monorepo containing only node specific packages or both browser/node compatible.

I'm facing issues using @size-limit/preset-small-lib preset of a node library, which is using node-fetch package. After running check the bundle was surprisingly small. After investigation I have figured out that whole node-fetch package was replace with some small esbuild global.fetch polyfill. That probably because it was build for web, not node platform. So I have switched preset to https://github.com/un-ts/size-limit/tree/main/packages/preset-node-lib and try to run bundle check with it. But it refuses to working with import option. I have also tried to change the CLI --save-bundler option to check the output without import option, but it had the same error, so its impossible to properly check the bundle size for node packages.

CleanShot 2022-12-13 at 18 16 01@2x CleanShot 2022-12-13 at 18 16 59@2x
ai commented 1 year ago

After investigation I have figured out that whole node-fetch package was replace with some small esbuild global.fetch polyfill

It is expected behavior. Why it doesn’t work for you?

slavik-chapelskyi commented 1 year ago

After investigation I have figured out that whole node-fetch package was replace with some small esbuild global.fetch polyfill

It is expected behavior. Why it doesn’t work for you?

Because the package I'm checking bundle size is developed to be used in node projects only, and I'm interested in what the actual bundle size for node project not supporting fetch

ai commented 1 year ago

In my world, there is no such thing as bundle size for Node.js project since you do not need bundlers for Node.js.

What do you mean for “bundle” in Node.js and why do you care about it’s size?

slavik-chapelskyi commented 1 year ago

Our backend is build on top of AWS lambda, which have size limit of 50mb. So there is cold start problem when it comes to lamdas https://khalilstemmler.com/blogs/serverless/5-ways-to-manage-lambda-cold-starts/#Understanding-the-Cold-Start-Problem While bundle size is not primary first problem with cold starts, we are still care about reusable packages bundle size to make it each lambda as small as possible, to ensure smaller cold starts

slavik-chapelskyi commented 1 year ago

I was able to fix this by replacing size-limit-preset-node-lib with undocumented modifyEsbuildConfig option and setting esbuild platform to node.

CleanShot 2022-12-13 at 18 54 26@2x

ai commented 1 year ago

Interesting problem :-/

Seems like you need to set custom esbuild config with a target to Node.js https://esbuild.github.io/api/#target

It will disable auto-polyfills.

Try to use config option in Size Limi config.