Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion compiler/src/Generate/Html.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,50 @@ import qualified Lamdera.UiSourceMap

-- SANDWICH


-- @LAMDERA root :: FilePath parameter added.
sandwich :: FilePath -> Name.Name -> B.Builder -> B.Builder
sandwich root moduleName javascript =
Lamdera.alternativeImplementationWhen Lamdera.isLamdera_ (sandwich_ root moduleName javascript) $
let name = Name.toBuilder moduleName in
[r|<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>|] <> name <> [r|</title>
<style>body { padding: 0; margin: 0; }</style>
</head>

<body>

<pre id="elm"></pre>

<script>
try {
|] <> javascript <> [r|

var app = Elm.|] <> name <> [r|.init({ node: document.getElementById("elm") });
}
catch (e)
{
// display initialization errors (e.g. bad flags, infinite recursion)
var header = document.createElement("h1");
header.style.fontFamily = "monospace";
header.innerText = "Initialization Error";
var pre = document.getElementById("elm");
document.body.insertBefore(header, pre);
pre.innerText = e;
throw e;
}
</script>

</body>
</html>|]


-- @LAMDERA

sandwich_ :: FilePath -> Name.Name -> B.Builder -> B.Builder
sandwich_ root moduleName javascript =
let
name = Name.toBuilder moduleName

Expand Down
1 change: 1 addition & 0 deletions elm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ Executable lamdera
Test.Wire
Test.JsOutput
Test.WebGL
Test.BackwardsCompat
Test.Lamdera.Evergreen.TestMigrationHarness
Test.Lamdera.Evergreen.TestMigrationGenerator

Expand Down
24 changes: 24 additions & 0 deletions extra/Lamdera/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ makeDev_ path =
makeDev (FP.takeDirectory path) [path]


makeDevHtml :: FilePath -> [FilePath] -> IO ()
makeDevHtml root paths = do
debug $ "🏗 makeDevHtml: lamdera make " <> root <> "/"

r <- async $
Ext.Common.withProjectRoot root $ do
Make.run paths $
Make.Flags
{ _debug = True
, _optimize = False
, _output = Nothing
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
-- the progress bar – which messes with subsequent output if it gets written to
-- stdout too quickly, as it doesn't seem to flush fast enough. Adding a small
-- delay seems to solve the problem.
sleep 10


-- Runs `lamdera make` of harness file with JS file output
makeHarnessDevJs :: FilePath -> IO ()
makeHarnessDevJs root = do
Expand Down
5 changes: 5 additions & 0 deletions test/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import qualified Test.Ext.ElmPages.Check
import qualified Test.TypeHashes
import qualified Test.JsOutput
import qualified Test.WebGL
<<<<<<< HEAD
import qualified Test.Lamdera.Live
=======
import qualified Test.BackwardsCompat
>>>>>>> bdcb71f7 (Test Lamdera HTML injections don't happen on vanilla Elm projects)

import qualified Test.Lamdera.Evergreen.TestMigrationHarness
import qualified Test.Lamdera.Evergreen.TestMigrationGenerator
Expand Down Expand Up @@ -161,4 +165,5 @@ allTests =
, scope "Test.WebGL -> " $ Test.WebGL.suite
, scope "Test.JsOutput -> " $ Test.JsOutput.suite
, scope "Test.Lamdera.Live -> " $ Test.Lamdera.Live.suite
, scope "Test.BackwardsCompat -> " $ Test.BackwardsCompat.suite
]
39 changes: 39 additions & 0 deletions test/Test/BackwardsCompat.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.BackwardsCompat where

import EasyTest
import Ext.Common
import Lamdera hiding ((&))
import Test.Helpers
import qualified Lamdera.Compile

suite :: Test ()
suite = tests $
[ scope "an vanilla elm project compiled with lamdera should not inject lamdera html modifications" $
let
project = "./test/scenario-empty-elm-init"
bashInScenario c = bash $ "cd " ++ project ++ " && " ++ c

setup = do
rmdir $ project ++ "/elm-home"
rmdir $ project ++ "/elm-stuff"

cleanup _ = do
pure ()

test _ = do
io $ do
withEnvVars [("ELM_HOME", project ++ "/elm-home")] $
Lamdera.Compile.makeDevHtml project ["src/Main.elm"]

htmlM <- io $ readUtf8Text $ project ++ "/index.html"

case htmlM of
Nothing -> crash "index.html not found"
Just html -> do
expectTextDoesNotContain html "apple-mobile-web-app-capable"

in
using setup cleanup test
]