vue/build/build.js

102 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-08-28 07:03:15 +08:00
const fs = require('fs')
const path = require('path')
const zlib = require('zlib')
const rollup = require('rollup')
const uglify = require('uglify-js')
2016-04-11 10:47:28 +08:00
2016-04-27 01:01:13 +08:00
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist')
}
2016-04-20 08:13:47 +08:00
// Update main file
2016-08-28 07:03:15 +08:00
const version = process.env.VERSION || require('../package.json').version
const main = fs
2016-04-24 04:12:19 +08:00
.readFileSync('src/core/index.js', 'utf-8')
2016-06-11 07:02:16 +08:00
.replace(/Vue\.version = '[^']+'/, "Vue.version = '" + version + "'")
2016-04-24 04:12:19 +08:00
fs.writeFileSync('src/core/index.js', main)
2016-04-11 10:47:28 +08:00
2016-08-28 07:03:15 +08:00
let builds = require('./config').getAllBuilds()
2016-04-26 04:37:20 +08:00
// filter builds via command line arg
if (process.argv[2]) {
2016-08-28 07:03:15 +08:00
const filters = process.argv[2].split(',')
2016-04-26 04:37:20 +08:00
builds = builds.filter(b => {
2016-08-28 07:03:15 +08:00
return filters.some(f => b.dest.indexOf(f) > -1)
2016-04-26 04:37:20 +08:00
})
}
build(builds)
2016-04-20 08:13:47 +08:00
function build (builds) {
2016-08-28 07:03:15 +08:00
let built = 0
const total = builds.length
const next = () => {
buildEntry(builds[built]).then(() => {
2016-04-20 08:13:47 +08:00
built++
if (built < total) {
next()
}
}).catch(logError)
}
2016-08-28 07:03:15 +08:00
next()
2016-04-20 08:13:47 +08:00
}
2016-08-28 07:03:15 +08:00
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, {
2016-04-20 08:13:47 +08:00
fromString: true,
output: {
2016-05-20 00:52:47 +08:00
screw_ie8: true,
2016-04-20 08:13:47 +08:00
ascii_only: true
2016-04-25 10:01:55 +08:00
},
compress: {
pure_funcs: ['makeMap']
2016-04-20 08:13:47 +08:00
}
}).code
2016-08-28 07:03:15 +08:00
return write(config.dest, minified).then(zip(config.dest))
2016-04-20 08:13:47 +08:00
} else {
2016-08-28 07:03:15 +08:00
return write(config.dest, code)
2016-04-20 08:13:47 +08:00
}
2016-04-11 10:47:28 +08:00
})
2016-04-20 08:13:47 +08:00
}
2016-04-11 10:47:28 +08:00
function write (dest, code) {
return new Promise(function (resolve, reject) {
fs.writeFile(dest, code, function (err) {
if (err) return reject(err)
2016-08-28 07:03:15 +08:00
console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code))
2016-04-11 10:47:28 +08:00
resolve()
})
})
}
2016-04-13 16:34:37 +08:00
function zip (file) {
return function () {
return new Promise(function (resolve, reject) {
fs.readFile(file, function (err, buf) {
2016-04-11 10:47:28 +08:00
if (err) return reject(err)
2016-04-13 16:34:37 +08:00
zlib.gzip(buf, function (err, buf) {
if (err) return reject(err)
write(file + '.gz', buf).then(resolve)
})
2016-04-11 10:47:28 +08:00
})
})
2016-04-13 16:34:37 +08:00
}
2016-04-11 10:47:28 +08: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'
}