2018-12-18 02:21:29 +01:00
|
|
|
import chokidar from 'chokidar'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
2019-01-20 00:06:25 +01:00
|
|
|
import { promisify } from 'util'
|
2018-12-18 02:21:29 +01:00
|
|
|
import { buildSass } from './build-sass'
|
|
|
|
import { buildInlineScript } from './build-inline-script'
|
|
|
|
import { buildSvg } from './build-svg'
|
|
|
|
import now from 'performance-now'
|
|
|
|
import debounce from 'lodash-es/debounce'
|
|
|
|
|
2019-01-20 00:06:25 +01:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
2018-12-18 02:21:29 +01:00
|
|
|
|
|
|
|
const DEBOUNCE = 500
|
|
|
|
|
|
|
|
const builders = [
|
|
|
|
{
|
2019-01-26 18:34:05 +01:00
|
|
|
watch: 'src/scss',
|
2018-12-18 02:21:29 +01:00
|
|
|
comment: '<!-- inline CSS -->',
|
|
|
|
rebuild: buildSass
|
|
|
|
},
|
|
|
|
{
|
|
|
|
watch: 'inline-script.js',
|
|
|
|
comment: '<!-- inline JS -->',
|
|
|
|
rebuild: buildInlineScript
|
|
|
|
},
|
|
|
|
{
|
2019-01-26 18:34:05 +01:00
|
|
|
watch: 'src/bin/svgs.js',
|
2018-12-18 02:21:29 +01:00
|
|
|
comment: '<!-- inline SVG -->',
|
|
|
|
rebuild: buildSvg
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// array of strings and builder functions, we build this on-the-fly
|
|
|
|
const partials = buildPartials()
|
|
|
|
|
|
|
|
function buildPartials () {
|
2019-01-19 22:32:36 +01:00
|
|
|
let rawTemplate = fs.readFileSync(path.resolve(__dirname, '../src/build/template.html'), 'utf8')
|
2018-12-18 02:21:29 +01:00
|
|
|
|
|
|
|
let partials = [rawTemplate]
|
|
|
|
|
|
|
|
builders.forEach(builder => {
|
|
|
|
for (let i = 0; i < partials.length; i++) {
|
|
|
|
let partial = partials[i]
|
|
|
|
if (typeof partial !== 'string') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let idx = partial.indexOf(builder.comment)
|
|
|
|
if (idx !== -1) {
|
|
|
|
partials.splice(
|
|
|
|
i,
|
|
|
|
1,
|
|
|
|
partial.substring(0, idx),
|
|
|
|
builder,
|
|
|
|
partial.substring(idx + builder.comment.length)
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return partials
|
|
|
|
}
|
|
|
|
|
|
|
|
function doWatch () {
|
|
|
|
// rebuild each of the partials on-the-fly if something changes
|
|
|
|
partials.forEach(partial => {
|
|
|
|
if (typeof partial === 'string') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
chokidar.watch(partial.watch).on('change', debounce(path => {
|
|
|
|
console.log(`Detected change in ${path}...`)
|
|
|
|
delete partial.result
|
|
|
|
buildAll()
|
|
|
|
}), DEBOUNCE)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildAll () {
|
|
|
|
let start = now()
|
|
|
|
let html = (await Promise.all(partials.map(async partial => {
|
|
|
|
if (typeof partial === 'string') {
|
|
|
|
return partial
|
|
|
|
}
|
|
|
|
if (!partial.result) {
|
|
|
|
partial.result = partial.comment + '\n' + (await partial.rebuild())
|
|
|
|
}
|
|
|
|
return partial.result
|
|
|
|
}))).join('')
|
|
|
|
|
|
|
|
await writeFile(path.resolve(__dirname, '../src/template.html'), html, 'utf8')
|
|
|
|
let end = now()
|
|
|
|
console.log(`Built template.html in ${(end - start).toFixed(2)}ms`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
if (process.argv.includes('--watch')) {
|
|
|
|
doWatch()
|
2018-12-18 07:42:39 +01:00
|
|
|
} else {
|
|
|
|
await buildAll()
|
2018-12-18 02:21:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|