js 目标工程增加 www 目录

This commit is contained in:
chai2010
2023-11-22 08:24:16 +08:00
parent b6b0a2a257
commit d408ac5115
4 changed files with 77 additions and 9 deletions

View File

@@ -3,7 +3,9 @@
package appbase
import (
"io"
"os"
"path/filepath"
"strings"
"unicode"
)
@@ -95,4 +97,48 @@ func IsValidPkgpath(s string) bool {
return IsValidAppName(pkgname)
}
// 是标准库路径
// 复制目录
func CopyDir(dst, src string) (err error) {
entryList, err := os.ReadDir(src)
if err != nil && !os.IsExist(err) {
return err
}
for _, entry := range entryList {
if entry.IsDir() {
if err = CopyDir(dst+"/"+entry.Name(), src+"/"+entry.Name()); err != nil {
return err
}
} else {
srcFname := filepath.Clean(src + "/" + entry.Name())
dstFname := filepath.Clean(dst + "/" + entry.Name())
if err := CopyFile(dstFname, srcFname); err != nil {
return err
}
}
}
return nil
}
// 复制文件
func CopyFile(dst, src string) error {
err := os.MkdirAll(filepath.Dir(dst), 0777)
if err != nil && !os.IsExist(err) {
return err
}
fsrc, err := os.Open(src)
if err != nil {
return err
}
defer fsrc.Close()
fdst, err := os.Create(dst)
if err != nil {
return err
}
defer fdst.Close()
if _, err = io.Copy(fdst, fsrc); err != nil {
return err
}
return nil
}

View File

@@ -182,14 +182,22 @@ func BuildApp(opt *appbase.Option, input, outfile string) (wasmBytes []byte, err
os.Exit(1)
}
// 生成 index.html 文件
indexHtmlPath := filepath.Join(filepath.Dir(outfile), "index.html")
if !appbase.PathExists(indexHtmlPath) {
htmlOutput := compiler.GenIndexHtml(filepath.Base(jsOutfile))
err = os.WriteFile(indexHtmlPath, []byte(htmlOutput), 0666)
if err != nil {
fmt.Printf("write %s failed: %v\n", indexHtmlPath, err)
os.Exit(1)
// 复制 www 目录
if appbase.IsNativeDir(filepath.Join(manifest.Root, "www")) {
appbase.CopyDir(
filepath.Join(manifest.Root, "output"),
filepath.Join(manifest.Root, "www"),
)
} else {
// 生成 index.html 文件
indexHtmlPath := filepath.Join(filepath.Dir(outfile), "index.html")
if !appbase.PathExists(indexHtmlPath) {
htmlOutput := compiler.GenIndexHtml(filepath.Base(jsOutfile))
err = os.WriteFile(indexHtmlPath, []byte(htmlOutput), 0666)
if err != nil {
fmt.Printf("write %s failed: %v\n", indexHtmlPath, err)
os.Exit(1)
}
}
}
}

View File

@@ -6,6 +6,10 @@ func main {
heart()
}
func GetSomething => string {
return "你好,凹语言!"
}
func sum(n: int) => int {
v: int
for i := 1; i <= n; i++ {

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html><title>myapp</title>
<h1>你好,凹语言</h1>
<script type="text/javascript" src="./{{.Name}}.js"></script>
<script>
// let result = waApp.GetSomething();
// console.log(result);
</script>