Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions scripts/buildPackages/src/buildPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import path from "path";
import { hideBin } from "yargs/helpers";
import { PackageBuildError } from "./PackageBuildError";
import { queueMetaWrite } from "./writeMetaQueue";
import { writeDistBuildHash } from "./distBuildHash";

const argv = yargs(hideBin(process.argv)).parse();

Expand Down Expand Up @@ -88,6 +89,15 @@ export const buildPackages = async () => {
const [pkg] = allPackages;
try {
await buildPackage(pkg, options.buildOverrides, "inherit", options.safeReplace);

// Record the source hash (in dist as a marker and in build meta) so
// a later build treats this package as a cache hit and skips the copy.
const sourceHash = await getPackageSourceHash(pkg);
writeDistBuildHash(pkg, sourceHash);
const meta = getBuildMeta();
meta.packages[pkg.packageJson.name] = { sourceHash };
writeJsonFileSync(META_FILE_PATH, meta);

sendNotification(`Webiny Build (${projectFolder})`, "Build completed successfully");
} catch (err) {
sendNotification(`Webiny Build (${projectFolder})`, "Build failed");
Expand Down Expand Up @@ -130,6 +140,9 @@ export const buildPackages = async () => {

// Store package hash
const sourceHash = await getPackageSourceHash(pkg);
// Stamp dist so a later no-op build can
// skip the cache→dist copy for this package.
writeDistBuildHash(pkg, sourceHash);
await queueMetaWrite(async () => {
const currentMeta = getBuildMeta();
currentMeta.packages[pkg.packageJson.name] = {
Expand Down
33 changes: 33 additions & 0 deletions scripts/buildPackages/src/distBuildHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from "fs-extra";
import path from "path";
import { getBuildOutputFolder } from "./getBuildOutputFolder";
import type { Package } from "./types";

// Marker file written into a package's build output recording the source hash
// the output was produced from. Lets the orchestrator skip re-copying a cached
// dist into place when the existing dist is already up to date.
const MARKER = ".webiny-build-hash";

const markerPath = (pkg: Package) => path.join(getBuildOutputFolder(pkg), MARKER);

export function readDistBuildHash(pkg: Package): string | null {
const file = markerPath(pkg);
if (!fs.existsSync(file)) {
return null;
}
try {
return fs.readFileSync(file, "utf8").trim();
} catch {
return null;
}
}

export function writeDistBuildHash(pkg: Package, hash: string) {
const dir = getBuildOutputFolder(pkg);
fs.ensureDirSync(dir);
fs.writeFileSync(markerPath(pkg), hash);
}

export function distBuildHashMatches(pkg: Package, hash: string): boolean {
return readDistBuildHash(pkg) === hash;
}
21 changes: 21 additions & 0 deletions scripts/buildPackages/src/getBatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getBuildOutputFolder } from "./getBuildOutputFolder";
import { getPackageSourceHash } from "./getPackageSourceHash";
import { getBuildMeta } from "./getBuildMeta";
import { getPackageCacheFolderPath } from "./getPackageCacheFolderPath";
import { distBuildHashMatches, writeDistBuildHash } from "./distBuildHash";

const { green } = chalk;

Expand All @@ -23,6 +24,9 @@ export async function getBatches(options: GetBatchesOptions = {}) {

const packagesNoCache: Package[] = [];
const packagesUseCache: Package[] = [];
// Source hash of each cache-hit package, reused below to skip the cache→dist
// copy when the existing dist was already built from that same hash.
const cacheHitHashes = new Map<string, string>();

let workspacesPackages = (
getPackages({
Expand Down Expand Up @@ -62,6 +66,7 @@ export async function getBatches(options: GetBatchesOptions = {}) {

if (packageMeta.sourceHash === sourceHash) {
packagesUseCache.push(workspacePackage);
cacheHitHashes.set(workspacePackage.name, sourceHash);
} else {
packagesNoCache.push(workspacePackage);
}
Expand Down Expand Up @@ -114,10 +119,26 @@ export async function getBatches(options: GetBatchesOptions = {}) {
}
}

let copied = 0;
for (let i = 0; i < packagesUseCache.length; i++) {
const workspacePackage = packagesUseCache[i];
const sourceHash = cacheHitHashes.get(workspacePackage.name)!;

// Skip the copy when dist was already built/restored from this exact
// source hash — the bytes on disk are already identical. This is the
// common local-dev case (dist persists between builds).
if (distBuildHashMatches(workspacePackage, sourceHash)) {
continue;
}

const cacheFolderPath = path.join(CACHE_FOLDER_PATH, workspacePackage.packageJson.name);
fs.copySync(cacheFolderPath, getBuildOutputFolder(workspacePackage));
writeDistBuildHash(workspacePackage, sourceHash);
copied++;
}

if (copied > 0) {
console.log(`Restored ${green(copied)} package(s) from cache into dist.`);
}
} else {
if (useCache) {
Expand Down
Loading