> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 macOS 上为单文件 JavaScript 可执行文件进行代码签名

> 解决运行 JavaScript 可执行文件时出现的“无法打开，因为来自未识别的开发者”Gatekeeper 警告。

使用 `--compile` 标志编译你的可执行文件。

```sh theme={"theme":{"light":"github-light","dark":"dracula"}}
bun build --compile ./path/to/entry.ts --outfile myapp
```

***

列出你可用的签名身份。其中一个将作为你传递给 `codesign` 命令的签名身份。此命令需要 macOS。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
security find-identity -v -p codesigning
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
1. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX "Developer ID Application: Your Name (ZZZZZZZZZZ)"
   1 valid identities found
```

***

可选，但推荐：创建一个包含必要权限的 `entitlements.plist` 文件，以保证 JavaScript 引擎正常工作。

```xml entitlements.plist icon="file-code" theme={"theme":{"light":"github-light","dark":"dracula"}}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.cs.allow-jit</key>
    <true/>
    <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
    <true/>
    <key>com.apple.security.cs.disable-executable-page-protection</key>
    <true/>
    <key>com.apple.security.cs.allow-dyld-environment-variables</key>
    <true/>
    <key>com.apple.security.cs.disable-library-validation</key>
    <true/>
</dict>
</plist>
```

***

使用 `codesign` 命令对你的可执行文件进行签名并验证其有效性。

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
codesign --entitlements entitlements.plist -vvvv --deep --sign "XXXXXXXXXX" ./myapp --force
codesign -vvv --verify ./myapp
```

***

有关 macOS 代码签名的更多信息，请参阅 [Apple 的代码签名文档](https://developer.apple.com/documentation/security/code_signing_services)。有关使用 Bun 创建独立可执行文件的详细信息，请参阅 [Standalone Executables](/bundler/executables)。本指南要求 Bun 版本 1.2.4 或更新版本。
