-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
43 lines (37 loc) · 1.1 KB
/
build.js
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
39
40
41
42
43
const fs = require('fs')
const { rollup } = require('rollup')
const { minify } = require('terser')
const pb = require('pretty-bytes')
const gzip = require('gzip-size')
const pkg = require('./package')
const banner = `/*!
* Scramble v${pkg.version}
* Copyright (c) ${new Date().getFullYear()} Ignatius Bagus
* MIT Licensed
* scramble.js.org
*/
`
function format(type, file, name) {
const format = {
banner,
format: type,
file: file
}
if (name) format.name = name
return format
}
console.info('Compiling...')
;(async () => {
const bundle = await rollup({ input: 'src/index.js' })
bundle.write(format('cjs', pkg.main))
bundle.write(format('es', pkg.module))
const umd = pkg['umd:main']
await bundle.write(format('umd', umd, pkg['umd:name']))
const data = fs.readFileSync(umd, 'utf8')
const { code, error } = minify(data) // minify code
if (error) return console.error(error.message)
fs.writeFileSync(umd, `${banner}\n${code}`) // rewrite banner
const gzipSize = gzip.sync(code)
console.info('Compilation was successful!')
console.info(`~> gzip size: ${pb(gzipSize)}`)
})()