mirror of
https://github.com/privacy-scaling-explorations/emp-wasm.git
synced 2026-01-09 10:07:54 -05:00
refactor: add code to fix EmscriptenCode again
This commit is contained in:
@@ -38,6 +38,10 @@ async function build() {
|
||||
|
||||
await shell('tsc', [], gitRoot);
|
||||
|
||||
// We need to fix this in the actual file rather than combining it with
|
||||
// `getEmscriptenCode` because the file itself is used when loading in NodeJS.
|
||||
await fixEmscriptenCode(gitRoot);
|
||||
|
||||
const workerCode = [
|
||||
await getEmscriptenCode(),
|
||||
await getAppendWorkerCode(),
|
||||
@@ -103,4 +107,28 @@ async function shell(cmd: string, args: string[], cwd: string): Promise<void> {
|
||||
});
|
||||
}
|
||||
|
||||
async function fixEmscriptenCode(gitRoot: string) {
|
||||
await replaceInFile(
|
||||
join(gitRoot, 'build/jslib.js'),
|
||||
'var fs=require("fs")',
|
||||
// This doesn't really affect behavior, but it fixes a nextjs issue where
|
||||
// it analyzes the require statically and fails even when the code works as
|
||||
// a whole.
|
||||
'var fs=(()=>{try{return require("fs")}catch(e){throw e}})();'
|
||||
);
|
||||
}
|
||||
|
||||
async function replaceInFile(path: string, search: string, replace: string) {
|
||||
const content = await fs.readFile(path, 'utf-8');
|
||||
const parts = content.split(search);
|
||||
|
||||
if (parts.length === 1) {
|
||||
throw new Error(`Search string not found in file: ${search}`);
|
||||
}
|
||||
|
||||
const updatedContent = parts.join(replace);
|
||||
|
||||
await fs.writeFile(path, updatedContent, 'utf-8');
|
||||
}
|
||||
|
||||
build().catch(console.error);
|
||||
|
||||
Reference in New Issue
Block a user