Skip to content

Commit

Permalink
feat: incremental build (#123)
Browse files Browse the repository at this point in the history
* incremental

* changeset

* also incremental in ts

* omit incremental flag completely
  • Loading branch information
enisdenjo committed Aug 15, 2022
1 parent c0ce567 commit b68da59
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-moles-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bob-the-bundler": minor
---

better performance by incrementally building only packages that had changes
36 changes: 28 additions & 8 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ function assertTypeScriptBuildResult(result: execa.ExecaReturnValue<string>) {
}
}

async function buildTypeScript(buildPath: string) {
async function buildTypeScript(
buildPath: string,
options: { incremental?: boolean } = {}
) {
assertTypeScriptBuildResult(
await execa("npx", [
"tsc",
...compilerOptionsToArgs(typeScriptCompilerOptions("esm")),
...(options.incremental ? ["--incremental"] : []),
"--outDir",
join(buildPath, "esm"),
])
Expand All @@ -88,22 +92,34 @@ async function buildTypeScript(buildPath: string) {
await execa("npx", [
"tsc",
...compilerOptionsToArgs(typeScriptCompilerOptions("cjs")),
...(options.incremental ? ["--incremental"] : []),
"--outDir",
join(buildPath, "cjs"),
])
);
}

export const buildCommand = createCommand<{}, {}>((api) => {
export const buildCommand = createCommand<
{},
{
incremental?: boolean;
}
>((api) => {
const { reporter } = api;

return {
command: "build",
describe: "Build",
builder(yargs) {
return yargs.options({});
return yargs.options({
incremental: {
describe:
"Better performance by building only packages that had changes.",
type: "boolean",
},
});
},
async handler() {
async handler({ incremental }) {
const cwd = process.cwd();
const rootPackageJSON = await getRootPackageJSON(cwd);
const workspaces = getWorkspaces(rootPackageJSON);
Expand All @@ -112,8 +128,10 @@ export const buildCommand = createCommand<{}, {}>((api) => {
if (isSinglePackage) {
const buildPath = join(cwd, ".bob");

await fse.remove(buildPath);
await buildTypeScript(buildPath);
if (!incremental) {
await fse.remove(buildPath);
}
await buildTypeScript(buildPath, { incremental });
const pkg = await fse.readJSON(resolve(cwd, "package.json"));
const fullName: string = pkg.name;

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

const bobBuildPath = join(cwd, ".bob");
await fse.remove(bobBuildPath);
await buildTypeScript(bobBuildPath);
if (!incremental) {
await fse.remove(bobBuildPath);
}
await buildTypeScript(bobBuildPath, { incremental });

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

0 comments on commit b68da59

Please sign in to comment.