2018-08-22 16:41:26 +08:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import zlib from 'zlib'
|
|
|
|
import uglify from 'uglify-js'
|
|
|
|
import { rollup } from 'rollup'
|
|
|
|
import config from './config'
|
|
|
|
|
|
|
|
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`
|
|
|
|
}
|
|
|
|
|
|
|
|
function write(dest, code, zip) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
function report(extra) {
|
2019-01-19 15:56:21 +08:00
|
|
|
console.log(`${blue(path.relative(process.cwd(), dest))} ${getSize(code)}${extra || ''}`)
|
2018-08-22 16:41:26 +08:00
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(dest, code, error => {
|
|
|
|
if (error) return reject(error)
|
|
|
|
if (zip) {
|
|
|
|
zlib.gzip(code, (err, zipped) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
report(` (gzipped: ${getSize(zipped)})`)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
report()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildEntry({ input, output }) {
|
|
|
|
const isProd = /min\.js$/.test(output.file)
|
|
|
|
return rollup(input)
|
|
|
|
.then(bundle => bundle.generate(output))
|
2019-01-19 15:56:21 +08:00
|
|
|
.then(response => {
|
|
|
|
// console.log({ bla })
|
2018-08-22 16:41:26 +08:00
|
|
|
if (isProd) {
|
2019-01-19 15:56:21 +08:00
|
|
|
const minified = uglify.minify(response.output[0].code, {
|
2018-08-22 16:41:26 +08:00
|
|
|
output: {
|
|
|
|
preamble: output.banner,
|
|
|
|
ascii_only: true,
|
|
|
|
},
|
|
|
|
}).code
|
|
|
|
return write(output.file, minified, true)
|
|
|
|
}
|
2019-01-19 15:56:21 +08:00
|
|
|
// console.log({ isProd })
|
|
|
|
// console.dir(response, { depth: null })
|
|
|
|
return write(output.file, response.output[0].code)
|
2018-08-22 16:41:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function build(builds) {
|
|
|
|
let built = 0
|
|
|
|
const total = builds.length
|
|
|
|
const next = () => {
|
2018-08-30 04:28:50 +08:00
|
|
|
const distPath = path.dirname(builds[built].output.file)
|
|
|
|
|
|
|
|
if (!fs.existsSync(distPath)) {
|
|
|
|
fs.mkdirSync(distPath)
|
|
|
|
}
|
|
|
|
|
2018-08-22 16:41:26 +08:00
|
|
|
buildEntry(builds[built]).then(() => {
|
|
|
|
built++
|
|
|
|
if (built < total) {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}).catch(logError)
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
|
|
|
build(config)
|