git.iko.soy/lynxjs-elm/compiler
on master help sign in
Merge pull request #2217 from elm/new-compile-endpoint

Add compile/v2 returning errors in JSON

commit39949053f9469862b17006e3965f3b440414d13c authorEvan Czaplicki <evancz@users.noreply.github.com> dateSun 22 Aug 2021, 13:36 (5 years ago) parentfc503352b1be53
2 files changed+111−20
worker/src/Endpoint/Compile.hs+109−19 worker/src/Main.hs+2−1
worker/src/Endpoint/Compile.hs+109−19
@@ -1,7 +1,8 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
33module Endpoint.Compile
4− ( endpoint
4+ ( endpoint_V1
5+ , endpoint_V2
56 , loadErrorJS
67 )
78 where
@@ -63,26 +64,50 @@ allowedOrigins =
6364
6465
6566
67+-- ENDPOINT (V1)
68+
69+
70+endpoint_V1 :: A.Artifacts -> Snap ()
71+endpoint_V1 artifacts =
72+ endpoint artifacts $ \result ->
73+ case result of
74+ Ok name js -> writeBuilder $ Html.sandwich name js
75+ Err report -> writeBuilder $ renderProblem_V1 report
76+
77+
78+
79+-- ENDPOINT (V2)
80+
81+
82+endpoint_V2 :: A.Artifacts -> Snap ()
83+endpoint_V2 artifacts =
84+ endpoint artifacts $ \result ->
85+ case result of
86+ Ok name js -> writeBuilder $ renderSuccess_V2 name js
87+ Err report -> writeBuilder $ renderProblem_V2 report
88+
89+
90+
6691-- ENDPOINT
6792
6893
69−endpoint :: A.Artifacts -> Snap ()
70−endpoint artifacts =
94+data Result
95+ = Ok N.Name B.Builder
96+ | Err Help.Report
97+
98+
99+endpoint :: A.Artifacts -> (Result -> Snap ()) -> Snap ()
100+endpoint artifacts callback =
71101 Cors.allow POST allowedOrigins $
72102 do result <- foldMultipart defaultUploadPolicy ignoreFile 0
73103 case result of
74104 ([("code",source)], 0) ->
75105 do modifyResponse $ setContentType "text/html; charset=utf-8"
76− case compile artifacts source of
77− Success builder ->
78− writeBuilder builder
79−
80− NoMain ->
81− writeBuilder $ renderReport noMain
82−
83− BadInput name err ->
84− writeBuilder $ renderReport $
85− Help.compilerReport "/" (Error.Module name "/try" File.zeroTime source err) []
106+ callback $
107+ case compile artifacts source of
108+ Success name js -> Ok name js
109+ NoMain -> Err noMain
110+ BadInput name err -> Err $ Help.compilerReport "/" (Error.Module name "/try" File.zeroTime source err) []
86111
87112 _ ->
88113 do modifyResponse $ setResponseStatus 400 "Bad Request"
@@ -104,7 +129,7 @@ ignoreFile _ _ count =
104129
105130
106131data Outcome
107− = Success B.Builder
132+ = Success N.Name B.Builder
108133 | NoMain
109134 | BadInput ModuleName.Raw Error.Error
110135
@@ -138,7 +163,7 @@ compile (A.Artifacts interfaces objects) source =
138163 mains = Map.singleton home main_
139164 graph = Opt.addLocalGraph locals objects
140165 in
141− Success $ Html.sandwich name $ JS.generate mode graph mains
166+ Success name $ JS.generate mode graph mains
142167
143168
144169checkImports :: Map.Map ModuleName.Raw I.Interface -> [Src.Import] -> Either (NE.List Import.Error) (Map.Map ModuleName.Raw I.Interface)
@@ -163,11 +188,11 @@ checkImports interfaces imports =
163188
164189
165190
166−-- RENDER REPORT
191+-- RENDER PROBLEM (V1)
167192
168193
169−renderReport :: Help.Report -> B.Builder
170−renderReport report =
194+renderProblem_V1 :: Help.Report -> B.Builder
195+renderProblem_V1 report =
171196 [r|<!DOCTYPE HTML>
172197<html>
173198<head>
@@ -179,7 +204,7 @@ renderReport report =
179204 <script>
180205 var app = Elm.Errors.init({flags:|] <> Encode.encodeUgly (Exit.toJson report) <> [r|});
181206 app.ports.jumpTo.subscribe(function(region) {
182− window.parent.postMessage(JSON.stringify(region), '*');
207+ window.parent.postMessage(JSON.stringify(region), "*");
183208 });
184209 </script>
185210</body>
@@ -187,6 +212,71 @@ renderReport report =
187212
188213
189214
215+-- RENDER SUCCESS (V2)
216+
217+
218+renderSuccess_V2 :: N.Name -> B.Builder -> B.Builder
219+renderSuccess_V2 moduleName javascript =
220+ let name = N.toBuilder moduleName in
221+ [r|<!DOCTYPE HTML>
222+<html>
223+<head>
224+ <meta charset="UTF-8">
225+ <title>|] <> name <> [r|</title>
226+ <style>body { padding: 0; margin: 0; }</style>
227+</head>
228+
229+<body>
230+
231+<pre id="elm"></pre>
232+
233+<script>
234+window.parent.postMessage("SUCCESS", "*");
235+
236+try {
237+|] <> javascript <> [r|
238+
239+ var app = Elm.|] <> name <> [r|.init({ node: document.getElementById("elm") });
240+}
241+catch (e)
242+{
243+ // display initialization errors (e.g. bad flags, infinite recursion)
244+ var header = document.createElement("h1");
245+ header.style.fontFamily = "monospace";
246+ header.innerText = "Initialization Error";
247+ var pre = document.getElementById("elm");
248+ document.body.insertBefore(header, pre);
249+ pre.innerText = e;
250+ throw e;
251+}
252+</script>
253+
254+</body>
255+</html>|]
256+
257+
258+
259+-- RENDER PROBLEM (V2)
260+
261+
262+renderProblem_V2 :: Help.Report -> B.Builder
263+renderProblem_V2 report =
264+ [r|<!DOCTYPE HTML>
265+<html>
266+<head>
267+ <meta charset="UTF-8">
268+ <style>body { padding: 0; margin: 0; display: none; }</style>
269+</head>
270+<body>
271+ <script>
272+ var errors = |] <> Encode.encodeUgly (Exit.toJson report) <> [r|;
273+ window.parent.postMessage(JSON.stringify(errors), "*");
274+ </script>
275+</body>
276+</html>|]
277+
278+
279+
190280-- NO MAIN
191281
192282
worker/src/Main.hs+2−1
@@ -31,7 +31,8 @@ main =
3131 httpServe config $ msum $
3232 [ ifTop $ status
3333 , path "repl" $ Repl.endpoint rArtifacts
34− , path "compile" $ Compile.endpoint cArtifacts
34+ , path "compile" $ Compile.endpoint_V1 cArtifacts
35+ , path "compile/v2" $ Compile.endpoint_V2 cArtifacts
3536 , path "compile/errors.js" $ serveJavaScript errorJS
3637 , path "compile/deps-info.json" $ serveDepsInfo depsInfo
3738 , notFound