Skip to content

Commit 33a5869

Browse files
committed
feat!: unified config for programmatic api
1 parent 6d5567e commit 33a5869

File tree

5 files changed

+56
-35
lines changed

5 files changed

+56
-35
lines changed

README.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The **obuild** project aims to be the next-generation successor to the current [
88

99
- 👌 Focus on ESM compatibility.
1010
- 🌱 Fresh rewrite with cleanups and removal of legacy features.
11-
- 🚀 Using [**oxc**](https://oxc.rs/) and [**rolldown**](https://rolldown.rs/) for much faster builds!
11+
- 🚀 Using [**oxc**](https://oxc.rs/) (for transform) and [**rolldown**](https://rolldown.rs/) (for bundle) for much faster builds!
1212

1313
Some differences are not easy to adopt. Developing as a standalone project allows for faster progress and dogfooding in real projects.
1414

@@ -45,9 +45,22 @@ npx obuild ./src/runtime/:./dist/runtime
4545

4646
You can use `--dir` to set the working directory.
4747

48-
## Optional CLI Config
48+
If paths end with `/`, obuild uses transpile mode using [oxc-transform](https://www.npmjs.com/package/oxc-transform) instead of bundle mode with [rolldown](https://rolldown.rs/).
4949

50-
**`build.config.mjs`:**
50+
### Programmatic
51+
52+
```js
53+
import { build } from "obuild";
54+
55+
await build({
56+
cwd: ".",
57+
entries: ["./src/index.ts"],
58+
});
59+
```
60+
61+
## Config
62+
63+
You can use `build.config.mjs` (or `.ts`) or pass config to `build()` function.
5164

5265
```js
5366
import { defineBuildConfig } from "obuild";
@@ -60,10 +73,8 @@ export default defineBuildConfig({
6073
// outDir: "./dist",
6174
// minify: false,
6275
// stub: false,
63-
// https://rolldown.rs/reference/config-options
64-
// rolldown: {},
65-
// https://github.com/sxzz/rolldown-plugin-dts#options
66-
// dts: {},
76+
// rolldown: {}, // https://rolldown.rs/reference/config-options
77+
// dts: {}, // https://github.com/sxzz/rolldown-plugin-dts#options
6778
},
6879
{
6980
type: "transform",
@@ -74,22 +85,16 @@ export default defineBuildConfig({
7485
// oxc: {},
7586
},
7687
],
88+
hooks: {
89+
// start: (ctx) => {},
90+
// end: (ctx) => {},
91+
// entries: (entries, ctx) => {},
92+
// rolldownConfig: (config, ctx) => {},
93+
// rolldownOutput: (output, res, ctx) => {},
94+
},
7795
});
7896
```
7997

80-
### Programmatic
81-
82-
```js
83-
import { build } from "obuild";
84-
85-
await build(".", [
86-
/* ... entries ... */
87-
]);
88-
```
89-
90-
> [!NOTE]
91-
> Auto entries inference similar to unbuild coming soon ([#4](https://github.com/unjs/obuild/issues/4)).
92-
9398
## Stub Mode
9499

95100
When working on a package locally, it can be tedious to rebuild or run the watch command every time.

src/build.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { BuildEntry, BuildContext, BuildConfig } from "./types.ts";
1+
import type {
2+
BuildContext,
3+
BuildConfig,
4+
TransformEntry,
5+
BundleEntry,
6+
} from "./types.ts";
27

38
import { fileURLToPath } from "node:url";
49
import { isAbsolute, join, resolve } from "node:path";
@@ -13,24 +18,29 @@ import prettyBytes from "pretty-bytes";
1318
/**
1419
* Build dist/ from src/
1520
*/
16-
export async function build(
17-
_cwd: string | URL,
18-
_entries: BuildEntry[],
19-
{ hooks = {} }: Omit<BuildConfig, "entries"> = {},
20-
): Promise<void> {
21+
export async function build(config: BuildConfig): Promise<void> {
2122
const start = Date.now();
2223

23-
const pkgDir = normalizePath(_cwd);
24+
const pkgDir = normalizePath(config.cwd);
2425
const pkg = await readJSON(join(pkgDir, "package.json")).catch(() => ({}));
2526
const ctx: BuildContext = { pkg, pkgDir };
2627

2728
consola.log(
2829
`📦 Building \`${ctx.pkg.name || "<no name>"}\` (\`${ctx.pkgDir}\`)`,
2930
);
3031

32+
const hooks = config.hooks || {};
33+
3134
await hooks.start?.(ctx);
3235

33-
const entries = _entries.map((entry) => {
36+
const entries = (config.entries || []).map((entry) => {
37+
if (typeof entry === "string") {
38+
const [input, outDir] = entry.split(":") as [string, string | undefined];
39+
return input.endsWith("/")
40+
? ({ type: "transform", input, outDir } as TransformEntry)
41+
: ({ type: "bundle", input: input.split(","), outDir } as BundleEntry);
42+
}
43+
3444
if (!entry.input) {
3545
throw new Error(
3646
`Build entry missing \`input\`: ${JSON.stringify(entry, null, 2)}`,

src/cli.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ const { config = {} } = await loadConfig<BuildConfig>({
2929
cwd: args.values.dir,
3030
});
3131

32-
const dir = args.values.dir;
33-
3432
const rawEntries =
3533
args.positionals.length > 0
3634
? (args.positionals as string[])
@@ -57,4 +55,8 @@ if (rawEntries.length === 0) {
5755
process.exit(1);
5856
}
5957

60-
await build(dir, entries, config);
58+
await build({
59+
cwd: args.values.dir,
60+
...config,
61+
entries,
62+
});

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export interface BuildHooks {
104104
}
105105

106106
export interface BuildConfig {
107+
cwd?: string | URL;
107108
entries?: (BuildEntry | string)[];
108109
hooks?: BuildHooks;
109110
}

test/obuild.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ describe("obuild", () => {
1212
});
1313

1414
test("build fixture", async () => {
15-
await build(fixtureDir, [
16-
{ type: "bundle", input: ["src/index", "src/cli"] },
17-
{ type: "transform", input: "src/runtime", outDir: "dist/runtime" },
18-
]);
15+
await build({
16+
cwd: fixtureDir,
17+
entries: [
18+
{ type: "bundle", input: ["src/index", "src/cli"] },
19+
{ type: "transform", input: "src/runtime", outDir: "dist/runtime" },
20+
],
21+
});
1922
});
2023

2124
test("dist files match expected", async () => {

0 commit comments

Comments
 (0)