diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2024-02-15 17:00:45 +0000 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2024-02-15 17:00:45 +0000 |
commit | e486614c0aafbed8da06b22fa20c32812d01ed52 (patch) | |
tree | b6d5a2625f776bd9aaa5ec776c0b86702d5377ff /test/build.gleam | |
parent | 6bba4b25b0cfbe3ba04f7e043ac3ed33be81a8a2 (diff) | |
download | lustre-e486614c0aafbed8da06b22fa20c32812d01ed52.tar.gz lustre-e486614c0aafbed8da06b22fa20c32812d01ed52.zip |
:wrench: Create a build script for bundling the server component runtime.
Diffstat (limited to 'test/build.gleam')
-rw-r--r-- | test/build.gleam | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/test/build.gleam b/test/build.gleam new file mode 100644 index 0000000..5eb1b0b --- /dev/null +++ b/test/build.gleam @@ -0,0 +1,122 @@ +// IMPORTS --------------------------------------------------------------------- + +import gleam/bool +import gleam/io +import gleam/regex.{Options} +import gleam/result +import gleam/string +import shellout +import simplifile + +// MAIN ------------------------------------------------------------------------ + +pub fn main() { + io.debug({ + use exists <- try(verify_esbuild(), SimplifileError) + use <- bool.guard(!exists, Error(MissingEsbuild)) + + use _ <- try(build_for_javascript(), ShelloutError) + use _ <- try(bundle_server_component(), ShelloutError) + use _ <- try(bundle_minified_server_component(), ShelloutError) + + use script <- try(read_script(), SimplifileError) + use module <- try(read_module(), SimplifileError) + use _ <- try(inject_script(script, module), SimplifileError) + + use _ <- try(format_project(), ShelloutError) + + Ok(Nil) + }) +} + +// CONSTANTS ------------------------------------------------------------------- + +const esbuild = "./build/.lustre/bin/esbuild" + +// STEPS ----------------------------------------------------------------------- + +fn verify_esbuild() { + simplifile.verify_is_file(esbuild) +} + +fn build_for_javascript() { + shellout.command( + run: "gleam", + with: ["build", "--target", "javascript"], + in: ".", + opt: [], + ) +} + +fn bundle_server_component() { + shellout.command( + run: esbuild, + with: [ + "./src/server-component.mjs", "--bundle", "--format=esm", + "--outfile=./priv/static/lustre-server-component.mjs", + ], + in: ".", + opt: [], + ) +} + +fn bundle_minified_server_component() { + shellout.command( + run: esbuild, + with: [ + "./src/server-component.mjs", "--bundle", "--minify", "--format=esm", + "--outfile=./priv/static/lustre-server-component.min.mjs", + ], + in: ".", + opt: [], + ) +} + +fn read_script() { + simplifile.read("./priv/static/lustre-server-component.min.mjs") + |> result.map(string.replace(_, "\"", "\\\"")) + |> result.map(string.trim) +} + +fn read_module() { + simplifile.read("./src/lustre/server.gleam") +} + +fn inject_script(script, module) { + let inject_regex = "// <<INJECT RUNTIME>>\\n.+\\n.+\\n \\)," + let options = Options(case_insensitive: False, multi_line: True) + let assert Ok(re) = regex.compile(inject_regex, options) + let assert [before, after] = regex.split(re, module) + + simplifile.write( + "./src/lustre/server.gleam", + before + <> "// <<INJECT RUNTIME>>\n element.text(\"" + <> script + <> "\")," + <> after, + ) +} + +fn format_project() { + shellout.command(run: "gleam", with: ["format"], in: ".", opt: []) +} + +// ERROR HANDLING -------------------------------------------------------------- + +pub type Error { + MissingEsbuild + ShelloutError(#(Int, String)) + SimplifileError(simplifile.FileError) +} + +fn try( + result: Result(a, e), + to_error: fn(e) -> Error, + then: fn(a) -> Result(b, Error), +) -> Result(b, Error) { + case result { + Ok(value) -> then(value) + Error(error) -> Error(to_error(error)) + } +} |