aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2024-03-16 23:52:33 +0000
committerHayleigh Thompson <me@hayleigh.dev>2024-03-17 17:14:02 +0000
commit80b418f6ee44321d55ed18375d34db1d35620deb (patch)
treedbce546f150a1c06fd3287b8858ba22a6c8508f5
parent4aeb451d32d7ad94a5a4111543584a1430cd4571 (diff)
downloadlustre-80b418f6ee44321d55ed18375d34db1d35620deb.tar.gz
lustre-80b418f6ee44321d55ed18375d34db1d35620deb.zip
:sparkles: Add a '--spa' flag for serving a lustre app on all routes during development.
-rw-r--r--src/lustre/cli/dev.gleam51
-rw-r--r--src/lustre/cli/esbuild.gleam16
2 files changed, 24 insertions, 43 deletions
diff --git a/src/lustre/cli/dev.gleam b/src/lustre/cli/dev.gleam
index 3b24dc9..d2d4f5f 100644
--- a/src/lustre/cli/dev.gleam
+++ b/src/lustre/cli/dev.gleam
@@ -7,13 +7,10 @@ import gleam/package_interface.{type Type, Fn, Named, Variable}
import gleam/string
import glint.{type Command, CommandInput}
import glint/flag
-import lustre/attribute.{attribute}
import lustre/cli/esbuild
import lustre/cli/project.{type Module}
import lustre/cli/step
import lustre/cli/utils.{guard, keep, map, replace, template, try}
-import lustre/element
-import lustre/element/html.{html}
import simplifile
// COMMANDS --------------------------------------------------------------------
@@ -27,7 +24,8 @@ pub fn run() -> Command(Nil) {
let CommandInput(flags: flags, ..) = input
let assert Ok(host) = flag.get_string(flags, "host")
let assert Ok(port) = flag.get_string(flags, "port")
- let assert Ok(include_styles) = flag.get_bool(flags, "include-styles")
+ let assert Ok(use_lustre_ui) = flag.get_bool(flags, "use-lustre-ui")
+ let assert Ok(spa) = flag.get_bool(flags, "spa")
let script = {
use <- step.new("Building your project")
@@ -52,7 +50,7 @@ pub fn run() -> Command(Nil) {
|> string.replace("{app_name}", interface.name)
let html =
- template(case include_styles {
+ template(case use_lustre_ui {
True -> "index-with-lustre-ui.html"
False -> "index.html"
})
@@ -69,7 +67,7 @@ pub fn run() -> Command(Nil) {
),
map(BundleError),
)
- use _ <- step.run(esbuild.serve(host, port), map(BundleError))
+ use _ <- step.run(esbuild.serve(host, port, spa), map(BundleError))
step.return(Nil)
}
@@ -104,6 +102,14 @@ pub fn run() -> Command(Nil) {
|> flag.default(default)
|> flag.description(description)
})
+ |> glint.flag("spa", {
+ let description = ""
+ let default = False
+
+ flag.bool()
+ |> flag.default(default)
+ |> flag.description(description)
+ })
}
// ERROR HANDLING --------------------------------------------------------------
@@ -184,39 +190,6 @@ fn check_is_lustre_app(
// UTILS -----------------------------------------------------------------------
-fn index_html(
- app_name: String,
- container_id: String,
- include_styles: Bool,
-) -> String {
- let styles = case include_styles {
- True ->
- html.link([
- attribute.rel("stylesheet"),
- attribute.href(
- "https://cdn.jsdelivr.net/gh/lustre-labs/ui/priv/styles.css",
- ),
- ])
- False -> element.none()
- }
-
- html([], [
- html.head([], [
- html.meta([attribute("charset", "utf-8")]),
- html.meta([
- attribute("name", "viewport"),
- attribute("content", "width=device-width, initial-scale=1"),
- ]),
- html.title([], app_name),
- html.script([attribute.type_("module"), attribute.src("./index.mjs")], ""),
- styles,
- ]),
- html.body([], [html.div([attribute.id(container_id)], [])]),
- ])
- |> element.to_string
- |> string.append("<!DOCTYPE html>", _)
-}
-
fn is_nil_type(t: Type) -> Bool {
case t {
Named(name: "Nil", package: "", module: "gleam", parameters: []) -> True
diff --git a/src/lustre/cli/esbuild.gleam b/src/lustre/cli/esbuild.gleam
index 7fe613f..2161a2f 100644
--- a/src/lustre/cli/esbuild.gleam
+++ b/src/lustre/cli/esbuild.gleam
@@ -77,7 +77,7 @@ pub fn bundle(
step.return(Nil)
}
-pub fn serve(host: String, port: String) -> Step(Nil, Error) {
+pub fn serve(host: String, port: String, spa: Bool) -> Step(Nil, Error) {
use _ <- step.run(download(get_os(), get_cpu()), keep)
let root = project.root()
let flags = [
@@ -85,9 +85,17 @@ pub fn serve(host: String, port: String) -> Step(Nil, Error) {
"--servedir=" <> filepath.join(root, "build/.lustre"),
]
+ let options = case spa {
+ True -> [
+ "--serve-fallback=" <> filepath.join(root, "build/.lustre/index.html"),
+ ..flags
+ ]
+ False -> flags
+ }
+
use <- step.done("\nStarting dev server at " <> host <> ":" <> port <> "...")
use _ <- step.try(
- exec(run: "./build/.lustre/bin/esbuild", in: root, with: flags),
+ exec(run: "./build/.lustre/bin/esbuild", in: root, with: options),
on_error: fn(pair) { BundleError(pair.1) },
)
@@ -210,8 +218,8 @@ There was a network error!",
// TODO: this could give a better error for some common reason like Enoent.
SimplifileError(reason, path) -> io.println("
I ran into the following error at path `" <> path <> "`:" <> string.inspect(
- reason,
- ) <> ".")
+ reason,
+ ) <> ".")
UnknownPlatform(os, cpu) -> io.println("
I couldn't figure out the correct esbuild version for your