forked from vuejs/vuex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.main.js
81 lines (71 loc) · 1.81 KB
/
build.main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fs = require('fs')
const path = require('path')
const zlib = require('zlib')
const uglify = require('uglify-js')
const rollup = require('rollup')
const configs = require('./configs')
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist')
}
build(Object.keys(configs).map(key => configs[key]))
function build (builds) {
let built = 0
const total = builds.length
const next = () => {
buildEntry(builds[built]).then(() => {
built++
if (built < total) {
next()
}
}).catch(logError)
}
next()
}
function buildEntry (config) {
const isProd = /min\.js$/.test(config.dest)
return rollup.rollup(config).then(bundle => {
const code = bundle.generate(config).code
if (isProd) {
var minified = (config.banner ? config.banner + '\n' : '') + uglify.minify(code, {
fromString: true,
output: {
/* eslint-disable camelcase */
screw_ie8: true,
ascii_only: true
/* eslint-enable camelcase */
}
}).code
return write(config.dest, minified, true)
} else {
return write(config.dest, code)
}
})
}
function write (dest, code, zip) {
return new Promise((resolve, reject) => {
function report (extra) {
console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''))
resolve()
}
fs.writeFile(dest, code, err => {
if (err) return reject(err)
if (zip) {
zlib.gzip(code, (err, zipped) => {
if (err) return reject(err)
report(' (gzipped: ' + getSize(zipped) + ')')
})
} else {
report()
}
})
})
}
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'
}