Skip to content

Load individual modules on demand #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/.psc*
/.purs*
/.psa*
/.stack*
105 changes: 49 additions & 56 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,61 @@
"output"
],
"dependencies": {
"purescript-console": "^3.0.0",
"purescript-foreign-generic": "^5.0.0",
"purescript-jquery": "^4.2.1",
"purescript-js-timers": "^3.0.0",
"purescript-parallel": "^3.2.0",
"purescript-prelude": "^3.1.0",
"purescript-random": "^3.0.0"
"purescript-console": "^4.1.0",
"purescript-foreign-generic": "^7.0.0",
"purescript-jquery": "^5.0.0",
"purescript-js-timers": "^4.0.1",
"purescript-parallel": "^4.0.0",
"purescript-prelude": "^4.1.0",
"purescript-random": "^4.0.0",
"purescript-web-html": "^1.2.0"
},
"devDependencies": {
"ace": "^1.2.8",
"jquery": "^3.2.1",
"underscore": "^1.8.3",
"react": "^15.6.1",
"react": "^16.1.0",
"mathbox": "^0.0.5",
"purescript-thermite": "^4.1.1",
"purescript-behaviors": "^5.2.0",
"purescript-slides": "^0.5.0",
"purescript-flare": "^3.2.0",
"purescript-arrays": "^4.1.2",
"purescript-bifunctors": "^3.0.0",
"purescript-console": "^3.0.0",
"purescript-const": "^3.0.0",
"purescript-contravariant": "^3.1.0",
"purescript-control": "^3.3.0",
"purescript-distributive": "^3.0.0",
"purescript-eff": "^3.1.0",
"purescript-either": "^3.1.0",
"purescript-enums": "^3.2.1",
"purescript-exceptions": "^3.1.0",
"purescript-exists": "^3.0.0",
"purescript-foldable-traversable": "^3.6.0",
"purescript-foreign": "^4.0.1",
"purescript-free": "^4.1.0",
"purescript-functions": "^3.0.0",
"purescript-functors": "^2.2.0",
"purescript-generics": "^4.0.0",
"purescript-generics-rep": "^5.2.0",
"purescript-globals": "^3.0.0",
"purescript-graphs": "^3.0.0",
"purescript-identity": "^3.1.0",
"purescript-inject": "^4.0.0",
"purescript-integers": "^3.1.0",
"purescript-lazy": "^3.0.0",
"purescript-maps": "^3.5.2",
"purescript-arrays": "^5.1.0",
"purescript-bifunctors": "^4.0.0",
"purescript-console": "^4.1.0",
"purescript-const": "^4.1.0",
"purescript-contravariant": "^4.0.0",
"purescript-control": "^4.1.0",
"purescript-distributive": "^4.0.0",
"purescript-effect": "^2.0.0",
"purescript-either": "^4.1.0",
"purescript-enums": "^4.0.0",
"purescript-exceptions": "^4.0.0",
"purescript-exists": "^4.0.0",
"purescript-foldable-traversable": "^4.1.0",
"purescript-foreign": "^5.0.0",
"purescript-foreign-object": "^1.0.0",
"purescript-free": "^5.1.0",
"purescript-functions": "^4.0.0",
"purescript-functors": "^3.1.0",
"purescript-generics-rep": "^6.1.0",
"purescript-globals": "^4.0.0",
"purescript-identity": "^4.1.0",
"purescript-integers": "^4.0.0",
"purescript-lazy": "^4.0.0",
"purescript-math": "^2.1.0",
"purescript-maybe": "^3.0.0",
"purescript-monoid": "^3.1.0",
"purescript-prelude": "^3.1.0",
"purescript-profunctor": "^3.2.0",
"purescript-proxy": "^2.1.0",
"purescript-quickcheck": "^4.4.0",
"purescript-random": "^3.0.0",
"purescript-refs": "^3.0.0",
"purescript-semirings": "^4.0.0",
"purescript-sets": "^3.0.0",
"purescript-st": "^3.0.0",
"purescript-strings": "^3.3.1",
"purescript-tailrec": "^3.3.0",
"purescript-transformers": "^3.4.0",
"purescript-tuples": "^4.1.0",
"purescript-typelevel-prelude": "^2.4.0",
"purescript-unfoldable": "^3.0.0",
"purescript-validation": "^3.1.0"
"purescript-maybe": "^4.0.0",
"purescript-ordered-collections": "^1.0.0",
"purescript-prelude": "^4.1.0",
"purescript-profunctor": "^4.0.0",
"purescript-proxy": "^3.0.0",
"purescript-quickcheck": "^5.0.0",
"purescript-random": "^4.0.0",
"purescript-refs": "^4.1.0",
"purescript-semirings": "^5.0.0",
"purescript-st": "^4.0.0",
"purescript-strings": "^4.0.0",
"purescript-tailrec": "^4.0.0",
"purescript-transformers": "^4.1.0",
"purescript-tuples": "^5.1.0",
"purescript-typelevel-prelude": "^3.0.0",
"purescript-unfoldable": "^4.0.0",
"purescript-validation": "^4.0.0"
}
}
13 changes: 13 additions & 0 deletions frame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Try PureScript!</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/frame.js"></script>
</head>
<body>
<main id="main"></main>
</body>
</html>
67 changes: 20 additions & 47 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,6 @@
</a>
</li>
</ul>
</li><li class="menu-item menu-dropdown no-mobile">
<label title="Select a Backend">Backend</label>
<ul id="backend">
<li>
<input type="radio" name="backend_inputs" value="core" id="backend_core" checked="true">
<label for="backend_core" title="Use the core libraries backend">Core</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="thermite" id="backend_thermite">
<label for="backend_thermite" title="Use the try-thermite backend">Thermite</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="slides" id="backend_slides">
<label for="backend_slides" title="Use the try-slides backend">Slides</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="flare" id="backend_flare">
<label for="backend_flare" title="Use the try-flare backend">Flare</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="mathbox" id="backend_mathbox">
<label for="backend_mathbox" title="Use the try-mathbox backend">Mathbox</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="behaviors" id="backend_behaviors">
<label for="backend_behaviors" title="Use the try-behaviors backend">Behaviors</label>
</li>
</ul>
</li><li class="menu-item view_gist_li mobile-only">
<a class="view_gist" target="trypurs_gist">
<label title="Open the original gist in a new window">View Gist</label>
Expand Down Expand Up @@ -186,30 +158,31 @@
})(marker));
}

function setupIFrame($ctr, html, js) {
var $iframe = $('<iframe id="output-iframe">');
function setupIFrame($ctr, data) {
var $iframe = $('<iframe sandbox="allow-scripts" id="output-iframe" src="frame.html">');

$ctr
.empty()
.append($iframe);

var iframe = $iframe.get(0).contentWindow.document;
iframe.open();
iframe.write(html);
iframe.close();

var script = iframe.createElement('script');
script.appendChild(iframe.createTextNode(js));

$iframe.ready(function() {
var checkExists = setInterval(function() {
var body = iframe.getElementsByTagName('body')[0];
if (body) {
body.appendChild(script);
clearInterval(checkExists);
}
}, 100);
});
var tries = 0;
var sendSources = setInterval(function() {
// Stop after 10 seconds
if (tries >= 100) {
return clearInterval(sendSources);
}
tries++;
var iframe = $iframe.get(0).contentWindow;
if (iframe) {
iframe.postMessage(data, "*");
} else {
console.warn("Frame is not available");
}
}, 100);

window.addEventListener("message", function() {
clearInterval(sendSources);
}, { once: true });

return $iframe;
}
Expand Down
39 changes: 39 additions & 0 deletions js/frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(function() {
function evalSources(sources) {
var modules = {};
function dirname(str) {
var ix = str.lastIndexOf("/");
return ix < 0 ? "" : str.slice(0, ix);
}
function resolvePath(a, b) {
if (b[0] === "." && b[1] === "/") {
return dirname(a) + b.slice(1);
}
if (b[0] === "." && b[1] === "." && b[2] === "/") {
return dirname(dirname(a)) + b.slice(2);
}
return b;
}
return function load(name) {
if (modules[name]) {
return modules[name].exports;
}
function require(path) {
return load(resolvePath(name, path));
}
var module = modules[name] = { exports: {} };
new Function("module", "exports", "require", sources[name])(module, module.exports, require);
return module.exports;
};
}

document.addEventListener("DOMContentLoaded", function() {
window.addEventListener("message", function(event) {
event.source.postMessage("trypurescript", "*");
var file = evalSources(event.data)("<file>");
if (file.main && typeof file.main === "function") {
file.main();
}
}, { once: true });
}, { once: true });
})();
Loading