Last active
June 9, 2016 19:01
-
-
Save designfrontier/a36911c3b388a81f585ded955158a160 to your computer and use it in GitHub Desktop.
This file contains 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
// 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