Skip to content

Commit 8cfc062

Browse files
authored
Merge pull request #54 from rupertlssmith/pr-html-fix
Added Lamdera alternative implementation for HTML generation.
2 parents 1d47882 + 490dd70 commit 8cfc062

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

compiler/src/Generate/Html.hs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,50 @@ import qualified Lamdera.UiSourceMap
1717

1818
-- SANDWICH
1919

20-
20+
-- @LAMDERA root :: FilePath parameter added.
2121
sandwich :: FilePath -> Name.Name -> B.Builder -> B.Builder
2222
sandwich root moduleName javascript =
23+
Lamdera.alternativeImplementationWhen Lamdera.isLamdera_ (sandwich_ root moduleName javascript) $
24+
let name = Name.toBuilder moduleName in
25+
[r|<!DOCTYPE HTML>
26+
<html>
27+
<head>
28+
<meta charset="UTF-8">
29+
<title>|] <> name <> [r|</title>
30+
<style>body { padding: 0; margin: 0; }</style>
31+
</head>
32+
33+
<body>
34+
35+
<pre id="elm"></pre>
36+
37+
<script>
38+
try {
39+
|] <> javascript <> [r|
40+
41+
var app = Elm.|] <> name <> [r|.init({ node: document.getElementById("elm") });
42+
}
43+
catch (e)
44+
{
45+
// display initialization errors (e.g. bad flags, infinite recursion)
46+
var header = document.createElement("h1");
47+
header.style.fontFamily = "monospace";
48+
header.innerText = "Initialization Error";
49+
var pre = document.getElementById("elm");
50+
document.body.insertBefore(header, pre);
51+
pre.innerText = e;
52+
throw e;
53+
}
54+
</script>
55+
56+
</body>
57+
</html>|]
58+
59+
60+
-- @LAMDERA
61+
62+
sandwich_ :: FilePath -> Name.Name -> B.Builder -> B.Builder
63+
sandwich_ root moduleName javascript =
2364
let
2465
name = Name.toBuilder moduleName
2566

elm.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Executable lamdera
320320
Test.Wire
321321
Test.JsOutput
322322
Test.WebGL
323+
Test.BackwardsCompat
323324
Test.Lamdera.Evergreen.TestMigrationHarness
324325
Test.Lamdera.Evergreen.TestMigrationGenerator
325326

extra/Lamdera/Compile.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ makeDev_ path =
112112
makeDev (FP.takeDirectory path) [path]
113113

114114

115+
makeDevHtml :: FilePath -> [FilePath] -> IO ()
116+
makeDevHtml root paths = do
117+
debug $ "🏗 makeDevHtml: lamdera make " <> root <> "/"
118+
119+
r <- async $
120+
Ext.Common.withProjectRoot root $ do
121+
Make.run paths $
122+
Make.Flags
123+
{ _debug = True
124+
, _optimize = False
125+
, _output = Nothing
126+
, _report = Nothing
127+
, _docs = Nothing
128+
, _noWire = False
129+
, _optimizeLegible = False
130+
}
131+
wait r
132+
-- The compilation process ends by printing to terminal in a way that overwrites
133+
-- the progress bar – which messes with subsequent output if it gets written to
134+
-- stdout too quickly, as it doesn't seem to flush fast enough. Adding a small
135+
-- delay seems to solve the problem.
136+
sleep 10
137+
138+
115139
-- Runs `lamdera make` of harness file with JS file output
116140
makeHarnessDevJs :: FilePath -> IO ()
117141
makeHarnessDevJs root = do

test/Test.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import qualified Test.Ext.ElmPages.Check
2121
import qualified Test.TypeHashes
2222
import qualified Test.JsOutput
2323
import qualified Test.WebGL
24+
<<<<<<< HEAD
2425
import qualified Test.Lamdera.Live
26+
=======
27+
import qualified Test.BackwardsCompat
28+
>>>>>>> bdcb71f7 (Test Lamdera HTML injections don't happen on vanilla Elm projects)
2529

2630
import qualified Test.Lamdera.Evergreen.TestMigrationHarness
2731
import qualified Test.Lamdera.Evergreen.TestMigrationGenerator
@@ -161,4 +165,5 @@ allTests =
161165
, scope "Test.WebGL -> " $ Test.WebGL.suite
162166
, scope "Test.JsOutput -> " $ Test.JsOutput.suite
163167
, scope "Test.Lamdera.Live -> " $ Test.Lamdera.Live.suite
168+
, scope "Test.BackwardsCompat -> " $ Test.BackwardsCompat.suite
164169
]

test/Test/BackwardsCompat.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Test.BackwardsCompat where
4+
5+
import EasyTest
6+
import Ext.Common
7+
import Lamdera hiding ((&))
8+
import Test.Helpers
9+
import qualified Lamdera.Compile
10+
11+
suite :: Test ()
12+
suite = tests $
13+
[ scope "an vanilla elm project compiled with lamdera should not inject lamdera html modifications" $
14+
let
15+
project = "./test/scenario-empty-elm-init"
16+
bashInScenario c = bash $ "cd " ++ project ++ " && " ++ c
17+
18+
setup = do
19+
rmdir $ project ++ "/elm-home"
20+
rmdir $ project ++ "/elm-stuff"
21+
22+
cleanup _ = do
23+
pure ()
24+
25+
test _ = do
26+
io $ do
27+
withEnvVars [("ELM_HOME", project ++ "/elm-home")] $
28+
Lamdera.Compile.makeDevHtml project ["src/Main.elm"]
29+
30+
htmlM <- io $ readUtf8Text $ project ++ "/index.html"
31+
32+
case htmlM of
33+
Nothing -> crash "index.html not found"
34+
Just html -> do
35+
expectTextDoesNotContain html "apple-mobile-web-app-capable"
36+
37+
in
38+
using setup cleanup test
39+
]

0 commit comments

Comments
 (0)