diff options
author | Kai WU <kaiwu2004@gmail.com> | 2024-07-19 20:28:20 +0800 |
---|---|---|
committer | Kai WU <kaiwu2004@gmail.com> | 2024-07-19 20:28:20 +0800 |
commit | e9df8525229592002c7747a308a20db3c4b9db37 (patch) | |
tree | acb3d4373e2ed0624db8def50b52ef41bbcb7886 | |
parent | aa3ff1f356992953c7953ea7b314c927def13958 (diff) | |
download | wechat_dev_tools-e9df8525229592002c7747a308a20db3c4b9db37.tar.gz wechat_dev_tools-e9df8525229592002c7747a308a20db3c4b9db37.zip |
add components
-rw-r--r-- | src/build.gleam | 28 | ||||
-rw-r--r-- | src/bundle.gleam | 7 |
2 files changed, 34 insertions, 1 deletions
diff --git a/src/build.gleam b/src/build.gleam index 038afe9..e85f13a 100644 --- a/src/build.gleam +++ b/src/build.gleam @@ -32,6 +32,10 @@ fn file_path(path: String, p: String, t: String) -> String { string.concat([ path, p, "/", p, ".", t, ]) } +fn index_path(path: String, p: String, t: String) -> String { + string.concat([ path, p, "/", "index.", t, ]) +} + fn page_content(p: String) -> String { string.concat([ "import { pages, page } from '../../bundle.mjs'; page(pages(), \"", @@ -40,6 +44,14 @@ fn page_content(p: String) -> String { ]) } +fn component_content(p: String) -> String { + string.concat([ + "import { components, component } from '../../bundle.mjs'; component(components(), \"", + p, + "\")", + ]) +} + fn bundle_asset() -> List(Asset) { [ Asset(entry, dist <> "bundle.mjs", bundle_build) ] } @@ -63,6 +75,19 @@ fn pages_assets() -> List(Asset) { |> list.flat_map(fn(p) { page_assets(p) }) } +fn component_assets(p: String) -> List(Asset) { + [ 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) ] +} + +fn components_assets() -> List(Asset) { + bundle.components() + |> list.map(fn(p) { p.0 }) + |> list.flat_map(fn(p) { component_assets(p) }) +} + fn fold_result(r0: Result(Nil, String), r: Result(Nil, String)) -> Result(Nil, String) { case r0, r { Ok(Nil), Ok(Nil) -> r0 @@ -86,8 +111,9 @@ pub fn main() { use r0 <- promise.await(build(bundle_asset())) use r1 <- promise.await(build(app_assets())) use r2 <- promise.await(build(pages_assets())) + use r3 <- promise.await(build(components_assets())) - [r0, r1, r2] + [r0, r1, r2, r3] |> list.fold(Ok(Nil), fold_result) |> result.map_error(fn (e) { io.println_error(e) }) |> promise.resolve diff --git a/src/bundle.gleam b/src/bundle.gleam index 8781096..de4c8c2 100644 --- a/src/bundle.gleam +++ b/src/bundle.gleam @@ -2,6 +2,7 @@ import gleam/list import gleam/result import wechat/object.{type JsObject} import wechat/page.{run_page} +import wechat/component.{run_component} import wechat/app as weapp import app/app @@ -27,3 +28,9 @@ pub fn page(ps: List(#(String, Constructor)), p: String) -> Result(Nil, Nil) { |> list.find(fn(px) { px.0 == p }) |> result.try(fn(px) { px.1() |> run_page |> Ok}) } + +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}) +} |