跳至主要内容

Deno 1.4 发行说明

今天我们发布了 Deno 1.4.0,这是我们迄今为止最大的功能版本。以下是一些亮点

如果您已经安装了 Deno,则可以通过运行 deno upgrade 升级到 1.4。如果您是首次安装 Deno,则可以使用下面列出的方法之一

# Using Shell (macOS and Linux):
curl -fsSL https://deno.land/x/install/install.sh | sh

# Using PowerShell (Windows):
iwr https://deno.land/x/install/install.ps1 -useb | iex

# Using Homebrew (macOS):
brew install deno

# Using Scoop (Windows):
scoop install deno

# Using Chocolatey (Windows):
choco install deno

新功能和更改

WebSocket API

此版本增加了对 Web 标准 WebSocket API 的支持,该 API 在所有现代浏览器中都可用。它可用于通过 WebSocket 协议与远程服务器通信。

这是一个关于其工作原理的简短示例

// Start the connection to the WebSocket server at echo.websocket.org
const ws = new WebSocket("ws://echo.websocket.org/");

// Register event listeners for the open, close, and message events
ws.onopen = () => {
  console.log("WebSocket ready!");

  // Send a message over the WebSocket to the server
  ws.send("Hello World!");
};
ws.onmessage = (message) => {
  // Log the message we receive:
  console.log("Received data:", message.data);

  // Close the websocket after receiving the message
  ws.close();
};
ws.onclose = () => console.log("WebSocket closed!");
ws.onerror = (err) => console.log("WebSocket error:", err.error);

// When running this the following is logged to the console:
//
// WebSocket ready!
// Received data: Hello World!
// WebSocket closed!

您可以在本地尝试一下:deno run --allow-net=echo.websocket.org https://deno.org.cn/blog/v1.4/websocket.js

此版本还从 std/ws 中移除了 websocket 连接方法。请改用 WebSocket API

deno run --watch

Deno 现在有一个集成的文件监视器,可用于在任何依赖项更改时重启脚本。

要使用它,请像往常一样运行脚本,但添加 --watch 标志。您还需要添加 --unstable 标志,因为此功能尚不稳定。

$ echo "console.log('Hello World!')" > mod.ts
$ deno run --watch --unstable mod.ts
Check file:///home/deno/mod.ts
Hello World
Watcher Process terminated! Restarting on file change...
# now run `echo "console.log('File watching works!')" > ./mod.ts` in a different terminal
Watcher File change detected! Restarting!
Check file:///home/deno/mod.ts
File watching works!
Watcher Process terminated! Restarting on file change...

watch 标志不接受要监视的目录或文件的参数。相反,它会自动确定脚本的所有本地导入,并监视这些导入。

目前,文件监视仅支持 deno run,但在未来,它也将添加到 deno test 以及可能的其他子命令中。

deno test --coverage

您现在可以使用 deno test--coverage 标志来查找未被测试覆盖的代码。启用后,这将在所有测试运行后打印每个文件的代码覆盖率摘要。您还需要添加 --unstable 标志,因为此功能尚不稳定。

$ git clone git@github.com:denosaurs/deno_brotli.git && cd deno_brotli
$ deno test --coverage --unstable
Debugger listening on ws://127.0.0.1:9229/ws/5a593019-d185-478b-a928-ebc33e5834be
Check file:///home/deno/deno_brotli/.deno.test.ts
running 2 tests
test compress ... ok (26ms)
test decompress ... ok (13ms)

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (40ms)

test coverage:
file:///home/deno/deno_brotli/mod.ts 100.000%
file:///home/deno/deno_brotli/wasm.js 100.000%

目前,唯一可用的输出格式是文本摘要。lcovjson 等其他输出格式将在未来添加。

--unstable 中更严格的类型检查

对于所有使用 --unstable 的用户,默认情况下将启用 isolatedModulesimportsNotUsedAsValues TypeScript 编译器选项。我们将在未来为所有人默认启用这些标志。这些标志在 TypeScript 编译器中启用了一些更严格的检查,这可能会导致您以前未见过的一些新错误

ERROR TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.

ERROR TS1371: This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'.

当导入或重新导出接口或类型别名时,会发生这些错误。要修复错误,请更改您的导入和重新导出以使用 import typeexport type。 示例

// Bad
import { MyType } from "./mod.ts";
export { MyType } from "./mod.ts";

// Good
import type { MyType } from "./mod.ts";
export type { MyType } from "./mod.ts";

deno info 改进

用于进行依赖分析的 deno info 工具在此更新中进行了重大改进。它现在更快,错误更少。此外,现在显示了依赖项的文件大小,从而可以非常容易地找出哪些依赖项为您的项目添加了大量代码。

a screenshot of running `deno info https://deno.land/x/brotli/mod.ts`, which prints the a module graph for the `https://deno.land/x/brotli/mod.ts` module

console.log 中的 CSS 样式

大多数现代浏览器都支持使用 CSS 样式化 console.log 消息。为了不断努力实现 Web 兼容性,Deno 现在也支持 console.log 的 CSS 样式。

要样式化消息,请将 %c 格式参数添加到您的消息中,并将要应用的样式指定为 console.log 的参数

console.log("%cStop!", "color:red;font-weight:bold");
// This will print a bold red `Stop!` to the console.

Deno 支持 CSS 属性 colorbackground-colorfont-weightfont-styletext-decoration-colortext-decoration-line。对这些属性以及自定义 rgb、hex 和 hsl 颜色的支持取决于您的终端对 ANSI 的支持。

运行 `deno run https://deno.org.cn/blo/blogg/v1.4/rainbow.js` 的屏幕截图,该命令在控制台中打印一个带有 Deno 1.4 的彩虹 查看源代码:https://deno.org.cn/blog/v1.4/rainbow.js

在此版本中,我们添加了最终规则,以使 deno lint 规则与推荐的 eslinttypescript-eslint 规则集保持一致。这意味着 deno lint 应该能够捕获 @eslint/recommended@typescript-eslint/recommended 可以捕获的所有错误。(性能高出一个数量级。)这是朝着稳定 deno lint 迈出的重要一步。

deno doc 的更新

deno dochttps://doc.deno.land 在此版本中也获得了一系列新功能和修复。增加了对 export { foo }; 语法(在声明后导出语句)的支持,并且现在支持使用相同名称重新导出多个符号。

要试用这些新功能,只需浏览 https://doc.deno.land 上的任何模块。它已经使用新版本进行了更新。

deno.land/std 中的更改

在此版本中,writeJsonwriteJsonSyncreadJsonreadJsonSync 函数已从 https://deno.land/std/fs 中移除。您可以轻松地使用以下函数替换它们

- const accounting = await readJson("accounting.json");
+ const accounting = JSON.parse(await Deno.readTextFile("accounting.json"));

- const accounting = readJsonSync("accounting.json");
+ const accounting = JSON.parse(Deno.readTextFileSync("accounting.json"));

- await writeJson("hello_world.json", { "hello": "world" });
+ await Deno.writeTextFile("hello_world.json", JSON.stringify({ "hello": "world" }));

- writeJsonSync("hello_world.json", { "hello": "world" });
+ Deno.writeTextFileSync("hello_world.json", JSON.stringify({ "hello": "world" }));

deno_core Rust API 的更改

Deno 的基础子系统 deno_core 随着我们改进 CLI 而不断发展。在 0.57.0 中,我们将 CoreIsoateEsIsolate 合并为一个名为 JsRuntime 的结构。还公开了一种更简便的创建 ops 的工具。请查看示例,了解这些 API 如何协同工作。

VS Code 扩展的更新

Deno 的 VS Code 扩展最近进行了一些重要的功能发布。以下是快速摘要

远程 URL IntelliSense

该扩展程序的一个重要新功能是 deno.land 导入的 IntelliSense。它为您提供 deno.land/x 上的模块名称、所有版本及其完整目录列表的自动完成建议。所有这些操作均在不实际下载模块源代码的情况下完成,而是由 最近对 deno.land/x 的更新 提供支持。

内联 deno lint 诊断

deno lint 现在已完全集成到扩展程序中。要启用它,只需将扩展程序中的 deno.unstabledeno.lint 设置为 true。完成此操作后,您将获得代码的内联实时诊断


完整的发行说明,包括错误修复,可以在 https://github.com/denoland/deno/releases/tag/v1.4.0 中找到。

HN 评论