2018-04-15 00:50:16 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-09-23 21:26:01 +02:00
|
|
|
import crypto from 'crypto'
|
|
|
|
import fs from 'fs'
|
|
|
|
import pify from 'pify'
|
|
|
|
import path from 'path'
|
2018-12-08 20:21:54 +01:00
|
|
|
import { rollup } from 'rollup'
|
|
|
|
import { terser } from 'rollup-plugin-terser'
|
|
|
|
import replace from 'rollup-plugin-replace'
|
|
|
|
import fromPairs from 'lodash-es/fromPairs'
|
2018-12-11 16:31:48 +01:00
|
|
|
import { themes } from '../src/routes/_static/themes'
|
2018-09-23 21:26:01 +02:00
|
|
|
|
2018-04-15 00:50:16 +02:00
|
|
|
const readFile = pify(fs.readFile.bind(fs))
|
|
|
|
const writeFile = pify(fs.writeFile.bind(fs))
|
|
|
|
|
2018-12-08 20:21:54 +01:00
|
|
|
const themeColors = fromPairs(themes.map(_ => ([_.name, _.color])))
|
|
|
|
|
2018-04-15 00:50:16 +02:00
|
|
|
async function main () {
|
2018-09-23 21:26:01 +02:00
|
|
|
let inlineScriptPath = path.join(__dirname, '../inline-script.js')
|
2018-04-15 00:50:16 +02:00
|
|
|
|
2018-12-08 20:21:54 +01:00
|
|
|
let bundle = await rollup({
|
|
|
|
input: inlineScriptPath,
|
|
|
|
plugins: [
|
|
|
|
replace({
|
|
|
|
'process.browser': true,
|
|
|
|
'process.env.THEME_COLORS': JSON.stringify(themeColors)
|
|
|
|
}),
|
|
|
|
terser({
|
|
|
|
mangle: true,
|
|
|
|
compress: true
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
|
|
|
let { code, map } = await bundle.generate({
|
|
|
|
format: 'iife',
|
|
|
|
sourcemap: true
|
|
|
|
})
|
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
let fullCode = `${code}\n//# sourceMappingURL=/inline-script.js.map`
|
2018-12-08 20:21:54 +01:00
|
|
|
|
|
|
|
let checksum = crypto.createHash('sha256').update(fullCode).digest('base64')
|
2018-04-15 00:50:16 +02:00
|
|
|
|
|
|
|
let checksumFilepath = path.join(__dirname, '../inline-script-checksum.json')
|
2018-08-30 06:42:57 +02:00
|
|
|
await writeFile(checksumFilepath, JSON.stringify({ checksum }), 'utf8')
|
2018-04-15 00:50:16 +02:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
let htmlTemplateFilepath = path.join(__dirname, '../src/template.html')
|
|
|
|
let htmlTemplateFile = await readFile(htmlTemplateFilepath, 'utf8')
|
|
|
|
htmlTemplateFile = htmlTemplateFile.replace(
|
2018-04-15 00:50:16 +02:00
|
|
|
/<!-- insert inline script here -->[\s\S]+<!-- end insert inline script here -->/,
|
2018-12-08 20:21:54 +01:00
|
|
|
'<!-- insert inline script here --><script>' + fullCode + '</script><!-- end insert inline script here -->'
|
2018-04-15 00:50:16 +02:00
|
|
|
)
|
2018-12-11 16:31:48 +01:00
|
|
|
await writeFile(htmlTemplateFilepath, htmlTemplateFile, 'utf8')
|
2018-12-08 20:21:54 +01:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
await writeFile(path.resolve(__dirname, '../static/inline-script.js.map'), map.toString(), 'utf8')
|
2018-04-15 00:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|