deno compile
,獨立可執行檔
deno compile [--output <OUT>] <SRC>
會將腳本編譯成獨立的可執行檔。
> deno compile https://deno-docs.dev.org.tw/examples/welcome.ts
如果您略過 OUT
參數,將會推斷可執行檔檔案的名稱。
旗標
與 deno install
一樣,用於執行腳本的執行時期旗標必須在編譯時指定。這包含權限旗標。
> deno compile --allow-read --allow-net https://deno.land/std/http/file_server.ts
腳本參數 可以部分嵌入。
> deno compile --allow-read --allow-net https://deno.land/std/http/file_server.ts -p 8080
> ./file_server --help
動態匯入
預設情況下,靜態可分析的動態匯入(在 import("...")
呼叫表達式中具有字串文字的匯入)將包含在輸出中。
// calculator.ts and its dependencies will be included in the binary
const calculator = await import("./calculator.ts");
但非靜態可分析的動態匯入不會
const specifier = condition ? "./calc.ts" : "./better_calc.ts";
const calculator = await import(specifier);
若要包含非靜態可分析的動態匯入,請指定 --include <path>
旗標。
deno compile --include calc.ts --include better_calc.ts main.ts
工作執行緒
與非靜態可分析的動態匯入類似,預設情況下,工作執行緒 的程式碼不會包含在編譯的可執行檔中。您必須使用 --include <path>
旗標來包含工作執行緒程式碼。
deno compile --include worker.ts main.ts
交叉編譯
您可以透過新增 --target
CLI 旗標來編譯其他平台的二進位檔。Deno 目前支援編譯至 Windows x64、macOS x64、macOS ARM 和 Linux x64。使用 deno compile --help
來列出每個編譯目標的完整值。