-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - support assets including stylesheets
- Loading branch information
Showing
4 changed files
with
100 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,75 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE OverloadedRecordDot #-} | ||
|
||
#ifdef wasi_HOST_OS | ||
|
||
module MyMain (main) where | ||
|
||
import App (start) | ||
import App | ||
import GHC.Wasm.Prim | ||
import Language.Javascript.JSaddle.Wasm qualified as JSaddle.Wasm | ||
|
||
foreign export javascript "hs_start" main :: JSString -> IO () | ||
|
||
main :: JSString -> IO () | ||
main e = JSaddle.Wasm.run $ start e | ||
main e = JSaddle.Wasm.run (start e).app | ||
|
||
#else | ||
|
||
module Main (main) where | ||
|
||
import App (start) | ||
import App | ||
import Data.Text.Lazy qualified as T | ||
import Data.Text.Lazy.Encoding (encodeUtf8) | ||
import Language.Javascript.JSaddle | ||
import Language.Javascript.JSaddle.Warp | ||
import Network.Wai.Handler.Warp | ||
import Network.WebSockets | ||
import Network.Wai.Application.Static | ||
import System.Environment | ||
|
||
{- TODO | ||
work out how to live-reload on changes to stylesheet | ||
maybe don't use `dist` version... | ||
maybe just a matter of passing right flag to GHCID? | ||
in theory I shouldn't even need to do that - just force a page refresh | ||
is that something we can hook in to? | ||
open jsaddle PR and use it here as a SRP | ||
punt: | ||
somehow DRY to match static HTML files | ||
-} | ||
main :: IO () | ||
main = getArgs >>= \case | ||
[arg] -> runSettings (setPort 8000 defaultSettings) | ||
=<< jsaddleOr defaultConnectionOptions (start $ toJSString arg) | ||
jsaddleApp | ||
_ -> fail "bad args: specify an example, e.g. 2048" | ||
main = | ||
getArgs >>= \case | ||
-- Note that `debug` works with `cabal repl` but not `cabal run`. | ||
-- The best workflow is to run `ghcid -c "cabal repl ghc-wasm-miso-examples" -W -T ':main primer'`. | ||
[arg] -> | ||
let app = | ||
start | ||
-- "2048" | ||
(toJSString arg) | ||
-- we can't use multiline syntax alongside CPP... | ||
-- "<meta charset='utf-8'>\n\ | ||
-- \<meta name='viewport' content='width=device-width, initial-scale=1'>\n\ | ||
-- \<title>2048 | Miso example via GHC WASM</title>\n\ | ||
-- \<link rel='stylesheet' href='2048/main.css'/>\n\ | ||
-- \" | ||
header = | ||
encodeUtf8 $ | ||
T.unlines $ | ||
[ "<title>" <> app.name <> "</title>" | ||
] | ||
<> map | ||
(\s -> "<link rel='stylesheet' href='" <> s <> "'/>") | ||
app.stylesheets | ||
in | ||
-- can't work out how to get non-`debug` version working | ||
-- but I think I prefer this anyway | ||
debugOr | ||
(Just header) | ||
8000 | ||
app.app | ||
(staticApp (defaultWebAppSettings "frontend/dist")) | ||
_ -> fail "bad args: specify an example, e.g. 2048" | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters