From 87c486a29aedabe02f333bc5eafc533f7a8ef3eb Mon Sep 17 00:00:00 2001
From: mtyszczak <mateusz.tyszczak@gmail.com>
Date: Tue, 21 Jan 2025 15:54:28 +0100
Subject: [PATCH] Fix examples

---
 examples/ts/html/index.ts              |  5 ++++-
 examples/ts/nextjs-app/next.config.mjs | 11 +++++++++++
 ts/package.json                        |  5 +++--
 ts/rollup.config.js                    | 16 ++--------------
 4 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/examples/ts/html/index.ts b/examples/ts/html/index.ts
index 9bd53337b..a3d98c186 100644
--- a/examples/ts/html/index.ts
+++ b/examples/ts/html/index.ts
@@ -22,7 +22,10 @@ const configFor = (name: string): ConstructorParameters<typeof Parcel>[0] => ({
   const port = 8000;
 
   const server = createServer({
-    root: path.resolve(__dirname, 'dist')
+    root: path.resolve(__dirname, 'dist'),
+    logFn(req, res) {
+      console.log(req.method, req.url, res.statusCode);
+    },
   });
 
   server.listen(port);
diff --git a/examples/ts/nextjs-app/next.config.mjs b/examples/ts/nextjs-app/next.config.mjs
index d5456a15d..01487a7be 100644
--- a/examples/ts/nextjs-app/next.config.mjs
+++ b/examples/ts/nextjs-app/next.config.mjs
@@ -1,6 +1,17 @@
 /** @type {import('next').NextConfig} */
 const nextConfig = {
   reactStrictMode: true,
+  webpack5: true, // Enabled by default - just to be sure - follow this guide if using webpack <5 - https://bobbyhadz.com/blog/module-not-found-cant-resolve-fs#module-not-found-cant-resolve-fs-error-in-nextjs
+  webpack: (config, { isServer }) => {
+    if (!isServer) // Prevents client bundles from including node specific packages required for WASM loading in wax
+      config.resolve.fallback = {
+        fs: false,
+        path: false,
+        module: false
+      };
+
+    return config;
+  }
 };
 
 export default nextConfig;
diff --git a/ts/package.json b/ts/package.json
index be6f3b6aa..4d98ca0dd 100644
--- a/ts/package.json
+++ b/ts/package.json
@@ -93,14 +93,15 @@
     "LICENSE.md",
     "README.md",
     "wasm/dist/bundle/index.js",
-    "wasm/dist/bundle/index.d.ts"
+    "wasm/dist/bundle/index.d.ts",
+    "wasm/dist/bundle/wax.common.wasm"
   ],
   "repository": {
     "type": "git",
     "url": "https://gitlab.syncad.com/hive/wax.git"
   },
   "engines": {
-    "node": ">= 18"
+    "node": "^20.11 || >= 21.2"
   },
   "publishConfig": {
     "registry": "https://RegistryPlaceholder",
diff --git a/ts/rollup.config.js b/ts/rollup.config.js
index 6d643b524..2ad9147f7 100644
--- a/ts/rollup.config.js
+++ b/ts/rollup.config.js
@@ -26,20 +26,8 @@ const commonConfiguration = packEntire => ([
       replace({
         delimiters: ['', ''],
         values: {
-/*          // Generated Emscripten WASM code contains fs, which is not actually used by our code, so remove it to prevent client bundler errors:
-          'fs.readFileSync(filename,binary?undefined:"utf8")': null,
-          'fs.readFile(filename,binary?undefined:"utf8",(err,data)=>{if(err)onerror(err);else onload(binary?data.buffer:data)})': null,
-          'fs.readSync(fd,buf)': '0', // fallback - readSync returns the number of bytesRead
-          // Instead of fs we need crypto module in Node.js environment later for SSL initRandomDevice code - ensure proper module is imported:
-          'var fs=require("fs")': 'var node_crypto=await import("crypto")',
-          'require("crypto")': 'node_crypto',
-          // 'module' dependency is redundant in our environment - use 'await import' instead:
-          'const{createRequire:createRequire}=await import("module");': '',
-          'var require=createRequire(import.meta.url);': '',
-          'createRequire(import.meta.url);': '',
-          'var nodePath=require("path")': 'var nodePath=await import("path")',
-          // new URL("./") throws - use import.meta.url instead:
-          'require("url").fileURLToPath(new URL("./",import.meta.url))': 'import.meta.url',*/
+          // Equivalent and Supported by Node.js >=20.11 - replacing this results in less imports during runtime
+          'require("url").fileURLToPath(new URL("./",import.meta.url))': 'import.meta.dirname',
           // Hardcode package name and version for later use in the code:
           'process.env.npm_package_name': `"${process.env.npm_package_name}"`,
           'process.env.npm_package_version': `"${process.env.npm_package_version}"`,
-- 
GitLab