2

我不喜欢 Node 的一件事是,一旦你添加一个require("whatever"),你最终会得到 1000 个传递依赖项,这require会导致可能需要代码的可能性很小。

var whatever = require('whatever');
if (probablyFalse) {
   whatever.theOnlyFunctionThatIUse(); 
   // ...but `whatever` et al require other libraries which I won't actually use
}

我想构建一个包以部署在 Google Cloud Functions(以及 Lambda 上的类似应用程序)上。我的代码导入了@google-cloud/datastore,它有很多传递依赖,其中一些有二进制文件、计算导入等。我不想遇到包大小限制或增加 Node 解析代码所需的时间. 我想使用一种打包工具来进行摇树并将我的(大部分)代码和依赖项编译到一个文件中。我希望能够指定要从哪些库中排除index.js并仅在node_modules.

因为我正在编译 Typescript 并在我的构建/测试/打包/部署过程中使用其他库,所以 node_modules 包含 100-1000 个库,其中大部分在生产中不需要。

理想情况下,我希望能够构建如下所示的东西:

  • package.json - {“main”:“index.js”,依赖项:{“@google-cloud/datastore”:“1.4.1”}}
  • index.js - 从我项目中的多个 TypeScript 文件以及我从库和传递依赖项中导入的大部分代码编译而成
  • node_modules - 所有,但仅限于 index.js 中未包含但运行应用程序所需的代码。

我创建了一个简单的演示应用程序来展示我正在尝试做的事情(目前我正在使用FuseBox):

https://github.com/nalbion/packaged-google-function/blob/master/lib/demo.js

为了从我编译的 demo.js 中排除 @google-cloud/datastore 和它的传递依赖关系,我添加了一个 filterFile:

filterFile: file => {
    return !['@google-cloud/datastore'].includes(file.collection.name);
},

我对输出中的行感到困惑:

FuseBox.pkg("@google-cloud/datastore", {}, function(___scope___){
    return ___scope___.entry = "src/index.js";
});

Google Cloud Functions 也很困惑:

TypeError: Cannot read property 'default' of null
    at helloWorld (/user_code/demo.js:10:42)

作为参考,该演示一直有效,直到我尝试添加数据存储代码:

https://github.com/nalbion/packaged-google-function/blob/no-dependencies/lib/demo.js

我怀疑 filterFile 不是为了这个目的,或者我用错了。

FuseBox 中是否有等效的过滤包?

有没有更好的方法来做到这一点?

(编辑)私人 git repos 存在一个已知问题:

https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues/300

从 Google Cloud Source Control 自动部署 Google Cloud Functions

4

1 回答 1

0

你会做太多不必要的工作。

Google Cloud Functions会在您部署后使用 npm 在服务器上安装依赖项,从而自动为您处理依赖项(假设依赖项列在您的 package.json 中)。它不会上传 node_modules 的内容。不要费心尝试创建依赖项的具体化版本,除非你真的不希望 GCF 自动从 npm 安装它们。

于 2018-07-05T02:10:35.117 回答