导入文本文件
可以使用 `import` 关键字在 JS 和 TS 文件中导入文本文件。这使得在库中包含静态数据变得更加容易。使用此功能需要 `--unstable-raw-imports` CLI 标志。
文本文件可以在 JS 和 TS 模块中导入。此时,您需要指定 `type: "text"` 导入属性。
./main.ts
import text from "./log.txt" with { type: "text" };
console.log(text);
也支持动态导入。
./main.ts
const text = await import("./log.txt", {
with: { type: "text" },
});
console.log(text);
./log.txt
2025-07-01 08:15:12 - Program started
2025-07-01 08:16:45 - Uploading file: summary.xls
2025-07-01 08:17:33 - Error: Invalid file format (xls)
2025-07-01 08:18:01 - Uploadinf file failed: summary.xls