frontend-hamroun 1.2.67 → 1.2.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +353 -0
- package/bin/cli.js +2 -22
- package/dist/index.d.ts +3 -3
- package/package.json +11 -1
- package/templates/go-wasm-app/package.json +0 -20
- package/templates/go/README.md +0 -150
- package/templates/go-wasm-app/.tmp-wasm-build/example.go +0 -75
- package/templates/go-wasm-app/.tmp-wasm-build/go.mod +0 -3
- package/templates/go-wasm-app/README.md +0 -21
- package/templates/go-wasm-app/package-lock.json +0 -4123
- package/templates/go-wasm-app/public/index.html +0 -13
- package/templates/go-wasm-app/public/styles.css +0 -167
- package/templates/go-wasm-app/src/App.jsx +0 -64
- package/templates/go-wasm-app/src/components/WasmDemo.jsx +0 -119
- package/templates/go-wasm-app/src/main.jsx +0 -7
- package/templates/go-wasm-app/src/wasm/example.go +0 -75
- package/templates/go-wasm-app/vite.config.js +0 -28
package/README.md
CHANGED
@@ -19,6 +19,7 @@ A lightweight full-stack JavaScript framework with Virtual DOM and hooks impleme
|
|
19
19
|
- [Core Concepts](#-core-concepts)
|
20
20
|
- [Frontend Features](#-frontend-features)
|
21
21
|
- [Backend Features](#-backend-features)
|
22
|
+
- [WebAssembly Integration](#-webassembly-integration)
|
22
23
|
- [API Reference](#-api-reference)
|
23
24
|
- [CLI Tools](#-cli-tools)
|
24
25
|
- [Advanced Usage](#-advanced-usage)
|
@@ -37,6 +38,7 @@ Frontend Hamroun is a lightweight (~5KB gzipped) JavaScript framework designed f
|
|
37
38
|
- **Complete Hooks API**: useState, useEffect, useRef, useMemo, and more
|
38
39
|
- **Full-Stack Solution**: Unified frontend and backend development
|
39
40
|
- **Server-Side Rendering**: Optimized SSR with hydration
|
41
|
+
- **WebAssembly Integration**: Built-in Go WASM support for high-performance computing
|
40
42
|
- **File-Based Routing**: Intuitive API endpoint creation
|
41
43
|
- **Database Integration**: Built-in support for MongoDB, MySQL, PostgreSQL
|
42
44
|
- **TypeScript Support**: Full type definitions and excellent DX
|
@@ -159,6 +161,44 @@ function Greeting({ name }) {
|
|
159
161
|
}
|
160
162
|
```
|
161
163
|
|
164
|
+
### WebAssembly Integration
|
165
|
+
|
166
|
+
Frontend Hamroun provides first-class support for WebAssembly, particularly with Go:
|
167
|
+
|
168
|
+
```jsx
|
169
|
+
import { useEffect, useState, loadGoWasm } from 'frontend-hamroun';
|
170
|
+
|
171
|
+
function GoCalculator() {
|
172
|
+
const [result, setResult] = useState(0);
|
173
|
+
const [wasm, setWasm] = useState(null);
|
174
|
+
|
175
|
+
// Load the Go WASM module
|
176
|
+
useEffect(() => {
|
177
|
+
async function loadModule() {
|
178
|
+
const instance = await loadGoWasm('/calculator.wasm');
|
179
|
+
setWasm(instance);
|
180
|
+
}
|
181
|
+
loadModule();
|
182
|
+
}, []);
|
183
|
+
|
184
|
+
// Call Go functions from JavaScript
|
185
|
+
const calculate = () => {
|
186
|
+
if (wasm) {
|
187
|
+
// Call the Add function defined in Go
|
188
|
+
const sum = wasm.functions.goAdd(5, 7);
|
189
|
+
setResult(sum);
|
190
|
+
}
|
191
|
+
};
|
192
|
+
|
193
|
+
return (
|
194
|
+
<div>
|
195
|
+
<button onClick={calculate}>Calculate 5 + 7</button>
|
196
|
+
<p>Result: {result}</p>
|
197
|
+
</div>
|
198
|
+
);
|
199
|
+
}
|
200
|
+
```
|
201
|
+
|
162
202
|
## 🎨 Frontend Features
|
163
203
|
|
164
204
|
### Hooks System
|
@@ -781,6 +821,276 @@ export async function get(req, res) {
|
|
781
821
|
```
|
782
822
|
</details>
|
783
823
|
|
824
|
+
## 📦 WebAssembly Integration
|
825
|
+
|
826
|
+
Frontend Hamroun includes built-in support for Go WebAssembly modules, allowing you to leverage Go's performance benefits in your JavaScript applications:
|
827
|
+
|
828
|
+
<details>
|
829
|
+
<summary><b>Loading Go WASM Modules</b></summary>
|
830
|
+
|
831
|
+
```jsx
|
832
|
+
import { useState, useEffect } from 'frontend-hamroun';
|
833
|
+
import { loadGoWasm } from 'frontend-hamroun';
|
834
|
+
|
835
|
+
function GoWasmComponent() {
|
836
|
+
const [wasmModule, setWasmModule] = useState(null);
|
837
|
+
const [loading, setLoading] = useState(true);
|
838
|
+
const [error, setError] = useState(null);
|
839
|
+
const [result, setResult] = useState(null);
|
840
|
+
|
841
|
+
// Load the WASM module on component mount
|
842
|
+
useEffect(() => {
|
843
|
+
async function loadWasmModule() {
|
844
|
+
try {
|
845
|
+
const instance = await loadGoWasm('/go-module.wasm', {
|
846
|
+
goWasmPath: '/wasm_exec.js', // Path to the Go runtime JS
|
847
|
+
debug: true // Enable debug logging
|
848
|
+
});
|
849
|
+
|
850
|
+
setWasmModule(instance);
|
851
|
+
setLoading(false);
|
852
|
+
} catch (err) {
|
853
|
+
console.error('Failed to load WASM module:', err);
|
854
|
+
setError(err.message);
|
855
|
+
setLoading(false);
|
856
|
+
}
|
857
|
+
}
|
858
|
+
|
859
|
+
loadWasmModule();
|
860
|
+
}, []);
|
861
|
+
|
862
|
+
// Call a Go function
|
863
|
+
const handleCalculate = () => {
|
864
|
+
if (!wasmModule) return;
|
865
|
+
|
866
|
+
// Call the Go function
|
867
|
+
const sum = wasmModule.functions.goAdd(5, 7);
|
868
|
+
setResult(sum);
|
869
|
+
};
|
870
|
+
|
871
|
+
if (loading) return <div>Loading WebAssembly module...</div>;
|
872
|
+
if (error) return <div>Error: {error}</div>;
|
873
|
+
|
874
|
+
return (
|
875
|
+
<div>
|
876
|
+
<h2>Go WebAssembly Example</h2>
|
877
|
+
<button onClick={handleCalculate}>Calculate 5 + 7</button>
|
878
|
+
{result !== null && <p>Result: {result}</p>}
|
879
|
+
</div>
|
880
|
+
);
|
881
|
+
}
|
882
|
+
```
|
883
|
+
</details>
|
884
|
+
|
885
|
+
<details>
|
886
|
+
<summary><b>Creating Go WASM Modules</b></summary>
|
887
|
+
|
888
|
+
```go
|
889
|
+
// example.go
|
890
|
+
//go:build js && wasm
|
891
|
+
// +build js,wasm
|
892
|
+
|
893
|
+
package main
|
894
|
+
|
895
|
+
import (
|
896
|
+
"encoding/json"
|
897
|
+
"fmt"
|
898
|
+
"syscall/js"
|
899
|
+
)
|
900
|
+
|
901
|
+
// Function to be called from JavaScript
|
902
|
+
func add(this js.Value, args []js.Value) interface{} {
|
903
|
+
if len(args) != 2 {
|
904
|
+
return js.ValueOf("Error: Expected two arguments")
|
905
|
+
}
|
906
|
+
|
907
|
+
a := args[0].Int()
|
908
|
+
b := args[1].Int()
|
909
|
+
return js.ValueOf(a + b)
|
910
|
+
}
|
911
|
+
|
912
|
+
// Processing complex data
|
913
|
+
func processData(this js.Value, args []js.Value) interface{} {
|
914
|
+
if len(args) == 0 {
|
915
|
+
return js.ValueOf("Error: Expected at least one argument")
|
916
|
+
}
|
917
|
+
|
918
|
+
// Get input data as JS object
|
919
|
+
data := args[0]
|
920
|
+
jsonStr := js.Global().Get("JSON").Call("stringify", data).String()
|
921
|
+
|
922
|
+
// Parse JSON into Go structure
|
923
|
+
var inputMap map[string]interface{}
|
924
|
+
json.Unmarshal([]byte(jsonStr), &inputMap)
|
925
|
+
|
926
|
+
// Process data
|
927
|
+
inputMap["processed"] = true
|
928
|
+
inputMap["processor"] = "Go WASM"
|
929
|
+
|
930
|
+
// Add computed values
|
931
|
+
if values, ok := inputMap["values"].([]interface{}); ok {
|
932
|
+
sum := 0.0
|
933
|
+
for _, v := range values {
|
934
|
+
if num, ok := v.(float64); ok {
|
935
|
+
sum += num
|
936
|
+
}
|
937
|
+
}
|
938
|
+
inputMap["sum"] = sum
|
939
|
+
}
|
940
|
+
|
941
|
+
// Convert back to JSON
|
942
|
+
resultJSON, _ := json.Marshal(inputMap)
|
943
|
+
return js.ValueOf(string(resultJSON))
|
944
|
+
}
|
945
|
+
|
946
|
+
func main() {
|
947
|
+
// Register functions to be callable from JavaScript
|
948
|
+
js.Global().Set("goAdd", js.FuncOf(add))
|
949
|
+
js.Global().Set("goProcessData", js.FuncOf(processData))
|
950
|
+
|
951
|
+
// Keep the program running
|
952
|
+
<-make(chan bool)
|
953
|
+
}
|
954
|
+
```
|
955
|
+
|
956
|
+
```bash
|
957
|
+
# Compile the Go code to WebAssembly
|
958
|
+
GOOS=js GOARCH=wasm go build -o public/go-module.wasm example.go
|
959
|
+
|
960
|
+
# On Windows
|
961
|
+
set GOOS=js
|
962
|
+
set GOARCH=wasm
|
963
|
+
go build -o public/go-module.wasm example.go
|
964
|
+
```
|
965
|
+
|
966
|
+
```bash
|
967
|
+
# Copy the necessary Go WASM runtime
|
968
|
+
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" public/
|
969
|
+
```
|
970
|
+
</details>
|
971
|
+
|
972
|
+
<details>
|
973
|
+
<summary><b>Handling Complex Data with Go WASM</b></summary>
|
974
|
+
|
975
|
+
```jsx
|
976
|
+
import { useState, useEffect } from 'frontend-hamroun';
|
977
|
+
import { loadGoWasm, createTypedWasmFunction } from 'frontend-hamroun';
|
978
|
+
|
979
|
+
function DataProcessor() {
|
980
|
+
const [wasmModule, setWasmModule] = useState(null);
|
981
|
+
const [inputData, setInputData] = useState({
|
982
|
+
name: 'Test Data',
|
983
|
+
values: [10, 20, 30, 40, 50],
|
984
|
+
timestamp: new Date().toISOString()
|
985
|
+
});
|
986
|
+
const [processedData, setProcessedData] = useState(null);
|
987
|
+
|
988
|
+
// Load WASM module
|
989
|
+
useEffect(() => {
|
990
|
+
async function loadModule() {
|
991
|
+
const instance = await loadGoWasm('/go-module.wasm');
|
992
|
+
setWasmModule(instance);
|
993
|
+
}
|
994
|
+
loadModule();
|
995
|
+
}, []);
|
996
|
+
|
997
|
+
// Process data using Go
|
998
|
+
const handleProcessData = () => {
|
999
|
+
if (!wasmModule) return;
|
1000
|
+
|
1001
|
+
// Create a typed function wrapper for better TypeScript support
|
1002
|
+
const processData = createTypedWasmFunction<(data: any) => string>(
|
1003
|
+
wasmModule,
|
1004
|
+
'goProcessData'
|
1005
|
+
);
|
1006
|
+
|
1007
|
+
// Call the Go function
|
1008
|
+
const result = processData(inputData);
|
1009
|
+
setProcessedData(JSON.parse(result));
|
1010
|
+
};
|
1011
|
+
|
1012
|
+
return (
|
1013
|
+
<div>
|
1014
|
+
<h2>Data Processing with Go WASM</h2>
|
1015
|
+
<div>
|
1016
|
+
<h3>Input Data</h3>
|
1017
|
+
<pre>{JSON.stringify(inputData, null, 2)}</pre>
|
1018
|
+
</div>
|
1019
|
+
|
1020
|
+
<button
|
1021
|
+
onClick={handleProcessData}
|
1022
|
+
disabled={!wasmModule}
|
1023
|
+
>
|
1024
|
+
Process Data
|
1025
|
+
</button>
|
1026
|
+
|
1027
|
+
{processedData && (
|
1028
|
+
<div>
|
1029
|
+
<h3>Processed Result</h3>
|
1030
|
+
<pre>{JSON.stringify(processedData, null, 2)}</pre>
|
1031
|
+
</div>
|
1032
|
+
)}
|
1033
|
+
</div>
|
1034
|
+
);
|
1035
|
+
}
|
1036
|
+
```
|
1037
|
+
</details>
|
1038
|
+
|
1039
|
+
<details>
|
1040
|
+
<summary><b>Server-Side WASM Integration</b></summary>
|
1041
|
+
|
1042
|
+
```js
|
1043
|
+
import { loadGoWasmFromFile } from 'frontend-hamroun/server';
|
1044
|
+
import path from 'path';
|
1045
|
+
|
1046
|
+
// API endpoint using Go WASM for computation
|
1047
|
+
export async function get(req, res) {
|
1048
|
+
try {
|
1049
|
+
// Load the WASM module on the server
|
1050
|
+
const wasmPath = path.join(process.cwd(), 'wasm', 'calculator.wasm');
|
1051
|
+
const wasm = await loadGoWasmFromFile(wasmPath);
|
1052
|
+
|
1053
|
+
// Process data using the Go WASM module
|
1054
|
+
const input = {
|
1055
|
+
values: [10, 20, 30, 40, 50],
|
1056
|
+
operation: 'sum'
|
1057
|
+
};
|
1058
|
+
|
1059
|
+
const result = wasm.functions.goProcessData(input);
|
1060
|
+
|
1061
|
+
// Return the processed data
|
1062
|
+
res.json(JSON.parse(result));
|
1063
|
+
} catch (error) {
|
1064
|
+
res.status(500).json({ error: error.message });
|
1065
|
+
}
|
1066
|
+
}
|
1067
|
+
```
|
1068
|
+
</details>
|
1069
|
+
|
1070
|
+
<details>
|
1071
|
+
<summary><b>CLI for Go WASM Development</b></summary>
|
1072
|
+
|
1073
|
+
```bash
|
1074
|
+
# Create a new Go WASM module
|
1075
|
+
npx frontend-hamroun add:wasm calculator
|
1076
|
+
|
1077
|
+
# This will:
|
1078
|
+
# 1. Create calculator.go with basic functions
|
1079
|
+
# 2. Add build scripts for compiling to WASM
|
1080
|
+
# 3. Display usage instructions
|
1081
|
+
|
1082
|
+
# Then compile and use your module
|
1083
|
+
cd src/wasm
|
1084
|
+
./build.sh # or build.bat on Windows
|
1085
|
+
|
1086
|
+
# Import in your code
|
1087
|
+
import { loadGoWasm } from 'frontend-hamroun';
|
1088
|
+
|
1089
|
+
const wasm = await loadGoWasm('/wasm/calculator.wasm');
|
1090
|
+
const result = wasm.functions.goAdd(5, 7);
|
1091
|
+
```
|
1092
|
+
</details>
|
1093
|
+
|
784
1094
|
## 🔄 Server-Side Rendering
|
785
1095
|
|
786
1096
|
Frontend Hamroun provides built-in server-side rendering capabilities to improve performance and SEO:
|
@@ -851,6 +1161,7 @@ Available templates:
|
|
851
1161
|
- `basic-app`: Minimal client-side setup
|
852
1162
|
- `ssr-template`: Server-side rendering with hydration
|
853
1163
|
- `fullstack-app`: Complete solution with frontend, backend, auth, and DB
|
1164
|
+
- `go-wasm-app`: Application with Go WebAssembly integration
|
854
1165
|
</details>
|
855
1166
|
|
856
1167
|
<details>
|
@@ -880,6 +1191,22 @@ npx frontend-hamroun add:api orders --methods=get,post --auth
|
|
880
1191
|
```
|
881
1192
|
</details>
|
882
1193
|
|
1194
|
+
<details>
|
1195
|
+
<summary><b>WebAssembly Generation</b></summary>
|
1196
|
+
|
1197
|
+
```bash
|
1198
|
+
# Generate a new Go WASM module
|
1199
|
+
npx frontend-hamroun add:wasm calculator
|
1200
|
+
|
1201
|
+
# Specify path
|
1202
|
+
npx frontend-hamroun add:wasm data-processor --path=src/wasm
|
1203
|
+
|
1204
|
+
# Select module capabilities
|
1205
|
+
npx frontend-hamroun add:wasm math-lib
|
1206
|
+
# (Interactive prompt will ask for module features)
|
1207
|
+
```
|
1208
|
+
</details>
|
1209
|
+
|
883
1210
|
## 🧩 Advanced Usage
|
884
1211
|
|
885
1212
|
<details>
|
@@ -1236,6 +1563,32 @@ While Frontend Hamroun's API is similar to React's, they have different internal
|
|
1236
1563
|
Yes, Frontend Hamroun provides tools for static site generation. Use the `renderToString` API to pre-render pages at build time.
|
1237
1564
|
</details>
|
1238
1565
|
|
1566
|
+
<details>
|
1567
|
+
<summary><b>Why integrate Go with WebAssembly?</b></summary>
|
1568
|
+
|
1569
|
+
Go with WebAssembly provides several benefits:
|
1570
|
+
- **Performance**: Computation-heavy tasks run much faster than in JavaScript
|
1571
|
+
- **Reuse existing code**: Leverage Go libraries and codebases in the browser
|
1572
|
+
- **Type safety**: Go's strong typing helps prevent runtime errors
|
1573
|
+
- **Concurrency**: Utilize Go's powerful concurrency features
|
1574
|
+
- **Consistent behavior**: Same code runs identically in browser and on server
|
1575
|
+
|
1576
|
+
Frontend Hamroun makes this integration seamless with dedicated APIs to load and interact with Go WASM modules.
|
1577
|
+
</details>
|
1578
|
+
|
1579
|
+
<details>
|
1580
|
+
<summary><b>What's the performance impact of using Go WASM?</b></summary>
|
1581
|
+
|
1582
|
+
While WebAssembly is generally faster than JavaScript for CPU-intensive tasks, there are considerations:
|
1583
|
+
|
1584
|
+
- **Initial load**: WASM modules add to your bundle size (though they can be loaded on-demand)
|
1585
|
+
- **Memory usage**: Go's runtime in WASM uses more memory than hand-written WASM
|
1586
|
+
- **Startup time**: The Go runtime has some initialization overhead
|
1587
|
+
- **Computation speed**: For heavy calculations, Go WASM can be 2-10x faster than equivalent JS
|
1588
|
+
|
1589
|
+
Best practice is to use Go WASM for specific performance-critical parts of your application rather than for the entire application.
|
1590
|
+
</details>
|
1591
|
+
|
1239
1592
|
## 📄 License
|
1240
1593
|
|
1241
1594
|
MIT © Hamroun
|
package/bin/cli.js
CHANGED
@@ -149,15 +149,13 @@ async function chooseTemplate() {
|
|
149
149
|
'basic-app': '🚀',
|
150
150
|
'ssr-template': '🌐',
|
151
151
|
'fullstack-app': '⚡',
|
152
|
-
'go-wasm-app': '🔄',
|
153
152
|
};
|
154
153
|
|
155
154
|
// Detailed descriptions
|
156
155
|
const templateDescriptions = {
|
157
156
|
'basic-app': 'Single-page application with just the essentials. Perfect for learning the framework or building simple apps.',
|
158
157
|
'ssr-template': 'Server-side rendered application with hydration. Optimized for SEO and fast initial load.',
|
159
|
-
'fullstack-app': 'Complete solution with API routes, authentication, and database integration ready to go.'
|
160
|
-
'go-wasm-app': 'WebAssembly integration with Go for high-performance computing in the browser and Node.js.'
|
158
|
+
'fullstack-app': 'Complete solution with API routes, authentication, and database integration ready to go.'
|
161
159
|
};
|
162
160
|
|
163
161
|
console.log(boxen(
|
@@ -167,8 +165,7 @@ async function chooseTemplate() {
|
|
167
165
|
const shortDesc = {
|
168
166
|
'basic-app': 'Simple client-side application',
|
169
167
|
'ssr-template': 'Server-side rendering with hydration',
|
170
|
-
'fullstack-app': 'Complete fullstack application with API'
|
171
|
-
'go-wasm-app': 'Go WebAssembly application'
|
168
|
+
'fullstack-app': 'Complete fullstack application with API'
|
172
169
|
}[template] || 'Application template';
|
173
170
|
|
174
171
|
return `${icon} ${chalk.cyan(template)}\n ${chalk.dim(shortDesc)}`;
|
@@ -256,23 +253,6 @@ async function chooseTemplate() {
|
|
256
253
|
'• Full production applications',
|
257
254
|
'• Apps needing authentication',
|
258
255
|
'• Projects requiring database integration'
|
259
|
-
],
|
260
|
-
'go-wasm-app': [
|
261
|
-
`${chalk.bold('Go WASM App Template')} ${templateIcons['go-wasm-app']}`,
|
262
|
-
'',
|
263
|
-
`${chalk.dim('WebAssembly integration with Go programming language.')}`,
|
264
|
-
'',
|
265
|
-
`${chalk.yellow('Features:')}`,
|
266
|
-
'• Go + WebAssembly integration',
|
267
|
-
'• High-performance computation in browser',
|
268
|
-
'• Server-side WASM processing',
|
269
|
-
'• Shared code between Go and JavaScript',
|
270
|
-
'• Optimized build pipeline',
|
271
|
-
'',
|
272
|
-
`${chalk.yellow('Best for:')}`,
|
273
|
-
'• Computation-heavy applications',
|
274
|
-
'• Projects requiring Go libraries',
|
275
|
-
'• Performance-critical features'
|
276
256
|
]
|
277
257
|
}[answers.template] || ['No detailed information available for this template'];
|
278
258
|
|
package/dist/index.d.ts
CHANGED
@@ -4,11 +4,11 @@ export { batchUpdates } from './batch';
|
|
4
4
|
export { jsx, jsxs, Fragment } from './jsx-runtime';
|
5
5
|
export { render, hydrate } from './renderer';
|
6
6
|
export { renderToString } from './server-renderer';
|
7
|
-
export { loadGoWasm, createTypedWasmFunction, goValues } from './wasm';
|
8
|
-
export type { GoWasmInstance, GoWasmOptions } from './wasm';
|
9
7
|
export type { Context } from './context';
|
10
8
|
export type { VNode } from './types';
|
11
9
|
export type { Server, ServerConfig, User, DbConfig, MiddlewareFunction } from './server-types';
|
10
|
+
export { loadGoWasm, createTypedWasmFunction, goValues } from './wasm';
|
11
|
+
export type { GoWasmInstance, GoWasmOptions } from './wasm';
|
12
12
|
export declare const server: {
|
13
|
-
getServer(): Promise<
|
13
|
+
getServer(): Promise<any>;
|
14
14
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "frontend-hamroun",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.68",
|
4
4
|
"description": "A lightweight full-stack JavaScript framework",
|
5
5
|
"type": "module",
|
6
6
|
"main": "./dist/index.js",
|
@@ -69,6 +69,16 @@
|
|
69
69
|
"types": "./dist/renderer.d.ts",
|
70
70
|
"import": "./dist/renderer.js",
|
71
71
|
"require": "./dist/renderer.js"
|
72
|
+
},
|
73
|
+
"./wasm": {
|
74
|
+
"types": "./dist/wasm.d.ts",
|
75
|
+
"import": "./dist/wasm.js",
|
76
|
+
"require": "./dist/wasm.js"
|
77
|
+
},
|
78
|
+
"./server/wasm": {
|
79
|
+
"types": "./dist/server/wasm.d.ts",
|
80
|
+
"import": "./dist/server/wasm.js",
|
81
|
+
"require": "./dist/server/wasm.js"
|
72
82
|
}
|
73
83
|
},
|
74
84
|
"bin": {
|
@@ -1,20 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"name": "go-wasm-app",
|
3
|
-
"version": "1.0.0",
|
4
|
-
"description": "WebAssembly integration with Go for Frontend Hamroun",
|
5
|
-
"type": "module",
|
6
|
-
"main": "index.js",
|
7
|
-
"scripts": {
|
8
|
-
"build:wasm": "node build-wasm.js",
|
9
|
-
"dev": "npm run build:wasm && vite",
|
10
|
-
"build": "npm run build:wasm && vite build",
|
11
|
-
"serve": "vite preview",
|
12
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
13
|
-
},
|
14
|
-
"dependencies": {
|
15
|
-
"frontend-hamroun": "^1.2.66"
|
16
|
-
},
|
17
|
-
"devDependencies": {
|
18
|
-
"vite": "^4.4.9"
|
19
|
-
}
|
20
|
-
}
|
package/templates/go/README.md
DELETED
@@ -1,150 +0,0 @@
|
|
1
|
-
# Go WebAssembly Integration
|
2
|
-
|
3
|
-
This directory contains tools and examples for integrating Go with Frontend Hamroun via WebAssembly (WASM).
|
4
|
-
|
5
|
-
## Getting Started
|
6
|
-
|
7
|
-
### Prerequisites
|
8
|
-
|
9
|
-
1. Go 1.16+ installed on your system
|
10
|
-
2. Basic understanding of Go programming language
|
11
|
-
|
12
|
-
### Compiling Go to WebAssembly
|
13
|
-
|
14
|
-
To compile a Go file to WebAssembly for browser usage:
|
15
|
-
|
16
|
-
```bash
|
17
|
-
GOOS=js GOARCH=wasm go build -o mymodule.wasm myfile.go
|
18
|
-
```
|
19
|
-
|
20
|
-
For optimized production builds:
|
21
|
-
|
22
|
-
```bash
|
23
|
-
GOOS=js GOARCH=wasm go build -ldflags="-s -w" -o mymodule.wasm myfile.go
|
24
|
-
```
|
25
|
-
|
26
|
-
### Required Files
|
27
|
-
|
28
|
-
Copy these files to your project:
|
29
|
-
|
30
|
-
1. `wasm_exec.js` - for browser usage
|
31
|
-
2. `wasm_exec_node.js` - for Node.js (server-side) usage
|
32
|
-
|
33
|
-
You can find these files in your Go installation:
|
34
|
-
- Browser: `$(go env GOROOT)/misc/wasm/wasm_exec.js`
|
35
|
-
- Node.js: `$(go env GOROOT)/misc/wasm/wasm_exec_node.js`
|
36
|
-
|
37
|
-
## Usage Examples
|
38
|
-
|
39
|
-
### Browser Usage
|
40
|
-
|
41
|
-
```jsx
|
42
|
-
import { loadGoWasm, createTypedWasmFunction } from 'frontend-hamroun';
|
43
|
-
|
44
|
-
function GoCalculator() {
|
45
|
-
const [result, setResult] = useState(0);
|
46
|
-
const [wasmInstance, setWasmInstance] = useState(null);
|
47
|
-
|
48
|
-
useEffect(() => {
|
49
|
-
async function loadWasm() {
|
50
|
-
const instance = await loadGoWasm('/mymodule.wasm', {
|
51
|
-
goWasmPath: '/wasm_exec.js', // Path to the Go runtime JS file
|
52
|
-
debug: true
|
53
|
-
});
|
54
|
-
|
55
|
-
setWasmInstance(instance);
|
56
|
-
}
|
57
|
-
|
58
|
-
loadWasm();
|
59
|
-
}, []);
|
60
|
-
|
61
|
-
const handleCalculate = () => {
|
62
|
-
if (wasmInstance) {
|
63
|
-
// Call Go function from WASM
|
64
|
-
const goAdd = createTypedWasmFunction<(a: number, b: number) => number>(
|
65
|
-
wasmInstance,
|
66
|
-
'goAdd'
|
67
|
-
);
|
68
|
-
|
69
|
-
const sum = goAdd(5, 7);
|
70
|
-
setResult(sum);
|
71
|
-
}
|
72
|
-
};
|
73
|
-
|
74
|
-
return (
|
75
|
-
<div>
|
76
|
-
<button onClick={handleCalculate}>Calculate 5 + 7</button>
|
77
|
-
<p>Result: {result}</p>
|
78
|
-
</div>
|
79
|
-
);
|
80
|
-
}
|
81
|
-
```
|
82
|
-
|
83
|
-
### Server Usage
|
84
|
-
|
85
|
-
```js
|
86
|
-
import { loadGoWasmFromFile } from 'frontend-hamroun/server';
|
87
|
-
import path from 'path';
|
88
|
-
|
89
|
-
export async function get(req, res) {
|
90
|
-
const wasmPath = path.join(process.cwd(), 'lib', 'mymodule.wasm');
|
91
|
-
|
92
|
-
try {
|
93
|
-
const wasm = await loadGoWasmFromFile(wasmPath);
|
94
|
-
|
95
|
-
// Call Go function
|
96
|
-
const result = wasm.functions.goProcessData({
|
97
|
-
values: [1, 2, 3, 4, 5],
|
98
|
-
operation: 'sum'
|
99
|
-
});
|
100
|
-
|
101
|
-
res.json(JSON.parse(result));
|
102
|
-
} catch (error) {
|
103
|
-
res.status(500).json({ error: error.message });
|
104
|
-
}
|
105
|
-
}
|
106
|
-
```
|
107
|
-
|
108
|
-
## Advanced Usage
|
109
|
-
|
110
|
-
### Passing Complex Data Between JavaScript and Go
|
111
|
-
|
112
|
-
For complex data types, use JSON serialization:
|
113
|
-
|
114
|
-
```go
|
115
|
-
// In Go
|
116
|
-
func processData(this js.Value, args []js.Value) interface{} {
|
117
|
-
// Get JSON string from JS
|
118
|
-
jsonStr := args[0].String()
|
119
|
-
|
120
|
-
// Parse JSON
|
121
|
-
var data map[string]interface{}
|
122
|
-
json.Unmarshal([]byte(jsonStr), &data)
|
123
|
-
|
124
|
-
// Process data...
|
125
|
-
|
126
|
-
// Return as JSON string
|
127
|
-
resultJSON, _ := json.Marshal(result)
|
128
|
-
return js.ValueOf(string(resultJSON))
|
129
|
-
}
|
130
|
-
```
|
131
|
-
|
132
|
-
```js
|
133
|
-
// In JavaScript
|
134
|
-
const dataToProcess = { items: [1, 2, 3], config: { name: "test" } };
|
135
|
-
const jsonStr = JSON.stringify(dataToProcess);
|
136
|
-
const resultJson = wasmInstance.functions.processData(jsonStr);
|
137
|
-
const result = JSON.parse(resultJson);
|
138
|
-
```
|
139
|
-
|
140
|
-
## Performance Considerations
|
141
|
-
|
142
|
-
- WebAssembly functions run in the same thread as JavaScript
|
143
|
-
- For CPU-intensive tasks, consider using Web Workers
|
144
|
-
- Go's garbage collector can cause occasional pauses
|
145
|
-
- Large WASM files can increase initial load time
|
146
|
-
|
147
|
-
## Resources
|
148
|
-
|
149
|
-
- [Go WebAssembly Wiki](https://github.com/golang/go/wiki/WebAssembly)
|
150
|
-
- [Frontend Hamroun Documentation](https://github.com/hamroun/frontend-hamroun)
|