Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions packages/build-tools/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
calculateChecksum,
calculateFileChecksum,
fileExists,
getExistingBuildData,
} from './utils.js';
Expand All @@ -9,15 +9,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 calculateFileChecksum(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
26 changes: 15 additions & 11 deletions packages/build-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { componentize } from '@bytecodealliance/componentize-js';
import { version as componentizeVersion } from '@bytecodealliance/componentize-js';
import { getPackagesWithWasiDeps, processWasiDeps } from './wasiDepsParser.js';
import {
calculateChecksum,
calculateCheckSum,
calculateFileChecksum,
saveBuildData,
} from './utils.js';
import { getCliArgs } from './cli.js';
Expand All @@ -23,15 +24,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 +55,17 @@ async function main() {
'combined-wit:[email protected]',
);

// Calcuilate the checksum of the inline wit
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 +88,10 @@ async function main() {
// Save the checksum of the input file along with the componentize version
await saveBuildData(
getBuildDataPath(src),
await calculateChecksum(src),
await calculateFileChecksum(src),
componentizeVersion,
runtimeArgs,
inlineWitChecksum,
);

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


// Function to calculate file checksum
export async function calculateChecksum(filePath: string) {
export async function calculateFileChecksum(filePath: string) {
try {
const fileBuffer = await readFile(filePath);
const hash = createHash('sha256');
Expand All @@ -16,6 +16,12 @@ export async function calculateChecksum(filePath: string) {
}
}

export function calculateCheckSum(data: string | Buffer): string {
const hash = createHash('sha256');
hash.update(data);
return hash.digest('hex');
}

// Function to check if a file exists
export async function fileExists(filePath: string) {
try {
Expand Down Expand Up @@ -43,13 +49,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