2018-09-23 12:26:01 -07:00
|
|
|
import crypto from 'crypto'
|
|
|
|
import fs from 'fs'
|
2019-01-19 15:06:25 -08:00
|
|
|
import { promisify } from 'util'
|
2018-09-23 12:26:01 -07:00
|
|
|
import path from 'path'
|
2018-12-08 11:21:54 -08:00
|
|
|
import { rollup } from 'rollup'
|
|
|
|
import { terser } from 'rollup-plugin-terser'
|
2020-01-12 10:32:40 -08:00
|
|
|
import replace from '@rollup/plugin-replace'
|
2018-12-08 11:21:54 -08:00
|
|
|
import fromPairs from 'lodash-es/fromPairs'
|
2018-12-11 07:31:48 -08:00
|
|
|
import { themes } from '../src/routes/_static/themes'
|
2019-10-24 19:03:10 -07:00
|
|
|
import terserOptions from './terserOptions'
|
2018-09-23 12:26:01 -07:00
|
|
|
|
2019-01-19 15:06:25 -08:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
2018-04-14 15:50:16 -07:00
|
|
|
|
2018-12-08 11:21:54 -08:00
|
|
|
const themeColors = fromPairs(themes.map(_ => ([_.name, _.color])))
|
|
|
|
|
2018-12-17 17:21:29 -08:00
|
|
|
export async function buildInlineScript () {
|
2019-08-03 13:49:37 -07:00
|
|
|
const inlineScriptPath = path.join(__dirname, '../src/inline-script/inline-script.js')
|
2018-04-14 15:50:16 -07:00
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const bundle = await rollup({
|
2018-12-08 11:21:54 -08:00
|
|
|
input: inlineScriptPath,
|
|
|
|
plugins: [
|
|
|
|
replace({
|
|
|
|
'process.browser': true,
|
|
|
|
'process.env.THEME_COLORS': JSON.stringify(themeColors)
|
|
|
|
}),
|
2019-10-24 19:03:10 -07:00
|
|
|
!process.env.DEBUG && terser(terserOptions)
|
2018-12-08 11:21:54 -08:00
|
|
|
]
|
|
|
|
})
|
2019-08-03 13:49:37 -07:00
|
|
|
const { output } = await bundle.generate({
|
2018-12-08 11:21:54 -08:00
|
|
|
format: 'iife',
|
|
|
|
sourcemap: true
|
|
|
|
})
|
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const { code, map } = output[0]
|
2019-01-01 10:42:50 -08:00
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const fullCode = `${code}//# sourceMappingURL=/inline-script.js.map`
|
|
|
|
const checksum = crypto.createHash('sha256').update(fullCode).digest('base64')
|
2018-04-14 15:50:16 -07:00
|
|
|
|
2019-01-26 10:14:15 -08:00
|
|
|
await writeFile(path.resolve(__dirname, '../src/inline-script/checksum.js'),
|
|
|
|
`module.exports = ${JSON.stringify(checksum)}`, 'utf8')
|
2018-12-17 17:21:29 -08:00
|
|
|
await writeFile(path.resolve(__dirname, '../static/inline-script.js.map'),
|
|
|
|
map.toString(), 'utf8')
|
2018-12-08 11:21:54 -08:00
|
|
|
|
2018-12-17 17:21:29 -08:00
|
|
|
return '<script>' + fullCode + '</script>'
|
2018-04-14 15:50:16 -07:00
|
|
|
}
|