One thing I don't like about Node is that as soon as you add one require("whatever")
you end up with 1000's of transitive dependencies which call require
on the off chance that the code might be needed.
var whatever = require('whatever');
if (probablyFalse) {
whatever.theOnlyFunctionThatIUse();
// ...but `whatever` et al require other libraries which I won't actually use
}
I want to be build a package to deploy on Google Cloud Functions (and similar apps on Lambda). My code imports @google-cloud/datastore which has many transitive dependencies, some of which have binary files, computed imports etc. I don't want to run into package size limitations or increase the time that it takes for Node to parse the code. I want to use a packaging tool that does tree shaking and compiles (most of) my code and dependencies into one file. I want to be able to specify which libraries to exclude from index.js
and provide only the necessary files under node_modules
.
Because I'm compiling Typescript and using other libraries in my build/test/package/deploy process, node_modules contains 100s-1000s of libraries, most of which aren't needed in production.
Ideally, I'd like to be able to build something that looked like:
I've created a simple demo app to show what I'm trying to do (currently I'm using FuseBox):
https://github.com/nalbion/packaged-google-function/blob/master/lib/demo.js
To exclude @google-cloud/datastore and it's transitive dependencies from my compiled demo.js I've added a filterFile:
filterFile: file => {
return !['@google-cloud/datastore'].includes(file.collection.name);
},
I'm confused by the lines in the output:
FuseBox.pkg("@google-cloud/datastore", {}, function(___scope___){
return ___scope___.entry = "src/index.js";
});
Google Cloud Functions is also confused:
TypeError: Cannot read property 'default' of null
at helloWorld (/user_code/demo.js:10:42)
For reference, the demo was working until I tried to add the datastore code:
https://github.com/nalbion/packaged-google-function/blob/no-dependencies/lib/demo.js
I suspect that filterFile is not intended for this purpose, or maybe I'm using it wrong.
Is there an equivalent in FuseBox to filter packages?
Is there a better way of doing this?
(Edit) There's a known issue with private git repos:
https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues/300
Auto deploy Google Cloud Functions from Google Cloud Source Control
You're going about doing too much work unnecessarily.
Google Cloud Functions automatically handles dependencies for you by installing them on the server with npm after you deploy (assuming the dependencies are listed in your package.json). It doesn't upload the contents of node_modules. Don't bother trying to create a materialized version of your dependencies, unless you really don't want GCF to install them from npm automatically.