buildIOSApp
UniApp iOS 离线打包纯函数,整合项目构建、配置修改和模块集成等功能。
前置依赖
依赖参数
| 参数名 | 类型 | 说明 |
|---|---|---|
deps.existsSync | FileSystem['existsSync'] | 检查文件是否存在 |
deps.readFileSync | FileSystem['readFileSync'] | 读取文件内容 |
deps.writeFileSync | FileSystem['writeFileSync'] | 写入文件内容 |
deps.copyFileSync | FileSystem['copyFileSync'] | 复制文件 |
deps.mkdirSync | FileSystem['mkdirSync'] | 创建目录 |
deps.readdirSync | FileSystem['readdirSync'] | 读取目录内容 |
deps.statSync | FileSystem['statSync'] | 获取文件状态 |
deps.cp | FileSystem['promises']['cp'] | 异步复制目录 |
deps.join | Path['join'] | 路径拼接 |
deps.dirname | Path['dirname'] | 获取目录名 |
deps.basename | Path['basename'] | 获取文件名 |
deps.exec | ChildProcess['exec'] | 执行子进程命令 |
deps.platform | string | 进程平台信息 |
deps.parse | JSON5Parser['parse'] | JSON5 解析函数 |
deps.xmlParser | XMLParserConstructor | XML 解析器构造函数 |
deps.xmlSerializer | XMLSerializerConstructor | XML 序列化器构造函数 |
deps.xpath | XPath | XPath 查询库 |
deps.setTimeout | typeof setTimeout | 超时函数 |
deps.sharp | Sharp | 图像处理库(可选,用于启动屏配置) |
环境要求
- Node.js 环境: 需要文件系统、路径、子进程等 Node.js API
- @xmldom/xmldom: XML DOM 解析库
- xpath: XPath 查询库
- json5: JSON5 解析库
- sharp: 图像处理库(可选)
bash
npm install @xmldom/xmldom xpath json5 sharp函数签名
typescript
function buildIOSApp(
options: UniAppIOSBuildOptions,
deps: BuildIOSAppDeps
): Promise<UniAppIOSBuildResult>参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
options | UniAppIOSBuildOptions | 是 | iOS 打包选项 |
deps | BuildIOSAppDeps | 是 | 依赖函数对象 |
UniAppIOSBuildOptions
| 属性 | 类型 | 必填 | 说明 |
|---|---|---|---|
uniappProjectPath | string | 是 | UniApp 项目路径 |
projectPath | string | 是 | iOS 项目路径 |
bundleId | string | 否 | iOS Bundle Identifier |
appkey | string | 否 | UniApp 应用密钥 |
configuration | 'Debug' | 'Release' | 否 | 构建配置,默认为 'Release' |
scheme | string | 否 | Xcode Scheme 名称 |
clean | boolean | 否 | 是否清理构建,默认为 false |
outputPath | string | 否 | 输出路径 |
signing | IOSSigningOptions | 否 | 签名配置 |
nativePlugins | UniAppNativePluginIOSOptions[] | 否 | 原生插件配置列表 |
返回值
| 类型 | 说明 |
|---|---|
Promise<UniAppIOSBuildResult> | 打包结果,包含成功状态、输出路径、错误信息和日志 |
工作原理
- 解析 manifest.json - 读取 UniApp 项目配置
- 同步版本配置 - 更新 project.pbxproj 中的版本号
- 修改 Info.plist - 配置 Bundle ID、AppKey 等信息
- 编译 UniApp 项目 - 构建 UniApp 资源
- 复制资源文件 - 将 UniApp 资源复制到 iOS 项目
- 集成原生插件 - 如果配置了原生插件,则集成到项目中
- 编译 iOS 项目 - 使用 xcodebuild 编译并导出 IPA
注意事项
- 需要在 macOS 环境下运行
- 需要配置 Xcode 签名信息
- 建议使用
buildIOSPlugin进行插件开发调试