feat(js): error when GOROOT not found, add dev server to example

master
ALI Hamza 2021-03-21 09:28:47 +07:00
parent f19165c53d
commit ef20dff682
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
12 changed files with 8438 additions and 722 deletions

1
.gitignore vendored

@ -1,3 +1,4 @@
node_modules node_modules
example/node_modules example/node_modules
.gocache .gocache
wasm_exec.js

@ -0,0 +1,3 @@
example
wasm
.gocache

@ -0,0 +1,4 @@
dist/main.wasm
dist/main.js
node_modules
src/api/main.wasm

File diff suppressed because one or more lines are too long

@ -1,6 +1,2 @@
gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320160445-87f4b612f08a h1:yP+ZWeFYr26sQbkjOZKlJvDdPNzxsdK3T9WeDDpRZ3Y=
gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320160445-87f4b612f08a/go.mod h1:aXGSS5eQ84RFpINcZVu4y/MeaguTCZM9RvZct1OtsE0=
gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320171600-df451aa899a5 h1:1obgUBM+Sx9Hi+2nv05MvPBR8qu/0ZDCRs8JIky8U2o=
gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320171600-df451aa899a5/go.mod h1:aXGSS5eQ84RFpINcZVu4y/MeaguTCZM9RvZct1OtsE0=
gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320172655-205806929b8f h1:A1ImTmMMUKGRvxCXO+hQh92DAsRz37ioQGlg8dpxFjc= gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320172655-205806929b8f h1:A1ImTmMMUKGRvxCXO+hQh92DAsRz37ioQGlg8dpxFjc=
gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320172655-205806929b8f/go.mod h1:aXGSS5eQ84RFpINcZVu4y/MeaguTCZM9RvZct1OtsE0= gitea.teamortix.com/Team-Ortix/go-mod-wasm/wasm v0.0.0-20210320172655-205806929b8f/go.mod h1:aXGSS5eQ84RFpINcZVu4y/MeaguTCZM9RvZct1OtsE0=

File diff suppressed because it is too large Load Diff

@ -5,16 +5,16 @@
"private": true, "private": true,
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "GOROOT=`go env GOROOT` webpack" "start": "GOROOT=`go env GOROOT` webpack serve"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"webpack": "^5.27.0", "webpack": "^5.27.0",
"webpack-cli": "^4.5.0" "webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.11.2"
}, },
"dependencies": { "dependencies": {
"crypto-browserify": "^3.12.0", "crypto-browserify": "^3.12.0"
"loader-utils": "^2.0.0"
} }
} }

@ -8,7 +8,9 @@ import (
func main() { func main() {
c := make(chan bool, 0) c := make(chan bool, 0)
fmt.Println("Hello Go!")
fmt.Println("Hello from go-mod-wasm!")
wasm.Ready() wasm.Ready()
<-c <-c
} }

@ -1,6 +1,5 @@
import wasm from './main.go'; import wasm from './api/main.go';
(async () => { (async () => {
console.log(wasm)
console.log(await wasm.test(), "..")
})() })()

@ -2,6 +2,11 @@ const path = require('path');
module.exports = { module.exports = {
entry: './src/index.js', entry: './src/index.js',
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
compress: true,
port: 3000,
},
mode: "production", mode: "production",
output: { output: {
filename: 'main.js', filename: 'main.js',
@ -36,4 +41,14 @@ module.exports = {
} }
] ]
}, },
performance: {
assetFilter: (file) => {
return !/\.wasm/.test(file)
}
},
ignoreWarnings: [
{
module: /wasm_exec.js$/
}
]
}; };

@ -1,8 +1,7 @@
const fs = require("fs") const fs = require("fs")
const { execSync, execFileSync } = require("child_process") const { execFileSync } = require("child_process")
const path = require("path") const path = require("path")
const { lookpath } = require("lookpath") const { lookpath } = require("lookpath")
const { executionAsyncResource } = require("async_hooks")
const exists = async (dir, file) => { const exists = async (dir, file) => {
return new Promise((res, rej) => { return new Promise((res, rej) => {
@ -23,6 +22,11 @@ module.exports = function (source) {
return cb(new Error("go bin not found in path.")); return cb(new Error("go bin not found in path."));
} }
if (!process.env.GOROOT) {
return cb(new Error("Could not find GOROOT in environment.\n" +
"Please try adding this to your script:\nGOROOT=`go env GOROOT` npm run ..."))
}
const parent = path.dirname(this.resourcePath) const parent = path.dirname(this.resourcePath)
const outFile = this.resourcePath.slice(0, -2) + "wasm" const outFile = this.resourcePath.slice(0, -2) + "wasm"
let modDir = parent let modDir = parent
@ -59,7 +63,11 @@ module.exports = function (source) {
return cb(e) return cb(e)
} }
const wasmPath = path.join(process.env.GOROOT, "misc", "wasm", "wasm_exec.js") const wasmOrigPath = path.join(process.env.GOROOT, "misc", "wasm", "wasm_exec.js")
const wasmEmitPath = path.join(__dirname, 'wasm_exec.js')
if (!(await exists(__dirname, 'wasm_exec.js'))) {
fs.copyFileSync(wasmOrigPath, wasmEmitPath)
}
let contents = fs.readFileSync(outFile) let contents = fs.readFileSync(outFile)
fs.unlinkSync(outFile) fs.unlinkSync(outFile)
@ -69,7 +77,7 @@ module.exports = function (source) {
this.addContextDependency(modDir) this.addContextDependency(modDir)
cb(null, cb(null,
`require('!${wasmPath}') `require('!${wasmEmitPath}')
import goWasm from '${path.join(__dirname, 'bridge.js')}'; import goWasm from '${path.join(__dirname, 'bridge.js')}';
const wasm = fetch('${emitPath}').then(response => response.arrayBuffer()) const wasm = fetch('${emitPath}').then(response => response.arrayBuffer())

@ -1,11 +1,8 @@
{ {
"name": "go-mod-wasm", "name": "go-mod-wasm",
"version": "0.0.1", "version": "0.1.2",
"description": "whatever", "description": "A webpack-based configuration to work with wasm using Go.",
"main": "index.js", "main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitea.teamortix.com/Team-Ortix/go-mod-wasm" "url": "https://gitea.teamortix.com/Team-Ortix/go-mod-wasm"