Created
June 14, 2019 18:10
-
-
Save ronanyeah/08558e95f1728e3fe8052229d4280ea3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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