Skip to content

Commit 7c6f909

Browse files
committed
feat: CLI 和配置增强
1 parent 9184154 commit 7c6f909

File tree

18 files changed

+476
-13
lines changed

18 files changed

+476
-13
lines changed

TODO.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
### 🔧 CLI 和配置增强
1010

11-
- [ ] **多格式输出支持** - 支持 `esm`, `cjs`, `iife`, `umd` 多种输出格式
12-
- [ ] **全局变量名配置** - 为 IIFE/UMD 格式指定全局变量名 (`globalName`)
13-
- [ ] **平台目标配置** - 支持 `browser`, `node`, `neutral` 平台目标
14-
- [ ] **目标环境配置** - 支持 ES 版本目标如 `es2015`, `esnext`
15-
- [ ] **外部依赖配置** - 更灵活的 `external``noExternal` 配置
16-
- [ ] **别名配置** - 模块路径别名支持 (`alias`)
17-
- [ ] **环境变量注入** - 编译时环境变量定义 (`env`, `define`)
11+
- [x] **多格式输出支持** - 支持 `esm`, `cjs`, `iife`, `umd` 多种输出格式
12+
- [x] **全局变量名配置** - 为 IIFE/UMD 格式指定全局变量名 (`globalName`)
13+
- [x] **平台目标配置** - 支持 `browser`, `node`, `neutral` 平台目标
14+
- [x] **目标环境配置** - 支持 ES 版本目标如 `es2015`, `esnext`
15+
- [x] **外部依赖配置** - 更灵活的 `external``noExternal` 配置
16+
- [x] **别名配置** - 模块路径别名支持 (`alias`)
17+
- [x] **环境变量注入** - 编译时环境变量定义 (`env`, `define`)
1818

1919
### 🏗️ 构建功能增强
2020

21-
- [ ] **清理功能** - 构建前自动清理输出目录 (`clean`)
21+
- [x] **清理功能** - 构建前自动清理输出目录 (`clean`)
2222
- [ ] **文件复制** - 静态资源复制功能 (`copy`)
2323
- [ ] **文件哈希** - 输出文件名哈希支持 (`hash`)
2424
- [ ] **固定扩展名** - 强制使用 `.cjs`/`.mjs` 扩展名 (`fixedExtension`)
@@ -74,6 +74,8 @@
7474
3. ✅ 环境变量注入 - 支持 env 和 define 配置,编译时变量替换
7575
4. ✅ 平台目标配置 - 支持 browser, node, neutral 平台目标
7676
5. ✅ 外部依赖配置增强 - 增强 external 和 noExternal 配置,支持正则表达式和函数
77+
6. ✅ 目标环境配置 - 支持 ES 版本目标 (es5, es2015-es2022, esnext)
78+
7. ✅ 别名配置 - 模块路径别名支持,支持 bundle 和 transform 模式
7779

7880
#### 实现详情
7981

src/builders/bundle.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export async function rolldownBuild(
100100
// Get configuration with defaults
101101
const formats = Array.isArray(entry.format) ? entry.format : [entry.format || 'esm']
102102
const platform = entry.platform || 'node'
103+
const target = entry.target || 'es2022'
103104
const outDir = entry.outDir || 'dist'
104105
const fullOutDir = resolve(ctx.pkgDir, outDir)
105106
const isMultiFormat = formats.length > 1
@@ -187,6 +188,12 @@ export async function rolldownBuild(
187188
? entry.external
188189
: externalDeps,
189190
define: defineOptions,
191+
resolve: {
192+
alias: entry.alias || {},
193+
},
194+
transform: {
195+
target,
196+
},
190197
} satisfies InputOptions)
191198

192199
await hooks.rolldownConfig?.(baseRolldownConfig, ctx)

src/builders/transform.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,37 @@ async function transformModule(entryPath: string, entry: TransformEntry) {
159159

160160
const magicString = new MagicString(sourceText)
161161

162-
// Rewrite relative imports
162+
// Rewrite relative imports and aliases
163163
const updatedStarts = new Set<number>()
164164
const rewriteSpecifier = (req: {
165165
value: string
166166
start: number
167167
end: number
168168
}) => {
169-
const moduleId = req.value
170-
if (!moduleId.startsWith('.')) {
169+
let moduleId = req.value
170+
let wasAliasResolved = false
171+
172+
// Handle aliases first
173+
if (entry.alias) {
174+
for (const [alias, target] of Object.entries(entry.alias)) {
175+
if (moduleId === alias || moduleId.startsWith(alias + '/')) {
176+
moduleId = moduleId.replace(alias, target)
177+
wasAliasResolved = true
178+
break
179+
}
180+
}
181+
}
182+
183+
// Skip external modules unless they were resolved from aliases
184+
if (!moduleId.startsWith('.') && !wasAliasResolved) {
171185
return
172186
}
187+
173188
if (updatedStarts.has(req.start)) {
174189
return // prevent double rewritings
175190
}
176191
updatedStarts.add(req.start)
192+
177193
const resolvedAbsolute = resolveModulePath(moduleId, resolveOptions)
178194
const newId = relative(
179195
dirname(entryPath),
@@ -204,6 +220,7 @@ async function transformModule(entryPath: string, entry: TransformEntry) {
204220
...entry.oxc,
205221
...sourceOptions,
206222
cwd: dirname(entryPath),
223+
target: entry.target || 'es2022',
207224
typescript: {
208225
declaration: { stripInternal: true },
209226
...entry.oxc?.typescript,

src/cli.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const args = parseArgs({
3232
'platform': {
3333
type: 'string',
3434
},
35+
'target': {
36+
type: 'string',
37+
},
3538
'global-name': {
3639
type: 'string',
3740
},
@@ -70,6 +73,7 @@ Options:
7073
-w, --watch Enable watch mode
7174
--format <format> Output format(s): esm, cjs, iife, umd (can be used multiple times)
7275
--platform <platform> Target platform: browser, node, neutral
76+
--target <target> Target ES version: es5, es2015, es2016, es2017, es2018, es2019, es2020, es2021, es2022, esnext
7377
--global-name <name> Global variable name for IIFE/UMD formats
7478
--clean Clean output directory before build (default: true)
7579
--no-clean Disable cleaning output directory
@@ -131,6 +135,11 @@ const entries: BuildEntry[] = rawEntries.map((entry) => {
131135
baseEntry.platform = args.values.platform
132136
}
133137

138+
// Target option
139+
if (args.values.target) {
140+
baseEntry.target = args.values.target
141+
}
142+
134143
// Global name for IIFE/UMD
135144
if (args.values['global-name']) {
136145
baseEntry.globalName = args.values['global-name']

src/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { Options as DtsOptions } from 'rolldown-plugin-dts'
1313

1414
export type OutputFormat = 'esm' | 'cjs' | 'iife' | 'umd'
1515
export type Platform = 'browser' | 'node' | 'neutral'
16+
export type Target = 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'esnext'
1617

1718
export interface BuildContext {
1819
pkgDir: string
@@ -46,11 +47,25 @@ export interface _BuildEntry {
4647
*/
4748
platform?: Platform
4849

50+
/**
51+
* Target ES version for the build.
52+
*
53+
* Defaults to `'es2022'` if not provided.
54+
*/
55+
target?: Target
56+
4957
/**
5058
* Global variable name for IIFE/UMD formats.
5159
*/
5260
globalName?: string
5361

62+
/**
63+
* Module path aliases.
64+
*
65+
* Allows defining path mappings for imports.
66+
*/
67+
alias?: Record<string, string>
68+
5469
/**
5570
* Clean output directory before build.
5671
*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## index.mjs
2+
3+
```js
4+
//#region src/utils/helper.ts
5+
const helper = "helper function";
6+
7+
//#endregion
8+
//#region src/index.ts
9+
const test = helper;
10+
11+
//#endregion
12+
export { test };
13+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## index.d.mts
2+
3+
```ts
4+
export declare const test: unknown;
5+
6+
```
7+
8+
## index.mjs
9+
10+
```js
11+
import { helper } from "./utils/helper.mjs";
12+
export const test = helper;
13+
14+
```
15+
16+
## utils/helper.d.mts
17+
18+
```ts
19+
export declare const helper = "helper function";
20+
21+
```
22+
23+
## utils/helper.mjs
24+
25+
```js
26+
export const helper = "helper function";
27+
28+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## index.d.mts
2+
3+
```ts
4+
export declare const test: unknown;
5+
6+
```
7+
8+
## index.mjs
9+
10+
```js
11+
const _excluded = ["a"];
12+
import _objectWithoutProperties from "@oxc-project/runtime/helpers/objectWithoutProperties";
13+
import _asyncToGenerator from "@oxc-project/runtime/helpers/asyncToGenerator";
14+
export const test = function() {
15+
var _ref = _asyncToGenerator(function* () {
16+
const obj = {
17+
a: 1,
18+
b: 2
19+
};
20+
const { a } = obj, rest = _objectWithoutProperties(obj, _excluded);
21+
return {
22+
a,
23+
rest
24+
};
25+
});
26+
return function test() {
27+
return _ref.apply(this, arguments);
28+
};
29+
}();
30+
31+
```

0 commit comments

Comments
 (0)