serverless-heaven/serverless-webpack

2nd level peerDependencies not packed when not used directly in the project

guillaumervls opened this issue · 3 comments

Hi,

As I understand, dependencies are packed if :

  • they are listed under dependencies in package.json

AND

  • they are part of Webpack compilation "chunks" (i.e. are imported in user's code)

What's the use of the second condition ? A dependency can be in package.json and not directly used in code : in the case of a peer-dependency of other dependencies... However if not packed these dependencies will crash...

Cheers !

PS : The solution I use for this is to import the peer-dependency in my handler.js without actually doing anything with it.

Hi @guillaumervls ,
that's indeed a valid edge case 👍 , as for a peer dependency your code does not reference it but the dependency that introduces it does. This should be fixed by analyzing the dependency tree for peer dependencies of 2nd level dependencies that must be included.

To your question: A major use case for the plugin is to let Webpack optimize the code and only bundle external modules that are actually used. This leads to much smaller packages and faster Lambda cold start times. As soon as the peer dependency bug is fixed it should be consistent again.
As a function deployment is not a library but final code, it is also valid to use node-externals whitelist parameter to bundle a complete dependency into the code and let Webpack dismiss not needed 2nd level dependencies (optimally used together with the ES6 compatible UglifyJSPlugin of Webpack 3.x).

For dependencies that are neither peers nor referenced directly, there's a feature request #217 for the next release that will allow to explicitly add a dependency.

@guillaumervls Can you check with the version from #231 if that solves the problem (i.e. you do not need to reference the unused peer dependency)?

Released with 3.1.0