Golang自动生成js(React&Preact)DOM包

博客分类: golang 阅读次数: comments

Golang自动生成js(React&Preact)DOM包

joy 官方介绍

在每个Chrome浏览器中,使用Go的类型系统生成简洁的Javascript 建立大型的web 应用程序。

这句话好绕😅 算了我还是贴原文:

Translate idiomatic Go into concise Javascript that works in every browser. Use Go’s type system and world-class tooling to build large web applications with confidence.

介绍:

Today I’m excited to share the Joy compiler with the developer community. Joy brings Go’s simple design and excellent tooling to the frontend. I believe Joy will become the most productive way to build large-scale, maintainable web applications. I hope you’ll find Joy delightful as I do.

go test -v 运行所有的测试

# 官方全部例子
访问https://mat.tm/joy/#examples或仔细阅读测试栗子。

编译进入``Javascript``:

joy

编译并运行无头Chrome浏览器中的Go代码:

joy run


构建代码的开发版本:

joy build –dev ...


构建代码的生产版本(即将推出!):

joy build ...


使用``livereload``启动开发服务器:

joy serve ...


运行``joy help``其他细节。

- 使用golang生成JavaScript栗子
- 
```golang
package main

import (
  "github.com/matthewmueller/joy/dom/htmlbodyelement"
  "github.com/matthewmueller/joy/dom/window"
)

func main() {
  document := window.New().Document()

  a := document.CreateElement("a")
  println(a.NodeName())
  strong := document.CreateElement("strong")
  println(document.CreateElement("strong").OuterHTML())
  a.AppendChild(strong)

  strong.SetTextContent("hi world!")

  body := document.Body().(*htmlbodyelement.HTMLBodyElement)
  body.AppendChild(a)
  println(document.Body().OuterHTML())
}

链接和提示:

我一直在使用这个来弄清楚如何构建JS树https://astexplorer.net: 简单的Go AST浏览器http://goast.yuroyoro.net/ ES3 AST格式这是在syntax.gohttps://github.com/estree/estree/blob/master/es5.md 需要参考这个来看看Go的AST中可能的类型https://golang.org/ref/spec 运行所有测试: go test -v 运行个人测试:go test -v -run Test/08 pretty.Println(ast)会漂亮的打印JS AST(需要这个包) ast.Print(nil, node)会很漂亮的打印Go AST