deno.com

导入二进制文件

可以使用 `import` 关键字在 JS 和 TS 文件中导入二进制文件。这使得在库中包含静态数据变得更加容易。使用此功能需要 `--unstable-raw-imports` CLI 标志。

二进制文件可以在 JS 和 TS 模块中导入。此时,您需要指定 `type: "bytes"` 导入属性。
./main.ts
import bytes from "./image.png" with { type: "bytes" };
console.log(text);
也支持动态导入。
./main.ts
const text = await import("./image.png", {
  with: { type: "bytes" },
});
console.log(bytes);
./image.png
89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52
00 00 00 01 00 00 00 01 08 02 00 00 00 90 77 53
de 00 00 00 0c 49 44 41 54 78 da 63 60 60 60 00
00 00 04 00 01 c8 ea eb f9 00 00 00 00 49 45 4e
44 ae 42 60 82

使用 Deno CLI 在本地运行此示例

deno run https://docs.deno.org.cn/examples/scripts/importing_bytes.ts/main

更多资源

您找到所需内容了吗?

隐私政策