fix nextjs by preventing static analysis of import

This commit is contained in:
Andrew Morris
2025-04-10 15:17:33 +10:00
parent b76a8325c8
commit ffde4d02b7
3 changed files with 10 additions and 5 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "emp-wasm",
"version": "0.2.2",
"version": "0.2.3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "emp-wasm",
"version": "0.2.2",
"version": "0.2.3",
"license": "MIT",
"dependencies": {
"ee-typed": "^0.1.1",

View File

@@ -1,6 +1,6 @@
{
"name": "emp-wasm",
"version": "0.2.2",
"version": "0.2.3",
"description": "Wasm build of authenticated garbling from [emp-toolkit/emp-ag2pc](https://github.com/emp-toolkit/emp-ag2pc).",
"type": "module",
"main": "dist/src/ts/index.js",

View File

@@ -111,6 +111,8 @@ async function fixEmscriptenCode(gitRoot: string) {
const path = join(gitRoot, 'dist/build/jslib.js');
let content = await fs.readFile(path, 'utf-8');
content = `function echo(x) { return x; }\n${content}`;
content = replace(
content,
['function intArrayFromBase64(s) { if (typeof ENVIRONMENT_IS_NODE'],
@@ -134,14 +136,17 @@ async function fixEmscriptenCode(gitRoot: string) {
// 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.
// FIXME: I can no longer reproduce nextjs failing without this replacement.
// It's easier to just leave it in for now but in future this should help
// motivate its removal if nextjs still doesn't seem to need it anymore.
'var fs=(()=>{try{return require("fs")}catch(e){throw e}})();'
);
// For deno compatibility
// For deno and nextjs compatibility
content = replace(
content,
['import("module")', "import('module')"],
'import("node:module")',
'import(echo("node:module"))',
);
await fs.writeFile(path, content);