Skip to content

Instantly share code, notes, and snippets.

@ronanyeah
Created June 14, 2019 18:10
Show Gist options
  • Select an option

  • Save ronanyeah/08558e95f1728e3fe8052229d4280ea3 to your computer and use it in GitHub Desktop.

Select an option

Save ronanyeah/08558e95f1728e3fe8052229d4280ea3 to your computer and use it in GitHub Desktop.
const JsDom = require("jsdom").JSDOM;
const fs = require("fs");
const elmJs = fs.readFileSync(__dirname + "/elm.js", "utf8");
const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
</head>
<body>
<div id="app"></script>
</body>
</html>`;
const script = `window.Elm.Main.init({
node: document.getElementById("app"),
flags: window.flags
});`;
module.exports = flags => {
const dom = new JsDom(html, {
runScripts: "dangerously"
});
dom.window.flags = flags;
const scriptElement = dom.window.document.createElement("script");
scriptElement.textContent = elmJs + ";" + script;
dom.window.document.body.appendChild(scriptElement);
const scripts = dom.window.document.querySelectorAll("script");
Array.from(scripts).forEach(s => s.parentNode.removeChild(s));
return dom.serialize();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment