You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DX: Zip only production relevant node_modules to decrease bundle size #2709
At the moment everything inside node_modules is being zipped and uploaded to s3.
If we would create a plugin for zipping the service (to not only be dependent on node.js), we could leverage npm (or at least their ls algorithm) to find a list of only the production dependencies (even with npm@3 and their flat directory structure). npm ls --production --parseable creates exactly this - a list of all dependencies and their transient dependencies, that are declared through the package.json's dependencies (excluding devDependencies).
Reacted by Deniz Adrian, Philipp Muens, Ken Snyder, Paul Mölders, Vladyslav Holubiev, Andrew Shini, Christian Saiki, Phillip Isaacs, Christopher Svanefalk, Gustavo Henke and 6 more
changed the title [-]Zip only production relevant node_modules to decrease bundle size[/-][+]DX: Zip only production relevant node_modules to decrease bundle size[/+]on Mar 9, 2017
@pmuens yeah we can start with root package.json and recursively traverse down to each depedancy's package.json copying the required prod dependancies folders over
I believe this is how npm is actually doing it with that npm ls --prod=true --parseable=true command
@pmuens very useful work, however there is a bug in the code which prevents functions to be packaged individually. I added a comment in your PR to describe the issue. Thx!
@pmuens very useful work, however there is a bug in the code which prevents functions to be packaged individually. I added a comment in your PR to describe the issue. Thx!
@cjelger thanks for looking into it and providing feedback 👍 I'll look into the issue you've described ASAP.
As commented in #3737, there is an issue if functions are packaged individually, and if functions are developed as separate nodejs packages, with each of these packages having its own package.json file. In this case the package.json of the main/service parent package is not relevant when packaging each function: the files to be included are defined by the include property of each function, and I think the only solution to use the right package.json file is to add a new property for that (nodejs-package-json in the example below). For example, something like:
where function1 and function2 are developed as 2 separate nodejs packages. In case nodejs-package-json is missing, the packaging code could fallback to using the package.json of the service project.
Doesn't this approach in general break the concept of microservices propagated by the Serverless framework? In my understanding a Serverless service represents a microsoervice, that is self-contained.
Putting different dependencies per function leads to a Serverless service containing multiple microservices - which is somehow contradictory to the base concept.
Nevertheless I agree to exclude the development dependencies, but have the production dependencies in common for the Serverless service.
There are plugins that solve the issue that you try to solve here already. E.g. the serverless-webpack plugin, which only deploys the used dependencies per function, if you enable packaging per function.
@HyperBrain Yes right you have a point, maybe our development model doesn't really fit with some (implicit?) assumptions, but except for this issue with devDependencies (not specific to our particular case), it works just fine. IMHO I think that assuming that the serverless service is just composed of one nodejs package introduces a new explicit restriction about the structure of the code. That's a design decision, but technically it's possible to support both (which is already the case). Note that even in the case of the single nodejs package, the code in #3737 already assumes that the package.json file is in the same directory as the serverless.yml file and this might be too restrictive in some cases. Having the possibility to define this at both the service and function level (for both under package:nodejs-package-json) would provide the maximum flexibility and would not assume any "project style".
@HyperBrain - Pragmatically, I don't think it violates anything, since the term "microservice" is a buzzword that has a lot of interpretation to it ;)
We regularly have separate functions that use different libraries even within a 'self contained system' since those functions usually do very different things. Wanting to minimize size seems reasonable.
I just pushed some changes to the PR (#3737) so that nested directories with dependencies are now supported out of the box. Feel free to give it a try.
Activity
dougmoscrop commentedon Nov 25, 2016
I wrote a plugin for this, called serverless-plugin-include-dependencies - give it a try?
pmuens commentedon Nov 26, 2016
Awesome @dougmoscrop! 🎉
Thanks for sharing!
BTW here's the link https://github.com/dougmoscrop/serverless-plugin-include-dependencies
miltador commentedon Nov 26, 2016
@dougmoscrop cool, I guess you'll want to add it to the list of plugins.
[-]Zip only production relevant node_modules to decrease bundle size[/-][+]DX: Zip only production relevant node_modules to decrease bundle size[/+]19 remaining items
DavidWells commentedon May 25, 2017
@pmuens yeah we can start with root
package.jsonand recursively traverse down to each depedancy'spackage.jsoncopying the required prod dependancies folders overI believe this is how npm is actually doing it with that
npm ls --prod=true --parseable=truecommandhttps://github.com/npm/npm/blob/6f09d6d2d915cdccfee6f423d7f14135be89e9f9/lib/ls.js
pmuens commentedon Jun 6, 2017
Quick update: We just started the implementation in #3737. Feel free to test it and provide feedback 👍
cjelger commentedon Jun 12, 2017
@pmuens very useful work, however there is a bug in the code which prevents functions to be packaged individually. I added a comment in your PR to describe the issue. Thx!
pmuens commentedon Jun 12, 2017
@cjelger thanks for looking into it and providing feedback 👍 I'll look into the issue you've described ASAP.
ProTip commentedon Jun 12, 2017
@marshall007 This was my solution. Copy package.json and node_modules into the build dir, run prune,
.
cjelger commentedon Jun 13, 2017
As commented in #3737, there is an issue if functions are packaged individually, and if functions are developed as separate nodejs packages, with each of these packages having its own
package.jsonfile. In this case thepackage.jsonof the main/service parent package is not relevant when packaging each function: the files to be included are defined by theincludeproperty of each function, and I think the only solution to use the rightpackage.jsonfile is to add a new property for that (nodejs-package-jsonin the example below). For example, something like:where function1 and function2 are developed as 2 separate nodejs packages. In case
nodejs-package-jsonis missing, the packaging code could fallback to using thepackage.jsonof the service project.HyperBrain commentedon Jun 13, 2017
Doesn't this approach in general break the concept of microservices propagated by the Serverless framework? In my understanding a Serverless service represents a microsoervice, that is self-contained.
Putting different dependencies per function leads to a Serverless service containing multiple microservices - which is somehow contradictory to the base concept.
Nevertheless I agree to exclude the development dependencies, but have the production dependencies in common for the Serverless service.
There are plugins that solve the issue that you try to solve here already. E.g. the serverless-webpack plugin, which only deploys the used dependencies per function, if you enable packaging per function.
cjelger commentedon Jun 13, 2017
@HyperBrain Yes right you have a point, maybe our development model doesn't really fit with some (implicit?) assumptions, but except for this issue with
devDependencies(not specific to our particular case), it works just fine. IMHO I think that assuming that the serverless service is just composed of one nodejs package introduces a new explicit restriction about the structure of the code. That's a design decision, but technically it's possible to support both (which is already the case). Note that even in the case of the single nodejs package, the code in #3737 already assumes that thepackage.jsonfile is in the same directory as theserverless.ymlfile and this might be too restrictive in some cases. Having the possibility to define this at both theserviceandfunctionlevel (for both underpackage:nodejs-package-json) would provide the maximum flexibility and would not assume any "project style".dougmoscrop commentedon Jun 13, 2017
@HyperBrain - Pragmatically, I don't think it violates anything, since the term "microservice" is a buzzword that has a lot of interpretation to it ;)
We regularly have separate functions that use different libraries even within a 'self contained system' since those functions usually do very different things. Wanting to minimize size seems reasonable.
pmuens commentedon Jun 16, 2017
Hey everyone! Thanks for the discussion here. 👍
I just pushed some changes to the PR (#3737) so that nested directories with dependencies are now supported out of the box. Feel free to give it a try.