目标平台
platform 选项指定构建目标平台,影响模块解析和内置模块处理。
默认值
默认平台为 node。
CLI 用法
sh
robuild --platform node ./src/index.ts
robuild --platform browser ./src/index.ts
robuild --platform neutral ./src/index.ts1
2
3
2
3
配置文件用法
build.config.ts
ts
import { defineConfig } from 'robuild'
export default defineConfig({
entries: [
{
type: 'bundle',
input: './src/index.ts',
platform: 'browser',
},
],
})1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
可用值
node
- 适用于 Node.js 环境
- 自动将 Node.js 内置模块标记为外部
- 输出
.mjs/.cjs扩展名
browser
- 适用于浏览器环境
- 可以打包 Node.js polyfills
- 输出
.js扩展名
neutral
- 通用平台,不做特殊处理
- 适用于同时支持多环境的库
- 输出
.js扩展名
使用场景
build.config.ts
ts
import { defineConfig } from 'robuild'
export default defineConfig({
entries: [
// Node.js CLI 工具
{
type: 'bundle',
input: './src/cli.ts',
platform: 'node',
},
// 浏览器库
{
type: 'bundle',
input: './src/browser.ts',
platform: 'browser',
format: 'iife',
globalName: 'MyLib',
},
// 同构库(同时支持 Node.js 和浏览器)
{
type: 'bundle',
input: './src/index.ts',
platform: 'neutral',
format: ['esm', 'cjs'],
},
],
})1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
TIP
对于纯库项目,通常使用 neutral 平台,让最终使用者决定如何处理平台特定的依赖。
