-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline-css-and-js.js
More file actions
executable file
·38 lines (32 loc) · 1.75 KB
/
inline-css-and-js.js
File metadata and controls
executable file
·38 lines (32 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env node
////////////////////////////////////////////////////////////////////////////////
//
// Inline the minified assets.
//
////////////////////////////////////////////////////////////////////////////////
const fs = require('fs')
let index = fs.readFileSync('tmp/index.html', 'utf-8')
const styles = fs.readFileSync('tmp/styles.css', 'utf-8')
// Note: the odd-looking replace is necessary because, after 24 years of using JavaScript, I just discovered
// ===== that string.replace is actually broken. Its default behaviour is to use dollar-denoted patterns in
// the replacement script to affect replacement behaviour. Until now, I was not bitten by this. What it
// means is, if you’re loading in content you don’t necessarily control and it has the dollar-denoted
// patterns in it, your replacement will not work as you thought it would. In this case, it broke on:
//
// const $ = document.querySelector.bind(document)
// const $$ = document.querySelectorAll.bind(document)
//
// The code after the replacement (without the hack below) would read:
//
// const $ = document.querySelector.bind(document)
// const $ = document.querySelectorAll.bind(document)
//
// This, of course, would then throw a JavaScript error.
//
// The way replace() should be designed is to default to not using patterns in the replacement string
// unless specifically told to do so.
//
const scripts = fs.readFileSync('tmp/scripts.js', 'utf-8').replace(/\$/g, '$$$$')
index = index.replace(/<\!-- Start: styles -->.*?<\!-- End: styles -->/s, `<style>${styles}</style>`)
index = index.replace(/<\!-- Start: scripts -->.*?<\!-- End: scripts -->/s, `<script>${scripts}</script>`)
fs.writeFileSync('tmp/index.html', index)