Skip to content

DX: Zip only production relevant node_modules to decrease bundle size #2709

Closed

Description

@ZauberNerd

This is a Feature Proposal

Description

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).

Activity

  1. dougmoscrop commented on Nov 25, 2016

    @dougmoscrop
    Contributor

    I wrote a plugin for this, called serverless-plugin-include-dependencies - give it a try?

  2. miltador commented on Nov 26, 2016

    @miltador
    Contributor

    @dougmoscrop cool, I guess you'll want to add it to the list of plugins.

  3. 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
  4. 19 remaining items

  5. DavidWells commented on May 25, 2017

    @DavidWells
    Contributor

    @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

    https://github.com/npm/npm/blob/6f09d6d2d915cdccfee6f423d7f14135be89e9f9/lib/ls.js

  6. modified the milestones: 1.16, 1.15 on Jun 5, 2017
  7. pmuens commented on Jun 6, 2017

    @pmuens
    Contributor

    Quick update: We just started the implementation in #3737. Feel free to test it and provide feedback 👍

  8. cjelger commented on Jun 12, 2017

    @cjelger

    @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!

  9. pmuens commented on Jun 12, 2017

    @pmuens
    Contributor

    @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.

  10. ProTip commented on Jun 12, 2017

    @ProTip

    @marshall007 This was my solution. Copy package.json and node_modules into the build dir, run prune, :shipit: .

  11. cjelger commented on Jun 13, 2017

    @cjelger

    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:

    service: my-service
    package:
      individually: true
      exclude:
        - ./**
    
    functions:
      function1:
        package:
          nodejs-package-json: src/package-for-function1/package.json
          include:
            - src/package-for-function1/**
          
      function2:
        package:
          nodejs-package-json: src/package-for-function2/package.json
          include:
            - src/package-for-function2/**
    

    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.

  12. HyperBrain commented on Jun 13, 2017

    @HyperBrain
    Contributor

    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.

  13. cjelger commented on Jun 13, 2017

    @cjelger

    @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".

  14. dougmoscrop commented on Jun 13, 2017

    @dougmoscrop
    Contributor

    @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.

  15. pmuens commented on Jun 16, 2017

    @pmuens
    Contributor

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions