1
0
mirror of https://github.com/nolanlawson/pinafore synced 2024-12-13 18:57:02 +01:00
Pinafore-Web-Client-Frontend/bin/build-inline-script.js
greenkeeper[bot] 26d0b827bc Update rollup to the latest version 🚀 (#860)
* fix(package): update rollup to version 1.0.0

* chore(package): update lockfile package-lock.json

* fix inline script script
2019-01-01 10:42:50 -08:00

48 lines
1.3 KiB
JavaScript

import crypto from 'crypto'
import fs from 'fs'
import pify from 'pify'
import path from 'path'
import { rollup } from 'rollup'
import { terser } from 'rollup-plugin-terser'
import replace from 'rollup-plugin-replace'
import fromPairs from 'lodash-es/fromPairs'
import { themes } from '../src/routes/_static/themes'
const writeFile = pify(fs.writeFile.bind(fs))
const themeColors = fromPairs(themes.map(_ => ([_.name, _.color])))
export async function buildInlineScript () {
let inlineScriptPath = path.join(__dirname, '../inline-script.js')
let bundle = await rollup({
input: inlineScriptPath,
plugins: [
replace({
'process.browser': true,
'process.env.THEME_COLORS': JSON.stringify(themeColors)
}),
terser({
mangle: true,
compress: true
})
]
})
let { output } = await bundle.generate({
format: 'iife',
sourcemap: true
})
let { code, map } = output[0]
let fullCode = `${code}//# sourceMappingURL=/inline-script.js.map`
let checksum = crypto.createHash('sha256').update(fullCode).digest('base64')
await writeFile(path.resolve(__dirname, '../inline-script-checksum.json'),
JSON.stringify({ checksum }), 'utf8')
await writeFile(path.resolve(__dirname, '../static/inline-script.js.map'),
map.toString(), 'utf8')
return '<script>' + fullCode + '</script>'
}