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

Can the bundle size be reduced? #113

Closed
mrchief opened this issue Apr 15, 2019 · 11 comments
Closed

Can the bundle size be reduced? #113

mrchief opened this issue Apr 15, 2019 · 11 comments

Comments

@mrchief
Copy link

mrchief commented Apr 15, 2019

According to bundlephobia, the core package weighs at 98kb minified+gzipped. Is there any way to reduce this bundle size?

image

lodash and winston seem to be obvious targets. lodash supports tree shaking (as well as has individual packages that are lighter). winston is a fatty package in itself with a lot of lighter & faster replacements.

I think it'd help to bring the size down especially for AWS Lambda packages which themselves can be very lightweight.

Thoughts?

@chrisradek
Copy link
Contributor

@mrchief
#99 was just merged to remove the lodash dependency. We have to be more careful about replacing winston as we would need a logger that output the same way winston does today but otherwise I think that's a fair change to make.

Longer-term we'll also be able to replace the aws-sdk with @aws-sdk/client-xray-node which should be a lot smaller, but will want to wait until that project moves out of developer preview first.

@mrchief
Copy link
Author

mrchief commented Apr 16, 2019

#99 was just merged to remove the lodash dependency.

@chrisradek This is great news!

We have to be more careful about replacing winston as we would need a logger that output the same way winston does today but otherwise I think that's a fair change to make.

Yes, I totally understand. I'm not deeply familiar with the subject, so I can't say for certain whether this would work out, but I've found pino to be very modular (in addition to being fast and lightweight). It has a big ecosystem of modules so you can pick and choose the ones you actually need.

Ultimately, I'd leave up to the maintainers to make the call but I'm happy that things are proceeding in the right direction!

@jplock
Copy link

jplock commented Oct 26, 2019

When can a release be made that moves the aws-sdk dependency to a peerDependency?

@willarmiros
Copy link
Contributor

Hi @jplock,

I explained in #187 why making aws-sdk a peer dependency is not a trivial matter. We would like to create a separate module for centralized sampling, which consumes aws-sdk, so it does not need to be imported by default. However, the way the package is currently designed makes sampling logic tightly coupled with many other parts of the X-Ray SDK, so making such a separate module is not an easy task. For that reason I cannot give a solid timeline on when such a release will be made.

@vertti
Copy link

vertti commented Nov 27, 2019

date-fns dependency is huge. It adds over 4MB to the size of my lambdas. I'm guessing you are using like 0,5% of that. date-fns claims to be modular and treeshakeable etc, but if I just use aws-xray-sdk-core as a dep in my Lambda, that 4.5MB is what I get.. :(

@willarmiros
Copy link
Contributor

Hi @vertti,
Can you explain what you mean a little further? date-fns is a tiny dependency, adding only about 3.2 kB to the SDK package, according to both the above image and bundlephobia.

@vertti
Copy link

vertti commented Dec 4, 2019

@willarmiros Sure. I would love to hear that I'm doing something silly here.

I have a bunch of lambdas that have a shared dependency. This dep is instrumented with x-ray for it's Postgresql calls.

In my common library I run:

rm -rf node_modules
npm i --production

then doing du -h node_modules gives me:

---clipped for brewity---
20K	node_modules/date-fns/sub_years
20K	node_modules/date-fns/start_of_day
20K	node_modules/date-fns/is_wednesday
20K	node_modules/date-fns/start_of_month
20K	node_modules/date-fns/end_of_minute
4,6M	node_modules/date-fns
..
--clipped for brewity--
..
12K	node_modules/emitter-listener/test
36K	node_modules/emitter-listener
64K	node_modules/semver/test
8,0K	node_modules/semver/bin
204K	node_modules/semver
72K	node_modules/pg-pool/test
116K	node_modules/pg-pool
40K	node_modules/isstream
20K	node_modules/cycle
24K	node_modules/postgres-array
8,0K	node_modules/pg-connection-string/test
36K	node_modules/pg-connection-string
59M	node_modules/

and checking where the date-fns comes from with npm ls date-fns tells me:

@kesko/k-ruoka-product-service-common@0.5.0 /home/vertti/dev/kesko/k-ruoka-product-service/common
└─┬ aws-xray-sdk-core@2.5.0
  └── date-fns@1.30.1 

But I'm guessing this is just the difference of using npm dependencies and not webpacking/treeshaking/uglifying?

@willarmiros
Copy link
Contributor

Interesting, I reproduced that just fine on my end. We shouldn't have to require our customers to use treeshaking before deploying their Lambda functions to reduce the size of a dependency - especially because we only use date-fns in our logger, which is ignored in Lambda 🙃

Since its only use could be accomplished with native JS, I'll open a PR to remove date-fns.

@khaledosman
Copy link

khaledosman commented Jan 23, 2020

Are there any updates on this? moving aws-sdk to peerDependencies to be more specific

I'd rather not use Xray at all than add the entire aws-sdk to my bundle, especially after doing all these workarounds in my serverless environment to remove it like:

  1. moving "aws-sdk" to devDependencies
  2. using serverless-webpack's includeModules
  3. using webpack node-externals plugin
  4. setting serverless-webpack to forceExclude aws-sdk
  5. setting up yarn --autoclean to remove unnecessary files from node_modules

And yet it still ends up in my package due to aws-xray-sdk-core, which makes my packaged lambdas size too big and slows down my deployments, very frustrating.

@willarmiros
Copy link
Contributor

Hi @khaledosman,
Thanks for raising this concern. The aws-sdk dependency will be removed entirely in version 3.0.0 of the SDK as stated in #157, and development on that removal is underway.

@willarmiros
Copy link
Contributor

As of 3.0.0-alpha.2, aws-sdk, winston, lodash, and date-fns have been removed from this package. We do not plan on removing any further dependencies, so I will resolve this issue.

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

No branches or pull requests

6 participants