2016-08-27 23:03:15 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const zlib = require('zlib')
|
|
|
|
const rollup = require('rollup')
|
2018-10-24 14:50:37 +00:00
|
|
|
const terser = require('terser')
|
2016-04-11 02:47:28 +00:00
|
|
|
|
2016-04-26 17:01:13 +00:00
|
|
|
if (!fs.existsSync('dist')) {
|
|
|
|
fs.mkdirSync('dist')
|
|
|
|
}
|
|
|
|
|
2016-08-27 23:03:15 +00:00
|
|
|
let builds = require('./config').getAllBuilds()
|
2016-04-25 20:37:20 +00:00
|
|
|
|
|
|
|
// filter builds via command line arg
|
|
|
|
if (process.argv[2]) {
|
2016-08-27 23:03:15 +00:00
|
|
|
const filters = process.argv[2].split(',')
|
2016-04-25 20:37:20 +00:00
|
|
|
builds = builds.filter(b => {
|
2017-10-04 22:14:53 +00:00
|
|
|
return filters.some(f => b.output.file.indexOf(f) > -1 || b._name.indexOf(f) > -1)
|
2016-04-25 20:37:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
build(builds)
|
2016-04-20 00:13:47 +00:00
|
|
|
|
|
|
|
function build (builds) {
|
2016-08-27 23:03:15 +00:00
|
|
|
let built = 0
|
|
|
|
const total = builds.length
|
|
|
|
const next = () => {
|
|
|
|
buildEntry(builds[built]).then(() => {
|
2016-04-20 00:13:47 +00:00
|
|
|
built++
|
|
|
|
if (built < total) {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}).catch(logError)
|
|
|
|
}
|
2016-08-27 23:03:15 +00:00
|
|
|
|
|
|
|
next()
|
2016-04-20 00:13:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 23:03:15 +00:00
|
|
|
function buildEntry (config) {
|
2017-09-27 22:05:39 +00:00
|
|
|
const output = config.output
|
|
|
|
const { file, banner } = output
|
2018-12-05 14:41:16 +00:00
|
|
|
const isProd = /(min|prod)\.js$/.test(file)
|
2017-07-11 07:33:19 +00:00
|
|
|
return rollup.rollup(config)
|
2017-09-27 22:05:39 +00:00
|
|
|
.then(bundle => bundle.generate(output))
|
2021-04-07 16:25:57 +00:00
|
|
|
.then(async ({ output: [{ code }] }) => {
|
2017-07-11 07:33:19 +00:00
|
|
|
if (isProd) {
|
2021-04-07 16:25:57 +00:00
|
|
|
const {code: minifiedCode} = await terser.minify(code, {
|
2018-12-05 14:41:16 +00:00
|
|
|
toplevel: true,
|
2017-07-11 07:33:19 +00:00
|
|
|
compress: {
|
2021-04-07 16:25:57 +00:00
|
|
|
pure_funcs: ['makeMap'],
|
|
|
|
},
|
|
|
|
format: {
|
|
|
|
ascii_only: true,
|
2017-07-11 07:33:19 +00:00
|
|
|
}
|
2021-04-07 16:25:57 +00:00
|
|
|
});
|
|
|
|
const minified = (banner ? banner + '\n' : '') + minifiedCode
|
2017-09-27 22:05:39 +00:00
|
|
|
return write(file, minified, true)
|
2017-07-11 07:33:19 +00:00
|
|
|
} else {
|
2017-09-27 22:05:39 +00:00
|
|
|
return write(file, code)
|
2017-07-11 07:33:19 +00:00
|
|
|
}
|
|
|
|
})
|
2016-04-20 00:13:47 +00:00
|
|
|
}
|
2016-04-11 02:47:28 +00:00
|
|
|
|
2016-11-16 21:41:44 +00:00
|
|
|
function write (dest, code, zip) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
function report (extra) {
|
|
|
|
console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''))
|
2016-04-11 02:47:28 +00:00
|
|
|
resolve()
|
2016-11-16 21:41:44 +00:00
|
|
|
}
|
2016-04-11 02:47:28 +00:00
|
|
|
|
2022-06-27 06:52:28 +00:00
|
|
|
if (!fs.existsSync(path.dirname(dest))) {
|
|
|
|
fs.mkdirSync(path.dirname(dest), { recursive: true })
|
|
|
|
}
|
2016-11-16 21:41:44 +00:00
|
|
|
fs.writeFile(dest, code, err => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
if (zip) {
|
|
|
|
zlib.gzip(code, (err, zipped) => {
|
2016-04-13 08:34:37 +00:00
|
|
|
if (err) return reject(err)
|
2016-11-16 21:41:44 +00:00
|
|
|
report(' (gzipped: ' + getSize(zipped) + ')')
|
2016-04-13 08:34:37 +00:00
|
|
|
})
|
2016-11-16 21:41:44 +00:00
|
|
|
} else {
|
|
|
|
report()
|
|
|
|
}
|
2016-04-11 02:47:28 +00:00
|
|
|
})
|
2016-11-16 21:41:44 +00:00
|
|
|
})
|
2016-04-11 02:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSize (code) {
|
|
|
|
return (code.length / 1024).toFixed(2) + 'kb'
|
|
|
|
}
|
|
|
|
|
|
|
|
function logError (e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
function blue (str) {
|
|
|
|
return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
|
|
|
|
}
|