-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathinline.cjs
More file actions
23 lines (18 loc) · 1.01 KB
/
inline.cjs
File metadata and controls
23 lines (18 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const fs = require('fs');
const DIST = './dist';
const UI = '/ui.html';
const SOURCE = '/ui.js';
const ENCODING = 'utf8';
const REFERENCE = '<script defer src="/ui.js"></script>';
const REFERENCE_ALT = '<script defer="defer" src="/ui.js"></script>';
let html = fs.readFileSync(DIST + UI, {encoding: ENCODING, flag: 'r'});
let src = fs.readFileSync(DIST + SOURCE, {encoding: ENCODING, flag: 'r'});
// Check for both reference formats
const referenceToUse = html.indexOf(REFERENCE) !== -1 ? REFERENCE : REFERENCE_ALT;
if (html && src && html.indexOf(referenceToUse) !== -1) {
src = src.replaceAll(/\<script\>/gi, '_script_'); // ... the script tag will break the parser, even if it is in a comment
html = html.slice(0, html.indexOf(referenceToUse)) + '<script defer>\n' + src + '\n</script>' + html.slice(html.indexOf(referenceToUse) + referenceToUse.length);
fs.writeFileSync(DIST + UI, html, {encoding: ENCODING, flag: 'w'});
fs.unlinkSync(DIST + SOURCE);
console.log('INLINED: ' + SOURCE + ' => ' + UI);
}