aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai WU <kaiwu2004@gmail.com>2024-07-20 10:59:38 +0800
committerKai WU <kaiwu2004@gmail.com>2024-07-20 10:59:38 +0800
commit75cedce799148b8e2541fee1ea5bd4d5c1cfe4c2 (patch)
treec4a41483c9b17e36c548813696e5e1f91d4fd3ac
parent8151853546507050f00fa4fe9032752b4320e595 (diff)
downloadwechat_dev_tools-75cedce799148b8e2541fee1ea5bd4d5c1cfe4c2.tar.gz
wechat_dev_tools-75cedce799148b8e2541fee1ea5bd4d5c1cfe4c2.zip
gleam fix
-rw-r--r--README.md3
-rw-r--r--package.json2
-rw-r--r--src/app/app.gleam5
-rw-r--r--src/app/pages/index/index.gleam11
-rw-r--r--src/build.gleam212
-rw-r--r--src/bundle.gleam18
6 files changed, 174 insertions, 77 deletions
diff --git a/README.md b/README.md
index 0f3e3c7..28a7cab 100644
--- a/README.md
+++ b/README.md
@@ -3,8 +3,7 @@
[![Package Version](https://img.shields.io/hexpm/v/wechat_dev_tools)](https://hex.pm/packages/wechat_dev_tools)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/wechat_dev_tools/)
-wechat_dev_tools is a template which enables developing wechat miniprograms in gleam.
-it uses [esbuild]() and [esbuild-plugin-less]() to build `*.json`, `*.wxml` and `.wxss` files
+wechat_dev_tools is a template which enables developing wechat miniprograms in [gleam](https://gleam.run). It uses [esbuild]() and [esbuild-plugin-less]() to build `*.json`, `*.wxml` and `.wxss` files
```sh
$ npm i
diff --git a/package.json b/package.json
index b4e34e6..c7540f7 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,7 @@
"scripts": {
"build": "gleam run -m build",
"watch": "WECHAT_BUILD_WATCH=1 gleam run -m build",
- "clean": "rm -rf dist/*"
+ "clean": "gleam clean && rm -rf dist/*"
},
"author": "Kai WU",
"license": "MIT",
diff --git a/src/app/app.gleam b/src/app/app.gleam
index 95cab5d..448bed5 100644
--- a/src/app/app.gleam
+++ b/src/app/app.gleam
@@ -11,9 +11,6 @@ fn on_show(_o: JsObject) -> Nil {
}
pub fn app() -> JsObject {
- object.literal([
- #("onLaunch", on_launch),
- #("onShow", on_show),
- ])
+ object.literal([#("onLaunch", on_launch), #("onShow", on_show)])
|> object.set("data", object.new())
}
diff --git a/src/app/pages/index/index.gleam b/src/app/pages/index/index.gleam
index c5a24d6..68954ab 100644
--- a/src/app/pages/index/index.gleam
+++ b/src/app/pages/index/index.gleam
@@ -14,12 +14,9 @@ fn on_ready() -> Nil {
}
pub fn page() -> JsObject {
- object.literal([
- #("onLoad", on_load),
- ])
- |> object.merge(object.literal([
- #("onShow", on_show),
- #("onReady", on_ready),
- ]))
+ object.literal([#("onLoad", on_load)])
+ |> object.merge(
+ object.literal([#("onShow", on_show), #("onReady", on_ready)]),
+ )
|> object.set("data", object.new())
}
diff --git a/src/build.gleam b/src/build.gleam
index 4c6c903..cfac0f6 100644
--- a/src/build.gleam
+++ b/src/build.gleam
@@ -1,52 +1,80 @@
+import bundle
+import envoy
import gleam/io
+import gleam/javascript/promise.{type Promise}
import gleam/list
-import gleam/string
import gleam/result
-import gleam/javascript/promise.{type Promise}
-import envoy
-import bundle
+import gleam/string
@external(javascript, "./build_ffi.mjs", "bundle_build")
-pub fn bundle_build(entry f: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn bundle_build(
+ entry f: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
@external(javascript, "./build_ffi.mjs", "js_build")
-pub fn js_build(content c: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn js_build(
+ content c: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
@external(javascript, "./build_ffi.mjs", "copy_build")
-pub fn copy_build(json f: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn copy_build(
+ json f: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
@external(javascript, "./build_ffi.mjs", "less_build")
-pub fn less_build(less f: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn less_build(
+ less f: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
@external(javascript, "./build_ffi.mjs", "bundle_watch")
-pub fn bundle_watch(entry f: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn bundle_watch(
+ entry f: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
@external(javascript, "./build_ffi.mjs", "js_watch")
-pub fn js_watch(content c: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn js_watch(
+ content c: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
@external(javascript, "./build_ffi.mjs", "copy_watch")
-pub fn copy_watch(json f: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn copy_watch(
+ json f: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
@external(javascript, "./build_ffi.mjs", "less_watch")
-pub fn less_watch(less f: String, outfile o: String) -> Promise(Result(Nil, String))
+pub fn less_watch(
+ less f: String,
+ outfile o: String,
+) -> Promise(Result(Nil, String))
const entry = "./build/dev/javascript/wechat_dev_tools/bundle.mjs"
+
const app_content = "import { app } from './bundle.mjs'; app()"
+
const dist = "./dist/"
+
const src = "./src/app/"
-pub type Builder = fn(String, String) -> Promise(Result(Nil, String))
+pub type Builder =
+ fn(String, String) -> Promise(Result(Nil, String))
pub type Asset {
- Asset(src: String, dist: String, builder: Builder)
+ Asset(src: String, dist: String, builder: Builder)
}
fn file_path(path: String, p: String, t: String) -> String {
- string.concat([ path, p, "/", p, ".", t, ])
+ string.concat([path, p, "/", p, ".", t])
}
fn index_path(path: String, p: String, t: String) -> String {
- string.concat([ path, p, "/", "index.", t, ])
+ string.concat([path, p, "/", "index.", t])
}
fn page_content(p: String) -> String {
@@ -67,32 +95,64 @@ fn component_content(p: String) -> String {
fn bundle_asset(watch: Bool) -> List(Asset) {
case watch {
- True -> [ Asset(entry, dist <> "bundle.mjs", bundle_watch) ]
- False -> [ Asset(entry, dist <> "bundle.mjs", bundle_build) ]
+ True -> [Asset(entry, dist <> "bundle.mjs", bundle_watch)]
+ False -> [Asset(entry, dist <> "bundle.mjs", bundle_build)]
}
}
fn app_assets(watch: Bool) -> List(Asset) {
case watch {
- True -> [ Asset(app_content, dist <> "app.js", js_watch),
- Asset(src <> "app.json", dist <> "app.json", copy_watch),
- Asset(src <> "app.less", dist <> "app.wxss", less_watch) ]
- False -> [ Asset(app_content, dist <> "app.js", js_build),
- Asset(src <> "app.json", dist <> "app.json", copy_build),
- Asset(src <> "app.less", dist <> "app.wxss", less_build) ]
+ True -> [
+ Asset(app_content, dist <> "app.js", js_watch),
+ Asset(src <> "app.json", dist <> "app.json", copy_watch),
+ Asset(src <> "app.less", dist <> "app.wxss", less_watch),
+ ]
+ False -> [
+ Asset(app_content, dist <> "app.js", js_build),
+ Asset(src <> "app.json", dist <> "app.json", copy_build),
+ Asset(src <> "app.less", dist <> "app.wxss", less_build),
+ ]
}
}
fn page_assets(p: String, watch: Bool) -> List(Asset) {
case watch {
- True -> [ Asset(page_content(p), file_path(dist <> "/pages/", p, "js"), js_watch),
- Asset(file_path(src <> "pages/", p, "json"), file_path(dist <> "pages/", p, "json"), copy_watch),
- Asset(file_path(src <> "pages/", p, "wxml"), file_path(dist <> "pages/", p, "wxml"), copy_watch),
- Asset(file_path(src <> "pages/", p, "less"), file_path(dist <> "pages/", p, "wxss"), less_watch) ]
- False -> [ Asset(page_content(p), file_path(dist <> "/pages/", p, "js"), js_build),
- Asset(file_path(src <> "pages/", p, "json"), file_path(dist <> "pages/", p, "json"), copy_build),
- Asset(file_path(src <> "pages/", p, "wxml"), file_path(dist <> "pages/", p, "wxml"), copy_build),
- Asset(file_path(src <> "pages/", p, "less"), file_path(dist <> "pages/", p, "wxss"), less_build) ]
+ True -> [
+ Asset(page_content(p), file_path(dist <> "/pages/", p, "js"), js_watch),
+ Asset(
+ file_path(src <> "pages/", p, "json"),
+ file_path(dist <> "pages/", p, "json"),
+ copy_watch,
+ ),
+ Asset(
+ file_path(src <> "pages/", p, "wxml"),
+ file_path(dist <> "pages/", p, "wxml"),
+ copy_watch,
+ ),
+ Asset(
+ file_path(src <> "pages/", p, "less"),
+ file_path(dist <> "pages/", p, "wxss"),
+ less_watch,
+ ),
+ ]
+ False -> [
+ Asset(page_content(p), file_path(dist <> "/pages/", p, "js"), js_build),
+ Asset(
+ file_path(src <> "pages/", p, "json"),
+ file_path(dist <> "pages/", p, "json"),
+ copy_build,
+ ),
+ Asset(
+ file_path(src <> "pages/", p, "wxml"),
+ file_path(dist <> "pages/", p, "wxml"),
+ copy_build,
+ ),
+ Asset(
+ file_path(src <> "pages/", p, "less"),
+ file_path(dist <> "pages/", p, "wxss"),
+ less_build,
+ ),
+ ]
}
}
@@ -104,14 +164,50 @@ fn pages_assets(watch: Bool) -> List(Asset) {
fn component_assets(p: String, watch: Bool) -> List(Asset) {
case watch {
- True -> [ Asset(component_content(p), index_path(dist <> "/components/", p, "js"), js_watch),
- Asset(file_path(src <> "components/", p, "json"), index_path(dist <> "components/", p, "json"), copy_watch),
- Asset(file_path(src <> "components/", p, "wxml"), index_path(dist <> "components/", p, "wxml"), copy_watch),
- Asset(file_path(src <> "components/", p, "less"), index_path(dist <> "components/", p, "wxss"), less_watch) ]
- False -> [ Asset(component_content(p), index_path(dist <> "/components/", p, "js"), js_build),
- Asset(file_path(src <> "components/", p, "json"), index_path(dist <> "components/", p, "json"), copy_build),
- Asset(file_path(src <> "components/", p, "wxml"), index_path(dist <> "components/", p, "wxml"), copy_build),
- Asset(file_path(src <> "components/", p, "less"), index_path(dist <> "components/", p, "wxss"), less_build) ]
+ True -> [
+ Asset(
+ component_content(p),
+ index_path(dist <> "/components/", p, "js"),
+ js_watch,
+ ),
+ Asset(
+ file_path(src <> "components/", p, "json"),
+ index_path(dist <> "components/", p, "json"),
+ copy_watch,
+ ),
+ Asset(
+ file_path(src <> "components/", p, "wxml"),
+ index_path(dist <> "components/", p, "wxml"),
+ copy_watch,
+ ),
+ Asset(
+ file_path(src <> "components/", p, "less"),
+ index_path(dist <> "components/", p, "wxss"),
+ less_watch,
+ ),
+ ]
+ False -> [
+ Asset(
+ component_content(p),
+ index_path(dist <> "/components/", p, "js"),
+ js_build,
+ ),
+ Asset(
+ file_path(src <> "components/", p, "json"),
+ index_path(dist <> "components/", p, "json"),
+ copy_build,
+ ),
+ Asset(
+ file_path(src <> "components/", p, "wxml"),
+ index_path(dist <> "components/", p, "wxml"),
+ copy_build,
+ ),
+ Asset(
+ file_path(src <> "components/", p, "less"),
+ index_path(dist <> "components/", p, "wxss"),
+ less_build,
+ ),
+ ]
}
}
@@ -121,12 +217,15 @@ fn components_assets(watch: Bool) -> List(Asset) {
|> list.flat_map(fn(p) { component_assets(p, watch) })
}
-fn fold_result(r0: Result(Nil, String), r: Result(Nil, String)) -> Result(Nil, String) {
+fn fold_result(
+ r0: Result(Nil, String),
+ r: Result(Nil, String),
+) -> Result(Nil, String) {
case r0, r {
- Ok(Nil), Ok(Nil) -> r0
- Error(_), Ok(Nil) -> r0
- Ok(Nil), Error(_) -> r
- Error(e1), Error(e2) -> Error(e1 <> e2)
+ Ok(Nil), Ok(Nil) -> r0
+ Error(_), Ok(Nil) -> r0
+ Ok(Nil), Error(_) -> r
+ Error(e1), Error(e2) -> Error(e1 <> "\n\n" <> e2)
}
}
@@ -135,22 +234,23 @@ fn build(ass: List(Asset)) -> Promise(Result(Nil, String)) {
|> list.map(fn(a) { a.builder(a.src, a.dist) })
|> promise.await_list
|> promise.map(fn(ls) {
- ls
- |> list.fold(Ok(Nil), fold_result)
- })
+ ls
+ |> list.fold(Ok(Nil), fold_result)
+ })
}
pub fn main() {
- let watch = envoy.get("WECHAT_BUILD_WATCH")
- |> result.is_ok
-
- use r0 <- promise.await(build(bundle_asset(watch)))
- use r1 <- promise.await(build(app_assets(watch)))
- use r2 <- promise.await(build(pages_assets(watch)))
- use r3 <- promise.await(build(components_assets(watch)))
+ let watch =
+ envoy.get("WECHAT_BUILD_WATCH")
+ |> result.is_ok
+
+ use r0 <- promise.await(bundle_asset(watch) |> build)
+ use r1 <- promise.await(app_assets(watch) |> build)
+ use r2 <- promise.await(pages_assets(watch) |> build)
+ use r3 <- promise.await(components_assets(watch) |> build)
[r0, r1, r2, r3]
|> list.fold(Ok(Nil), fold_result)
- |> result.map_error(fn (e) { io.println_error(e) })
+ |> result.map_error(fn(e) { io.println_error(e) })
|> promise.resolve
}
diff --git a/src/bundle.gleam b/src/bundle.gleam
index de4c8c2..30194ed 100644
--- a/src/bundle.gleam
+++ b/src/bundle.gleam
@@ -1,15 +1,16 @@
import gleam/list
import gleam/result
+import wechat/app as weapp
+import wechat/component.{run_component}
import wechat/object.{type JsObject}
import wechat/page.{run_page}
-import wechat/component.{run_component}
-import wechat/app as weapp
import app/app
-import app/pages/index/index
import app/components/basic/basic
+import app/pages/index/index
-pub type Constructor = fn() -> JsObject
+pub type Constructor =
+ fn() -> JsObject
pub fn app() -> Result(Nil, Nil) {
app.app() |> weapp.run_app |> Ok
@@ -26,11 +27,14 @@ pub fn components() -> List(#(String, Constructor)) {
pub fn page(ps: List(#(String, Constructor)), p: String) -> Result(Nil, Nil) {
ps
|> list.find(fn(px) { px.0 == p })
- |> result.try(fn(px) { px.1() |> run_page |> Ok})
+ |> result.try(fn(px) { px.1() |> run_page |> Ok })
}
-pub fn component(ps: List(#(String, Constructor)), p: String) -> Result(Nil, Nil) {
+pub fn component(
+ ps: List(#(String, Constructor)),
+ p: String,
+) -> Result(Nil, Nil) {
ps
|> list.find(fn(px) { px.0 == p })
- |> result.try(fn(px) { px.1() |> run_component |> Ok})
+ |> result.try(fn(px) { px.1() |> run_component |> Ok })
}