BunJS SEA
BunJS 是一个 JavaScript 运行时,支持将脚本编译成独立的可执行文件。
¥BunJS is a JavaScript runtime with support for compiling scripts into self-contained executables.
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
此演示使用 Bun 编译器和 SheetJS 创建独立的 CLI 工具,用于解析电子表格并生成 CSV 行。
¥This demo uses the Bun compiler and SheetJS to create a standalone CLI tool for parsing spreadsheets and generating CSV rows.
强烈建议在命令行工具中使用 SheetJS 库的系统上安装 BunJS。仅当认为需要独立的二进制文件时才应考虑此解决方法。
¥It is strongly recommended to install BunJS on systems using SheetJS libraries in command-line tools. This workaround should only be considered if a standalone binary is considered desirable.
优秀的开源软件会随着用户测试和报告而不断成长。任何问题都应报告给 BunJS 项目以进行进一步诊断。
¥Great open source software grows with user tests and reports. Any issues should be reported to the BunJS project for further diagnosis.
集成详情
¥Integration Details
SheetJS BunJS 模块 可以从 BunJS 脚本导入。
¥The SheetJS BunJS module can be imported from BunJS scripts.
bun build --compile
生成一个独立的可执行文件,其中包括 BunJS 运行时、用户 JS 代码以及支持脚本和资源
¥bun build --compile
generates a standalone executable that includes the BunJS
runtime, user JS code and supporting scripts and assets
脚本要求
¥Script Requirements
专门使用 SheetJS 库和 BunJS 内置模块的脚本可以使用 BunJS 进行打包。应直接要求模块:
¥Scripts that exclusively use SheetJS libraries and BunJS built-in modules can be bundled using BunJS. The module should be required directly:
const XLSX = require("xlsx");
例如,以下脚本接受一个命令行参数,使用 SheetJS readFile
方法 [^1] 解析指定文件,使用 sheet_to_csv
[^2] 从第一个工作表生成 CSV 文本,然后打印到终端:
¥For example, the following script accepts one command line argument, parses the
specified file using the SheetJS readFile
method[^1], generates CSV text from
the first worksheet using sheet_to_csv
[^2], and prints to terminal:
const XLSX = require("xlsx");
/* process.argv[2] is the first argument to the script */
const filename = process.argv[2];
/* read file */
const wb = XLSX.readFile(filename);
/* generate CSV of first sheet */
const ws = wb.Sheets[wb.SheetNames[0]];
const csv = XLSX.utils.sheet_to_csv(ws);
/* print to terminal */
console.log(csv);
完整示例
¥Complete Example
该演示最后在以下部署中进行了测试:
¥This demo was last tested in the following deployments:
架构 | BunJS | 日期 |
---|---|---|
darwin-x64 | 1.1.10 | 2024-05-28 |
darwin-arm | 1.1.10 | 2024-05-29 |
win10-x64 | 1.1.12 | 2024-06-10 |
linux-x64 | 1.1.12 | 2024-06-09 |
linux-arm | 1.1.12 | 2024-06-10 |
-
安装或更新 BunJS.[^3]
¥Install or update BunJS.[^3]
-
下载测试文件 https://xlsx.nodejs.cn/pres.numbers:
¥Download the test file https://xlsx.nodejs.cn/pres.numbers:
curl -o pres.numbers https://xlsx.nodejs.cn/pres.numbers
-
将
sheet2csv.ts
代码块的内容 至sheet2csv.ts
保存在项目文件夹中。¥Save the contents of the
sheet2csv.ts
code block tosheet2csv.ts
in the project folder. -
安装 SheetJS 依赖:
¥Install the SheetJS dependency:
bun install https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
在 Windows 上,命令失败并出现 ENOTEMPTY
错误:
¥On Windows, the command failed with a ENOTEMPTY
error:
error: InstallFailed extracting tarball for https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
error: moving "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz" to cache dir failed
ENOTEMPTY: Directory not empty (NtSetInformationFile())
解决方法是将 xlsx@
添加到 URL 前面:
¥The workaround is to prepend xlsx@
to the URL:
bun install xlsx@https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
-
使用
bun run
测试脚本:¥Test the script with
bun run
:
bun run sheet2csv.ts pres.numbers
该脚本应显示第一张工作表中的 CSV 内容:
¥The script should display CSV contents from the first sheet:
Name,Index
Bill Clinton,42
GeorgeW Bush,43
Barack Obama,44
Donald Trump,45
Joseph Biden,46
-
编译并运行
sheet2csv
:¥Compile and run
sheet2csv
:
bun build ./sheet2csv.ts --compile --outfile sheet2csv
./sheet2csv pres.numbers
该程序应显示与脚本相同的 CSV 内容(来自步骤 2)
¥The program should display the same CSV contents as the script (from step 2)
[^1]: 见 readFile
于 "读取文件"
¥See readFile
in "Reading Files"
[^2]: 见 sheet_to_csv
于 "CSV 和文本"
¥See sheet_to_csv
in "CSV and Text"
[^3]: 有关说明,请参阅 BunJS 文档中的 "安装"。
¥See "Installation" in the BunJS documentation for instructions.