在生产中导入 deno 第三方

Imported deno thirdparties in production

Deno 不使用任何像 npm 这样的包管理器,它只导入带有 URL 的第三方依赖项。让我们看下面的例子:

import { Application } from "https://deno.land/x/abc@v1.0.0-rc8/mod.ts";

生产中部署的代码是否包含 https://deno.land/x/abc@v1.0.0-rc8/mod.ts 的内容,或者生产中的服务器必须向 URL 发送请求以获取第三方代码?

对于生产,deno 建议将您的依赖项保存到 git,如果您遵循该建议,那么您的服务器将不需要下载任何内容,因为它已经被缓存了。

为此,您必须设置环境变量 DENO_DIR 以指定要下载依赖项的位置。

DENO_DIR=$PWD/vendor deno cache server.ts
# DENO_DIR=$PWD/vendor deno run server.ts

使用上述命令,server.ts 的所有依赖项都将下载到您的项目中,在 vendor/ 目录中,您可以提交到 git.

然后在生产服务器上,您必须将 DENO_DIR 设置为从 vendor/ 读取而不是默认路径,可以通过发出以下命令获得:

deno info

如果您不将依赖项存储在您的版本控制系统中,那么deno 将下载一次依赖项,并将它们存储到DENO_DIR 目录中。


取自deno manual:

但是如果 URL 的主机出现故障怎么办?来源将不可用。

This, like the above, is a problem faced by any remote dependency system. Relying on external servers is convenient for development but brittle in production. Production software should always vendor its dependencies. In Node this is done by checking node_modules into source control. In Deno this is done by pointing $DENO_DIR to some project-local directory at runtime, and similarly checking that into source control:

# Download the dependencies.
DENO_DIR=./deno_dir deno cache src/deps.ts

# Make sure the variable is set for any command which invokes the cache.
DENO_DIR=./deno_dir deno test src

# Check the directory into source control.
git add -u deno_dir
git commit