Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Last active June 9, 2016 19:01
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 designfrontier/a36911c3b388a81f585ded955158a160 to your computer and use it in GitHub Desktop.
Save designfrontier/a36911c3b388a81f585ded955158a160 to your computer and use it in GitHub Desktop.
// abs.js
const FILES = require('fs');
module.exports = {
absorb: (htmlPath) => {
let html = FILES.readFileSync(htmlPath, 'utf8');
html = html.replace(/(<absorb\s(.+)\/>)/g, function(absorbElement) {
let filePath = '', fileType = '', jsMod = '';
if (absorbElement.indexOf('css=') >= 0) {
fileType = 'css';
filePath = absorbElement.slice(
absorbElement.indexOf('css=') + 5,
absorbElement.indexOf('/>') - 1
); // filePath
} else if (absorbElement.indexOf('js=') > -1) {
jsMod = (absorbElement.indexOf(' defer ') > -1 ? ' defer' : '');
jsMod = (absorbElement.indexOf(' async ') > -1 ? ' async' : '');
fileType = 'js';
filePath = absorbElement.slice(
absorbElement.indexOf('js=') + 4,
absorbElement.indexOf('/>') -1
); // filePath
}; // if/else
let code = FILES.readFileSync(filePath, 'utf8');
if (fileType == 'css') return `<style>${code}</style>`;
if (fileType = 'js') return `<script${jsMod}>${code}</script>`;
return `<script async>alert('ERR: ${filePath}')</script>`;
});
return html;
} // absorb()
}; // module.exports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment