dougmoscrop / serverless-plugin-include-dependencies

MIT License
184 stars 40 forks source link

Adding dependencies of dependencies #47

Closed mpvosseller closed 4 years ago

mpvosseller commented 4 years ago

I'm new to this library and it's a bit unclear to me how dependencies of dependencies can be added.

Let's say I directly depend on a node_module called foo and that foo depends on another called bar. My code has require('foo') so this plugin adds foo as expected but it does not appear to add the bar node_module.

Is this expected? Is there a generalized solution for adding dependencies of dependencies?

Thanks.

dougmoscrop commented 4 years ago

It does add bar, but what it does it it only parses the AST for your immediate dependencies (foo), then it switches to just including the package.json dependencies (bar) of those dependencies, recursively.

So it's not as thorough of a tree-shake. This was a pragmatic choice since sometimes libraries (like foo) would do things like dynamically require() other libraries and rather than render those unusable, it errs on the side of over-including.

You can use my other plugin, serverless-plugin-common-excludes to help take some of the weight off of those naively-included transitive dependencies.

mpvosseller commented 4 years ago

@dougmoscrop OK. I think this may be more about the libraries I'm using and how they instantiate their dependencies. My code depends directly on typeorm and routing-controllers. These do properly get included. Then based on certain configuration settings (and API calls made) those libraries will load others. In my case typeorm will load mysql and routing-controllers will load koa and koa-router but these libs do not get added.

dougmoscrop commented 4 years ago

Right, dynamically included modules are problematic. If they're listed as (transitive) dependencies, it will still work, because this plugin switches to parsing package.json rather than source code after it encounters a library.

If they're not listed or listed as optional, you could add them as dependencies to your package.json and require() them (without needing to assign them to a variable) or create your own sort of shim library that just lists them as dependencies, and then exports the actual dependency in question

If you need more examples of this I'll be near a computer instead of just my phone later tonight!

mpvosseller commented 4 years ago

Got it. Thanks for the help! Closing.