Skip to content

Commit 84f9908

Browse files
Use ES Modules Shims for iframe shims
1 parent 91f18d1 commit 84f9908

File tree

7 files changed

+82
-233
lines changed

7 files changed

+82
-233
lines changed

client/config/dev/Try.Config.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Try.Config where
33
import Prelude
44

55
loaderUrl :: String
6-
loaderUrl = "js/output"
6+
loaderUrl = "./js/output"
77

88
compileUrl :: String
99
compileUrl = "http://localhost:8081"

client/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
"test": "spago test --path config/dev/Try.Config.purs",
77
"build": "spago build --path config/dev/Try.Config.purs",
88
"build:dev": "spago bundle-app --path config/dev/Try.Config.purs --to public/js/index.js",
9-
"build:production": "spago bundle-app --path config/prod/Try.Config.purs --purs-args '--censor-lib --strict' --to public/js/index.js"
9+
"build:production": "spago bundle-app --path config/prod/Try.Config.purs --purs-args '--censor-lib --strict' --to public/js/index.js",
10+
"serve": "http-server public/ -o / --cors=\"Access-Control-Allow-Origin: *\" -c-1",
11+
"serve:dev": "npm run build:dev && npm run serve"
1012
},
1113
"devDependencies": {
14+
"http-server": "^14.1.0",
1215
"purescript": "^0.13.6",
1316
"purescript-psa": "^0.7.3",
1417
"rimraf": "^2.5.4",

client/public/frame.html

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,57 @@
55
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
66
<meta content="utf-8" http-equiv="encoding">
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8-
<script src="js/frame.js"></script>
8+
<script noshim>
9+
window.esmsInitOptions = {
10+
// -- Hooks --
11+
// Module load error
12+
onerror: (e) => {
13+
console.log("Error while loading module: ");
14+
console.log(e);
15+
throw e;
16+
},
17+
};
18+
</script>
19+
<!--
20+
JSPM Generator Import Map
21+
Edit URL: https://generator.jspm.io/#Y2NnYGCzD80rySzJSU1hSMpM183MK0lNTy1yMNQz0zM1ZEhJTc7MTczRyyp2MDTQM9YzZChKTUwu0U3Jz3UwNNMzxCqiX5xaVJZaBJGAKystzUxxsACaYQQAoBlP83cA
22+
-->
23+
<script type="importmap">
24+
{
25+
"imports": {
26+
"big-integer": "https://ga.jspm.io/npm:[email protected]/BigInteger.js",
27+
"decimal.js": "https://ga.jspm.io/npm:[email protected]/decimal.js",
28+
"react": "https://ga.jspm.io/npm:[email protected]/index.js",
29+
"react-dom": "https://ga.jspm.io/npm:[email protected]/index.js",
30+
"react-dom/server": "https://ga.jspm.io/npm:[email protected]/server.browser.js",
31+
"uuid": "https://ga.jspm.io/npm:[email protected]/dist/esm-browser/index.js"
32+
},
33+
"scopes": {
34+
"https://ga.jspm.io/": {
35+
"object-assign": "https://ga.jspm.io/npm:[email protected]/index.js",
36+
"react": "https://ga.jspm.io/npm:[email protected]/index.js",
37+
"scheduler": "https://ga.jspm.io/npm:[email protected]/index.js"
38+
}
39+
}
40+
}
41+
</script>
42+
43+
<!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support (all except Chrome 89+) -->
44+
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js" integrity="sha384-ZGtUNdKMtA2sSrO9dN9d2TOeiSlXlDSrOrasgO9YoKR4LsDV7RLdUvvX1M0gCkl2" crossorigin="anonymous"></script>
45+
46+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/BigInteger.js" integrity="sha384-Lxn1K4ox8//F5O+uihYrTIJvbfM+UJaRL2SdFA2s/z0v6i5TrWg9B8RzfjM60R9y"/>
47+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/decimal.js" integrity="sha384-camBDJB1KoavtyRdQfdzzR4mj+uAKnNlvseJIwqKYOaHWnjZmnXR+1vyBP1WlZd0"/>
48+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" integrity="sha384-iQp1zoaqIhfUYyYkz3UNk1QeFfmBGgt1Ojq0kZD5Prql1g7fgJVzVgsjDoR65lv8"/>
49+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/cjs/react-dom-server.browser.production.min.js" integrity="sha384-XL5bjsaJ5UPjTIPsyzUfscq41OFbVXsLojl53DCgFfdzKNNUL2MOSd0WM/upjd53"/>
50+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/cjs/react-dom.production.min.js" integrity="sha384-HDQjgsF+F2j5XuiiDtCiuLA1vgyHr9ONiNrBhKmJEVa43KqIZdAB7VN2+QjUGwvq"/>
51+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" integrity="sha384-zAu0L7n/xnRAG7+D/qd1K4E1/2tYK4hL3puH/5YEliHNnO3OOt5jUhnnemFTHmIG"/>
52+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/server.browser.js" integrity="sha384-dsW+4a3rjWELxqtR1rVEIeLNHGyMjRFwdBVz6OG30oBLs8tO2/dYUIxEvc515PSZ"/>
53+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" integrity="sha384-T0mNrKFgmKBye+m7XrpShrh+7kfix2zQd9qrR8xytnppcxESpgzrb4IJXvE+uJcl"/>
54+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/cjs/react.production.min.js" integrity="sha384-pTMZhybzHZ+1G029kWUmoGvTrBp1C+2oJAkZV48BBq7+e6Hk3bGuXtvxT2vQfBqj"/>
55+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" integrity="sha384-jVagjV+2YtlseazU2byX6gMLPHaA5Ps2c6HhvsGDlWjn45YCCoU1q+QtQTOb1MjT"/>
56+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" integrity="sha384-HIW1B3OQdGPkUEgh29MiYoUKQp+mFTjw50hRVvGhZgUaNYrUFYN07a+3CqC0/I7L"/>
57+
<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/dist/esm-browser/index.js" integrity="sha384-y7UmGnwe+/V/S4AD5T6MLU9dHjA3PLvuVGNhrqZApiNbq9Lf8s3aNRrcnzOa+qtR"/>
58+
<script src="js/frame.js" crossorigin="anonymous" noshim></script>
959
</head>
1060
<body>
1161
<main id="main"></main>

client/public/js/frame.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,25 @@
11
(function() {
2-
function evalSources(sources) {
3-
var modules = {};
4-
function dirname(str) {
5-
var ix = str.lastIndexOf("/");
6-
return ix < 0 ? "" : str.slice(0, ix);
7-
}
8-
function resolvePath(a, b) {
9-
if (b[0] === "." && b[1] === "/") {
10-
return dirname(a) + b.slice(1);
11-
}
12-
if (b[0] === "." && b[1] === "." && b[2] === "/") {
13-
return dirname(dirname(a)) + b.slice(2);
14-
}
15-
return b;
16-
}
17-
return function load(name) {
18-
if (modules[name]) {
19-
return modules[name].exports;
20-
}
21-
function require(path) {
22-
return load(resolvePath(name, path));
23-
}
24-
var module = modules[name] = { exports: {} };
25-
new Function("module", "exports", "require", sources[name])(module, module.exports, require);
26-
return module.exports;
27-
};
28-
}
29-
302
var parent;
313

324
document.addEventListener("DOMContentLoaded", function() {
335
window.addEventListener("message", function(event) {
346
parent = event.source;
357
parent.postMessage("trypurescript", "*");
36-
var file = evalSources(event.data)("<file>");
37-
if (file.main && typeof file.main === "function") {
38-
file.main();
8+
const scriptEl = document.createElement("script");
9+
scriptEl.type = "module";
10+
const code = event.data.code;
11+
const url = event.data.url;
12+
const codeFixedImports = code
13+
.split("\n")
14+
.map((line) => line.replace(/^import (.+) from "..\/([^"]+)";$/, `import $1 from "${url}/$2";`))
15+
.join("\n");
16+
// See https://stackoverflow.com/a/6433770
17+
try {
18+
scriptEl.appendChild(document.createTextNode(codeFixedImports));
19+
} catch (e) {
20+
scriptEl.text = codeFixedImports;
21+
} finally {
22+
document.body.appendChild(scriptEl);
3923
}
4024
}, { once: true });
4125
}, { once: true });

client/src/Try/Container.purs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ import Try.Editor (MarkerType(..), toStringMarkerType)
2929
import Try.Editor as Editor
3030
import Try.Gist (getGistById, tryLoadFileFromGist)
3131
import Try.GitHub (getRawGitHubFile)
32-
import Try.Loader (Loader, makeLoader, runLoader)
3332
import Try.QueryString (getQueryStringMaybe)
3433
import Try.Session (createSessionIdIfNecessary, storeSession, tryRetrieveSession)
35-
import Try.Types (JS(..))
3634
import Web.HTML (window)
3735
import Web.HTML.Window (alert)
3836

@@ -83,12 +81,9 @@ data Action
8381
_editor :: SProxy "editor"
8482
_editor = SProxy
8583

86-
loader :: Loader
87-
loader = makeLoader Config.loaderUrl
88-
8984
type LoadCb = Effect Unit
9085
type FailCb = Effect Unit
91-
foreign import setupIFrame :: EffectFn3 (Object JS) LoadCb FailCb Unit
86+
foreign import setupIFrame :: EffectFn3 { code :: String, url :: String } LoadCb FailCb Unit
9287
foreign import teardownIFrame :: Effect Unit
9388

9489
component :: forall q i o. H.Component HH.HTML q i o Aff
@@ -189,22 +184,20 @@ component = H.mkComponent
189184
if settings.showJs then
190185
H.liftEffect teardownIFrame
191186
else do
192-
eitherSources <- H.liftAff $ runExceptT $ runLoader loader (JS js)
193187
for_ warnings \warnings_ -> do
194188
let anns = Array.mapMaybe (toAnnotation MarkerWarning) warnings_
195189
_ <- H.query _editor unit $ H.tell $ Editor.SetAnnotations anns
196190
pure unit
197-
case eitherSources of
198-
Right sources -> do
199-
let eventData = Object.insert "<file>" (JS js) sources
200-
H.liftAff $ makeAff \f -> do
201-
runEffectFn3 setupIFrame eventData (f (Right unit)) (f (Left $ Aff.error "Could not load iframe"))
202-
mempty
203-
H.modify_ _ { compiled = Just (Right res) }
204-
Left err -> do
205-
H.liftEffect teardownIFrame
206-
H.liftEffect $ error err
207-
H.modify_ _ { compiled = Just (Left err) }
191+
let
192+
eventData =
193+
{ code: js
194+
, url: Config.loaderUrl
195+
}
196+
H.liftEffect teardownIFrame
197+
H.liftAff $ makeAff \f -> do
198+
runEffectFn3 setupIFrame eventData (f (Right unit)) (f (Left $ Aff.error "Could not load iframe"))
199+
mempty
200+
H.modify_ _ { compiled = Just (Right res) }
208201

209202
HandleEditor (Editor.TextChanged text) -> do
210203
_ <- H.fork $ handleAction $ Cache text

client/src/Try/Loader.purs

Lines changed: 0 additions & 142 deletions
This file was deleted.

client/src/Try/Shim.purs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)