Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
add es6 module support
Browse files Browse the repository at this point in the history
  • Loading branch information
nobonobo committed May 22, 2020
1 parent 158fe06 commit 6cf4bd5
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 28 deletions.
20 changes: 19 additions & 1 deletion INTERNAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,25 @@
- SetTitle(title string)
- AddMeta(name, content string)
- AddStylesheet(url string)
- AddScript(url string)
- LoadScript(url string)
- LoadModule(names []string, url string) <-chan js.Value

##### LoadModule 例

```go
names := []string{"Server", "Client"}
defs := map[string]js.Value{}
for obj := range wecty.LoadModule(names, "jsonrpclib.js") {
defs[names[0]] = obj
names = names[1:]
}
```

以下の Javascript(module) 記述と等価

```javascript
import { Server , Client } as defs from "jsonrpclib.js";
```

### ルーター

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ import (
Usage of server:
-addr string
listen address (default ":8080")
-p path=endpoint
add reverse proxy rule (allow multiple rules)
-tinygo
use tinygo tool chain
```
Expand All @@ -286,10 +288,24 @@ Usage of server:
- "main.wasm"を要求されるとその該当フォルダで go-generate と WASM のビルド、gzip が行われその結果をサーブします
- index.html が無いところで要求されたら標準的な WASM 読み込み用 HTML を返します
- "wasm_exec.js"リソースが要求されたら適切な wasm_exec.js をサーブします
- 指定パスへのアクセス要求を別の Web サーバーへ転送します(リバースプロキシー)

### コマンドオプション

-tinygo: WASM ビルドに tinygo を使う
- -addr: 開発サーバーのリッスンアドレスの指定
- -p: 転送ルールの追加(複数指定可能)
- -tinygo: WASM ビルドに tinygo を使う

例:

```shell
wecty server -addr :8080 -p /api/=http://localhost:9001
```

バックエンドサーバーを http://localhost:9001 にて動作させた状態で開発を進める場合などで使います。

- http://localhost:8080/api/ へのアクセスは http://localhost:9001/ へ転送されます。
- http://localhost:8080/api/hoge へのアクセスは http://localhost:9001/hoge へ転送されます。

## 出力サイズ

Expand Down
2 changes: 1 addition & 1 deletion cmd/wecty/commands/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (dh *DevHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, fpath)
return
} else {
log.Println(err)
log.Println(err, base)
}
switch base {
case "index.html":
Expand Down
28 changes: 27 additions & 1 deletion examples/jsonrpc/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# JSON-RPC sample

login/logout & JSON-RPC sample

## Startup

Run below commands for each terminals.
Expand All @@ -10,7 +12,31 @@ Run below commands for each terminals.

```shell
> cd frontend
> wecty server -p /rpc=http://localhost:8888
> wecty server -p /rp/c=http://localhost:8888
```

open http://localhost:8080

## URLs

Static:

- Generated By `wecty server`
- index.html
- wasm_exec.js
- main.wasm
- static-files: http://localhost:8080/assets/
- app.css
- jsonrpcclient.js

Views:

- index: http://localhost:8080/#/
- login: http://localhost:8080/#/login

API:

- JSON-RPC endpoint: http://localhost:8080/rpc/ -> http://localhost:8888/
- Service.User(null) -> models.User
- login: POST http://localhost:8080/rpc/login -> http://localhost:8888/login
- logout: POST http://localhost:8080/rpc/logout -> http://localhost:8888/logout
Empty file removed examples/jsonrpc/frontend/index.js
Empty file.
6 changes: 4 additions & 2 deletions examples/jsonrpc/frontend/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"syscall/js"

"github.com/nobonobo/wecty"
"github.com/nobonobo/wecty/examples/jsonrpc/frontend/views"
)
Expand All @@ -10,8 +12,8 @@ func main() {
wecty.AddStylesheet("https://unpkg.com/spectre.css/dist/spectre.min.css")
wecty.AddStylesheet("https://unpkg.com/spectre.css/dist/spectre-exp.min.css")
wecty.AddStylesheet("https://unpkg.com/spectre.css/dist/spectre-icons.min.css")
wecty.AddModule("/assets/jsonrpcclient.js", "JsonRpcClient")
//wecty.Wait("index.js")
obj := <-wecty.LoadModule([]string{"JsonRpcClient"}, "/assets/jsonrpcclient.js")
js.Global().Set("JsonRpcClient", obj)
router := wecty.NewRouter()
router.Handle("/", func(key string) {
wecty.RenderBody(&views.Index{})
Expand Down
20 changes: 20 additions & 0 deletions utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ package wecty

import "syscall/js"

// Callback0 ...
func Callback0(fn func() interface{}) js.Func {
var cb js.Func
cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer cb.Release()
return fn()
})
return cb
}

// Callback1 ...
func Callback1(fn func(res js.Value) interface{}) js.Func {
var cb js.Func
Expand All @@ -11,3 +21,13 @@ func Callback1(fn func(res js.Value) interface{}) js.Func {
})
return cb
}

// CallbackN ...
func CallbackN(fn func(res []js.Value) interface{}) js.Func {
var cb js.Func
cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer cb.Release()
return fn(args)
})
return cb
}
54 changes: 32 additions & 22 deletions wecty.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,37 +396,47 @@ func AddStylesheet(url string) {
document.Get("head").Call("appendChild", link)
}

// AddScript adds an external script to the document.
func AddScript(url string) {
script := document.Call("createElement", "script")
script.Set("src", url)
// LoadScript ...
func LoadScript(url string) {
ch := make(chan bool)
script := Tag("script",
Attr("src", url),
Event("load", func(js.Value) interface{} {
println("load!")
close(ch)
return nil
}),
).Render().html()
document.Get("head").Call("appendChild", script)
<-ch
}

// AddModule adds an external script to the document.
func AddModule(url string, words ...string) {
ch := make(chan bool)
script := document.Call("createElement", "script")
script.Set("type", "module")
global.Set("__callback__", Callback1(func(ev js.Value) interface{} {
// LoadModule ...
func LoadModule(names []string, url string) <-chan js.Value {
ch := make(chan js.Value)
js.Global().Set("__wecty_send__", Callback1(func(obj js.Value) interface{} {
ch <- obj
return nil
}))
js.Global().Set("__wecty_close__", Callback0(func() interface{} {
close(ch)
println("loaded")
return nil
}))
keys := "*"
if len(words) > 0 {
keys = fmt.Sprintf("{ %s }", strings.Join(words, " "))
}
lines := []string{}
for _, v := range words {
lines = append(lines, fmt.Sprintf("window.%[1]s = %[1]s", v))
for _, name := range names {
lines = append(lines, fmt.Sprintf("__wecty_send__(%s);", name))
}
lines = append(lines, "__callback__(null)")
imp := strings.Join(lines, ";\n")
t := document.Call("createTextNode", fmt.Sprintf("import %s from %q;\n%s", keys, url, imp))
script.Call("appendChild", t)
lines = append(lines, "__wecty_close__();")
script := Tag("script",
Attr("type", "module"),
Text(fmt.Sprintf("import { %s } from %q;\n%s",
strings.Join(names, ", "),
url,
strings.Join(lines, "\n"),
)),
).Render().html()
document.Get("head").Call("appendChild", script)
<-ch
return ch
}

// RequestAnimationFrame ...
Expand Down

0 comments on commit 6cf4bd5

Please sign in to comment.