目标版本
target 选项指定输出代码的 ECMAScript 目标版本。
默认值
默认目标版本为 es2022。
CLI 用法
sh
robuild --target es2020 ./src/index.ts
robuild --target esnext ./src/index.ts1
2
2
配置文件用法
build.config.ts
ts
import { defineConfig } from 'robuild'
export default defineConfig({
entries: [
{
type: 'bundle',
input: './src/index.ts',
target: 'es2020',
},
],
})1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
可用值
es2015(ES6)es2016es2017es2018es2019es2020es2021es2022(默认)esnext
选择建议
| 目标环境 | 推荐值 |
|---|---|
| 现代浏览器 | es2020 或 es2022 |
| Node.js 18+ | es2022 |
| Node.js 16+ | es2020 |
| 旧版浏览器 | es2015 |
| 最新特性 | esnext |
TIP
选择较新的目标版本可以生成更小、更高效的代码,但需要确保运行环境支持。
与 platform 配合
target 通常与 platform 选项配合使用:
build.config.ts
ts
import { defineConfig } from 'robuild'
export default defineConfig({
entries: [
{
type: 'bundle',
input: './src/index.ts',
platform: 'node',
target: 'es2022',
},
],
})1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
