tiptap/build/packages/build.js

81 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-08-22 16:41:26 +08:00
import fs from 'fs'
import path from 'path'
import zlib from 'zlib'
2019-05-01 00:32:12 +08:00
import Terser from 'terser'
2018-08-22 16:41:26 +08:00
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) {
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))
.then(response => {
2018-08-22 16:41:26 +08:00
if (isProd) {
2019-05-01 00:32:12 +08:00
const minified = Terser.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)
}
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)