Skip to content

Commit

Permalink
use app as entries
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiwu committed Aug 4, 2024
1 parent 443327a commit 0be38e2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/app.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import app/misc
import njs/ngx.{type JsObject}

pub fn exports() -> JsObject {
ngx.object()
|> ngx.export(misc.version)
|> ngx.export(misc.decode_uri)
|> ngx.export(misc.hello)
|> ngx.export(misc.join)
}
52 changes: 42 additions & 10 deletions src/ngs.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,48 @@ type Asset {
Asset(src: String, dist: String, builder: Builder)
}

const entry = "./build/dev/javascript/ngs/app/misc.mjs"
type App {
App(name: String, entry: String)
}

fn apps() -> List(App) {
[App("app", "./build/dev/javascript/ngs/app.mjs")]
}

const dist = "./dist/nginx/"
const dist = "./dist/"

// const src = "./src/"
const src = "./src/"

fn bundle_asset(watch: Bool) -> List(Asset) {
case watch {
True -> [Asset(entry, dist <> "njs/app.js", bundle_watch)]
False -> [Asset(entry, dist <> "njs/app.js", bundle_build)]
}
fn bundle_asset(apps: List(App), watch: Bool) -> List(Asset) {
apps
|> list.map(fn(a) {
case watch {
True ->
Asset(a.entry, dist <> a.name <> "/nginx/njs/app.js", bundle_watch)
False ->
Asset(a.entry, dist <> a.name <> "/nginx/njs/app.js", bundle_build)
}
})
}

fn conf_asset(apps: List(App), watch: Bool) -> List(Asset) {
apps
|> list.map(fn(a) {
case watch {
True ->
Asset(
src <> a.name <> "/nginx.conf",
dist <> a.name <> "/nginx/conf/nginx.conf",
copy_watch,
)
False ->
Asset(
src <> a.name <> "/nginx.conf",
dist <> a.name <> "/nginx/conf/nginx.conf",
copy_build,
)
}
})
}

fn fold_result(
Expand Down Expand Up @@ -75,9 +106,10 @@ pub fn main() {
envoy.get("NJS_BUILD_WATCH")
|> result.is_ok

use r0 <- promise.await(bundle_asset(watch) |> build)
use r0 <- promise.await(apps() |> bundle_asset(watch) |> build)
use r1 <- promise.await(apps() |> conf_asset(watch) |> build)

[r0]
[r0, r1]
|> list.fold(Ok(Nil), fold_result)
|> result.map_error(fn(e) { io.println_error(e) })
|> promise.resolve
Expand Down
6 changes: 4 additions & 2 deletions src/ngs_ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export function bundle_build(entry, out) {
build({
entryPoints: [entry],
bundle: true,
minify: false,
minify: true,
footer: {js : "export default app.exports()"},
keepNames: true,
format: 'esm',
format: 'iife',
globalName: 'app',
outfile: out,
external: ['querystring'],
Expand Down Expand Up @@ -40,6 +41,7 @@ export function bundle_watch(entry, out) {
entryPoints: [entry],
bundle: true,
minify: true,
footer: "export default app.exports()",
keepNames: true,
format: 'iife',
globalName: 'app',
Expand Down

0 comments on commit 0be38e2

Please sign in to comment.