Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit d87886d

Browse files
committed
WIP dev-css support
1 parent 691f126 commit d87886d

File tree

4 files changed

+100
-19
lines changed

4 files changed

+100
-19
lines changed

app/App.hs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE CPP #-}
22

3-
module App (start) where
3+
module App (start, App(..)) where
44

55
#ifdef wasi_HOST_OS
66
import GHC.Wasm.Prim
@@ -9,18 +9,50 @@ import Language.Javascript.JSaddle (JSM)
99
import Language.Javascript.JSaddle
1010
#endif
1111

12+
import Data.Text.Lazy (Text)
1213
import SimpleCounter qualified
1314
import Snake qualified
1415
import TodoMVC qualified
1516
import TwoZeroFourEight qualified
1617
import XHR qualified
1718

18-
start :: JSString -> JSM ()
19+
data App = App
20+
{ name :: Text
21+
, stylesheets :: [Text]
22+
, app :: JSM ()
23+
}
24+
25+
start :: JSString -> App
1926
start e =
2027
case fromJSString e :: String of
21-
"simplecounter" -> SimpleCounter.start
22-
"snake" -> Snake.start
23-
"todomvc" -> TodoMVC.start
24-
"xhr" -> XHR.start
25-
"2048" -> TwoZeroFourEight.start
26-
_ -> fail "unknown example"
28+
"simplecounter" ->
29+
App
30+
{ name = "SimpleCounter"
31+
, stylesheets = []
32+
, app = SimpleCounter.start
33+
}
34+
"snake" ->
35+
App
36+
{ name = "Snake"
37+
, stylesheets = []
38+
, app = Snake.start
39+
}
40+
"todomvc" ->
41+
App
42+
{ name = "TodoMVC"
43+
, stylesheets = ["todomvc/base.css", "todomvc/index.css"]
44+
, app = TodoMVC.start
45+
}
46+
"xhr" ->
47+
App
48+
{ name = "XHR"
49+
, stylesheets = []
50+
, app = XHR.start
51+
}
52+
"2048" ->
53+
App
54+
{ name = "TwoZeroFourEight"
55+
, stylesheets = ["2048/main.css"]
56+
, app = TwoZeroFourEight.start
57+
}
58+
_ -> error "unknown example"

app/Main.hs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,75 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE OverloadedRecordDot #-}
23

34
#ifdef wasi_HOST_OS
45

56
module MyMain (main) where
67

7-
import App (start)
8+
import App
89
import GHC.Wasm.Prim
910
import Language.Javascript.JSaddle.Wasm qualified as JSaddle.Wasm
1011

1112
foreign export javascript "hs_start" main :: JSString -> IO ()
1213

1314
main :: JSString -> IO ()
14-
main e = JSaddle.Wasm.run $ start e
15+
main e = JSaddle.Wasm.run (start e).app
1516

1617
#else
1718

1819
module Main (main) where
1920

20-
import App (start)
21+
import App
22+
import Data.Text.Lazy qualified as T
23+
import Data.Text.Lazy.Encoding (encodeUtf8)
2124
import Language.Javascript.JSaddle
2225
import Language.Javascript.JSaddle.Warp
23-
import Network.Wai.Handler.Warp
24-
import Network.WebSockets
26+
import Network.Wai.Application.Static
2527
import System.Environment
2628

29+
{- TODO
30+
work out how to live-reload on changes to stylesheet
31+
maybe don't use `dist` version...
32+
maybe just a matter of passing right flag to GHCID?
33+
in theory I shouldn't even need to do that - just force a page refresh
34+
is that something we can hook in to?
35+
36+
open jsaddle PR and use it here as a SRP
37+
38+
punt:
39+
somehow DRY to match static HTML files
40+
-}
2741
main :: IO ()
28-
main = getArgs >>= \case
29-
[arg] -> runSettings (setPort 8000 defaultSettings)
30-
=<< jsaddleOr defaultConnectionOptions (start $ toJSString arg)
31-
jsaddleApp
32-
_ -> fail "bad args: specify an example, e.g. 2048"
42+
main =
43+
getArgs >>= \case
44+
-- Note that `debug` works with `cabal repl` but not `cabal run`.
45+
-- The best workflow is to run `ghcid -c "cabal repl ghc-wasm-miso-examples" -W -T ':main primer'`.
46+
[arg] ->
47+
let app =
48+
start
49+
-- "2048"
50+
(toJSString arg)
51+
-- we can't use multiline syntax alongside CPP...
52+
-- "<meta charset='utf-8'>\n\
53+
-- \<meta name='viewport' content='width=device-width, initial-scale=1'>\n\
54+
-- \<title>2048 | Miso example via GHC WASM</title>\n\
55+
-- \<link rel='stylesheet' href='2048/main.css'/>\n\
56+
-- \"
57+
header =
58+
encodeUtf8 $
59+
T.unlines $
60+
[ "<title>" <> app.name <> "</title>"
61+
]
62+
<> map
63+
(\s -> "<link rel='stylesheet' href='" <> s <> "'/>")
64+
app.stylesheets
65+
in
66+
-- can't work out how to get non-`debug` version working
67+
-- but I think I prefer this anyway
68+
debugOr
69+
(Just header)
70+
8000
71+
app.app
72+
(staticApp (defaultWebAppSettings "frontend/dist"))
73+
_ -> fail "bad args: specify an example, e.g. 2048"
3374

3475
#endif

cabal.project

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,13 @@ if arch(wasm32)
1818
location: https://github.com/amesgen/splitmix
1919
tag: 5f5b766d97dc735ac228215d240a3bb90bc2ff75
2020

21+
else
22+
-- https://github.com/ghcjs/jsaddle/pull/149
23+
source-repository-package
24+
type: git
25+
location: https://github.com/georgefst/jsaddle
26+
tag: 8ebf96891fe9580fc16e1fe99b0ca503b9cf2ed8
27+
subdir: jsaddle jsaddle-warp
28+
2129
package aeson
2230
flags: -ordered-keymap

ghc-wasm-miso-examples.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ executable ghc-wasm-miso-examples
2929
build-depends: ghc-experimental, jsaddle-wasm
3030
ghc-options: -no-hs-main -optl-mexec-model=reactor "-optl-Wl,--export=hs_start"
3131
else
32-
build-depends: jsaddle-warp, warp, websockets
32+
build-depends: jsaddle-warp, wai-app-static, warp, websockets

0 commit comments

Comments
 (0)