Skip to content

conflict with nodePolyfills() #28

@StackOverflowExcept1on

Description

@StackOverflowExcept1on

this code is executed:

kzg-wasm/src/loader.mjs

Lines 4 to 15 in f0ed6cc

if (typeof process !== 'undefined' && typeof process.cwd === 'function') {
// Dynamically import Node.js modules only when in Node.js environment
const { resolve, dirname } = await import('path')
const { readFileSync } = await import('fs')
const { fileURLToPath } = await import('url')
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const wasmPath = resolve(__dirname, '../wasm/kzg.wasm')
const buffer = readFileSync(wasmPath)
// Convert Node.js Buffer to ArrayBuffer
wasmBinary = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)
}

Failed to load KZG: TypeError: The URL must be of scheme file
    at fileURLToPath (url.js:996:11)
    at loadWasmModule (loader.mjs:8:28)
    at async loadKZG (index.js:20:22)
    at async App.tsx:34:27
(anonymous) @ App.tsx:38
diff --git a/package.json b/package.json
index ec4b7f3..73374fc 100644
--- a/package.json
+++ b/package.json
@@ -13,8 +13,10 @@
     "@emotion/styled": "latest",
     "@mui/icons-material": "next",
     "@mui/material": "next",
+    "kzg-wasm": "latest",
     "react": "latest",
-    "react-dom": "latest"
+    "react-dom": "latest",
+    "vite-plugin-node-polyfills": "^0.25.0"
   },
   "devDependencies": {
     "@types/react": "latest",
diff --git a/src/App.tsx b/src/App.tsx
index cf622f0..27f9c62 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,4 +1,5 @@
-import * as React from 'react';
+import { useEffect, useState } from 'react';
+import { loadKZG } from 'kzg-wasm';
 import Container from '@mui/material/Container';
 import Typography from '@mui/material/Typography';
 import Box from '@mui/material/Box';
@@ -24,12 +25,32 @@ function Copyright() {
 }
 
 export default function App() {
+  const [kzg, setKZG] = useState<any>(null);
+
+  useEffect(() => {
+    (async () => {
+      try {
+        debugger;
+        const kzgModule = await loadKZG();
+        console.log('KZG loaded:', kzgModule);
+        setKZG(kzgModule);
+      } catch (err) {
+        console.error('Failed to load KZG:', err);
+      }
+    })();
+  }, []);
+
   return (
     <Container maxWidth="sm">
       <Box sx={{ my: 4 }}>
         <Typography variant="h4" component="h1" sx={{ mb: 2 }}>
           Material UI Vite example in TypeScript
         </Typography>
+        {kzg ? (
+          <Typography>KZG module loaded successfully!</Typography>
+        ) : (
+          <Typography>Loading KZG module...</Typography>
+        )}
         <ProTip />
         <Copyright />
       </Box>
diff --git a/vite.config.ts b/vite.config.ts
index 4a5def4..8740d01 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,7 +1,8 @@
 import { defineConfig } from 'vite';
 import react from '@vitejs/plugin-react';
+import { nodePolyfills } from 'vite-plugin-node-polyfills';
 
 // https://vite.dev/config/
 export default defineConfig({
-  plugins: [react()],
+  plugins: [react(), nodePolyfills()],
 });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions