Skip to content

Commit b68da59

Browse files
authored
feat: incremental build (#123)
* incremental * changeset * also incremental in ts * omit incremental flag completely
1 parent c0ce567 commit b68da59

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

.changeset/cold-moles-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bob-the-bundler": minor
3+
---
4+
5+
better performance by incrementally building only packages that had changes

src/commands/build.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ function assertTypeScriptBuildResult(result: execa.ExecaReturnValue<string>) {
7474
}
7575
}
7676

77-
async function buildTypeScript(buildPath: string) {
77+
async function buildTypeScript(
78+
buildPath: string,
79+
options: { incremental?: boolean } = {}
80+
) {
7881
assertTypeScriptBuildResult(
7982
await execa("npx", [
8083
"tsc",
8184
...compilerOptionsToArgs(typeScriptCompilerOptions("esm")),
85+
...(options.incremental ? ["--incremental"] : []),
8286
"--outDir",
8387
join(buildPath, "esm"),
8488
])
@@ -88,22 +92,34 @@ async function buildTypeScript(buildPath: string) {
8892
await execa("npx", [
8993
"tsc",
9094
...compilerOptionsToArgs(typeScriptCompilerOptions("cjs")),
95+
...(options.incremental ? ["--incremental"] : []),
9196
"--outDir",
9297
join(buildPath, "cjs"),
9398
])
9499
);
95100
}
96101

97-
export const buildCommand = createCommand<{}, {}>((api) => {
102+
export const buildCommand = createCommand<
103+
{},
104+
{
105+
incremental?: boolean;
106+
}
107+
>((api) => {
98108
const { reporter } = api;
99109

100110
return {
101111
command: "build",
102112
describe: "Build",
103113
builder(yargs) {
104-
return yargs.options({});
114+
return yargs.options({
115+
incremental: {
116+
describe:
117+
"Better performance by building only packages that had changes.",
118+
type: "boolean",
119+
},
120+
});
105121
},
106-
async handler() {
122+
async handler({ incremental }) {
107123
const cwd = process.cwd();
108124
const rootPackageJSON = await getRootPackageJSON(cwd);
109125
const workspaces = getWorkspaces(rootPackageJSON);
@@ -112,8 +128,10 @@ export const buildCommand = createCommand<{}, {}>((api) => {
112128
if (isSinglePackage) {
113129
const buildPath = join(cwd, ".bob");
114130

115-
await fse.remove(buildPath);
116-
await buildTypeScript(buildPath);
131+
if (!incremental) {
132+
await fse.remove(buildPath);
133+
}
134+
await buildTypeScript(buildPath, { incremental });
117135
const pkg = await fse.readJSON(resolve(cwd, "package.json"));
118136
const fullName: string = pkg.name;
119137

@@ -150,8 +168,10 @@ export const buildCommand = createCommand<{}, {}>((api) => {
150168
);
151169

152170
const bobBuildPath = join(cwd, ".bob");
153-
await fse.remove(bobBuildPath);
154-
await buildTypeScript(bobBuildPath);
171+
if (!incremental) {
172+
await fse.remove(bobBuildPath);
173+
}
174+
await buildTypeScript(bobBuildPath, { incremental });
155175

156176
await Promise.all(
157177
packageInfoList.map(({ cwd, pkg, fullName }) =>

0 commit comments

Comments
 (0)