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
6 changes: 4 additions & 2 deletions packages/build-tools/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFile } from 'node:fs/promises';
import {
calculateChecksum,
fileExists,
Expand All @@ -9,15 +10,16 @@ export function getBuildDataPath(src: string): string {
}

export async function ShouldComponentize(
src: string, outputPath: string, componentizeVersion: string, runtimeArgs: string,
src: string, outputPath: string, componentizeVersion: string, runtimeArgs: string, targetWitChecksum: string
) {
const sourceChecksum = await calculateChecksum(src);
const sourceChecksum = await calculateChecksum(await readFile(src));
const existingBuildData = await getExistingBuildData(getBuildDataPath(src));

if (
existingBuildData?.version == componentizeVersion &&
existingBuildData?.checksum === sourceChecksum &&
existingBuildData?.runtimeArgs === runtimeArgs &&
existingBuildData?.targetWitChecksum === targetWitChecksum &&
(await fileExists(outputPath))
) {
return false;
Expand Down
22 changes: 12 additions & 10 deletions packages/build-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ async function main() {
let outputPath = CliArgs.output;
let runtimeArgs = CliArgs.debug ? '--enable-script-debugging' : '';

// Small optimization to skip componentization if the source file hasn't changed
if (!(await ShouldComponentize(src, outputPath, componentizeVersion, runtimeArgs))) {
console.log(
'No changes detected in source file. Skipping componentization.',
);
return;
}
console.log('Componentizing...');

// generate wit world string
let wasiDeps = getPackagesWithWasiDeps(process.cwd(), new Set(), true);
let { witPaths, targetWorlds } = processWasiDeps(wasiDeps);
Expand Down Expand Up @@ -63,6 +54,16 @@ async function main() {
'combined-wit:[email protected]',
);

let inlineWitChecksum = await calculateChecksum(inlineWit);
// Small optimization to skip componentization if the source file hasn't changed
if (!(await ShouldComponentize(src, outputPath, componentizeVersion, runtimeArgs, inlineWitChecksum))) {
console.log(
'No changes detected in source file and target World. Skipping componentization.',
);
return;
}
console.log('Componentizing...');

const source = await readFile(src, 'utf8');
const precompiledSource = precompile(source, src, true) as string;

Expand All @@ -85,9 +86,10 @@ async function main() {
// Save the checksum of the input file along with the componentize version
await saveBuildData(
getBuildDataPath(src),
await calculateChecksum(src),
await calculateChecksum(await readFile(src)),
componentizeVersion,
runtimeArgs,
inlineWitChecksum,
);

console.log('Component successfully written.');
Expand Down
11 changes: 6 additions & 5 deletions packages/build-tools/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { access, writeFile } from 'node:fs/promises';


// Function to calculate file checksum
export async function calculateChecksum(filePath: string) {
export async function calculateChecksum(content: string | Buffer) {
try {
const fileBuffer = await readFile(filePath);
const hash = createHash('sha256');
hash.update(fileBuffer);
hash.update(content);
return hash.digest('hex');
} catch (error) {
console.error(`Error calculating checksum for file ${filePath}:`, error);
console.error(`Error calculating checksum:`, error);
throw error;
}
}


// Function to check if a file exists
export async function fileExists(filePath: string) {
try {
Expand Down Expand Up @@ -43,13 +43,14 @@ export async function getExistingBuildData(buildDataPath: string) {
}

export async function saveBuildData(
buildDataPath: string, checksum: string, version: string, runtimeArgs: string,
buildDataPath: string, checksum: string, version: string, runtimeArgs: string, targetWitChecksum: string
) {
try {
const checksumData = {
version,
checksum,
runtimeArgs,
targetWitChecksum,
};
await writeFile(buildDataPath, JSON.stringify(checksumData, null, 2));
} catch (error) {
Expand Down
Loading