Skip to content

Instantly share code, notes, and snippets.

@ivarne
Created November 21, 2018 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivarne/c29240f544c148daa2f6718001a5c2d1 to your computer and use it in GitHub Desktop.
Save ivarne/c29240f544c148daa2f6718001a5c2d1 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
const asset_manifest = JSON.parse(
fs.readFileSync("./build/asset-manifest.json", { encoding: "utf8" })
);
const startupScript = fs.readFileSync(
path.join("./build", asset_manifest["runtime~main.js"])
);
const scripts = Object.keys(asset_manifest)
.filter(key => key === "main.js" || key.endsWith(".chunk.js"))
.map(key => asset_manifest[key]);
const styles = Object.keys(asset_manifest)
.filter(key => key === "main.css" || key.endsWith(".chunk.css"))
.map(key => asset_manifest[key]);
fs.writeFileSync(
"./build/widget.js",
`${startupScript}
(function(){
// Include required scripts for the widget
${scripts
.map(
(script, i) => `var script${i} = document.createElement('script');
script${i}.src = "${script}";
document.body.append(script${i});
`
)
.join("\n ")}
// Include styles
${styles
.map(
(style, i) => `var style${i} = document.createElement('link');
style${i}.rel = "stylesheet";
style${i}.href = "${style}";
document.head.appendChild(style${i});
`
)
.join("\n ")}
})()
`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment