vue/build/build.js

154 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-04-11 10:47:28 +08:00
var fs = require('fs')
var zlib = require('zlib')
var rollup = require('rollup')
var uglify = require('uglify-js')
var babel = require('rollup-plugin-babel')
var replace = require('rollup-plugin-replace')
2016-04-24 04:57:44 +08:00
var aliasPlugin = require('rollup-plugin-alias')
var baseAlias = require('./alias')
2016-04-11 10:47:28 +08:00
var version = process.env.VERSION || require('../package.json').version
var banner =
'/*!\n' +
' * Vue.js v' + version + '\n' +
2016-04-17 14:32:40 +08:00
' * (c) 2014-' + new Date().getFullYear() + ' Evan You\n' +
2016-04-11 10:47:28 +08:00
' * Released under the MIT License.\n' +
' */'
2016-04-20 08:13:47 +08:00
// Update main file
2016-04-11 10:47:28 +08:00
var main = fs
2016-04-24 04:12:19 +08:00
.readFileSync('src/core/index.js', 'utf-8')
2016-04-11 10:47:28 +08:00
.replace(/Vue\.version = '[\d\.]+'/, "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-04-20 08:13:47 +08:00
build([
// Runtime only, CommonJS build. Used by bundlers e.g. Webpack & Browserify
{
entry: 'src/entries/web-runtime.js',
2016-04-11 10:47:28 +08:00
format: 'cjs',
2016-04-20 08:13:47 +08:00
out: 'dist/vue.common.js'
},
// Minified runtime, only for filze size monitoring
{
entry: 'src/entries/web-runtime.js',
format: 'umd',
env: 'production',
out: 'dist/vue.common.min.js'
},
// Runtime+compiler standalone developement build.
{
entry: 'src/entries/web-runtime-with-compiler.js',
2016-04-20 07:45:15 +08:00
format: 'umd',
2016-04-20 08:13:47 +08:00
env: 'development',
out: 'dist/vue.js',
banner: true,
alias: {
entities: './entity-decoder'
2016-04-13 16:34:37 +08:00
}
2016-04-20 08:13:47 +08:00
},
// Runtime+compiler standalone production build.
{
2016-04-19 10:17:48 +08:00
entry: 'src/entries/web-runtime-with-compiler.js',
2016-04-20 08:13:47 +08:00
format: 'umd',
env: 'production',
out: 'dist/vue.min.js',
banner: true,
alias: {
entities: './entity-decoder'
}
},
2016-04-25 07:24:17 +08:00
// Web compiler CommonJS build, for npm distribution.
2016-04-20 08:13:47 +08:00
{
2016-04-24 04:12:19 +08:00
entry: 'src/entries/web-compiler.js',
2016-04-20 08:13:47 +08:00
format: 'cjs',
external: ['entities'],
2016-04-25 07:24:17 +08:00
out: 'dist/compiler.common.js'
2016-04-20 08:13:47 +08:00
}
])
function build (builds) {
var built = 0
var total = builds.length
next()
function next () {
buildEntry(builds[built]).then(function () {
built++
if (built < total) {
next()
}
}).catch(logError)
}
}
function buildEntry (opts) {
var plugins = [babel()]
if (opts.env) {
plugins.push(replace({
'process.env.NODE_ENV': JSON.stringify(opts.env)
}))
}
2016-04-24 04:57:44 +08:00
var alias = baseAlias
2016-04-20 08:13:47 +08:00
if (opts.alias) {
2016-04-24 04:57:44 +08:00
alias = Object.assign({}, baseAlias, opts.alias)
2016-04-20 08:13:47 +08:00
}
2016-04-24 04:57:44 +08:00
plugins.push(aliasPlugin(alias))
2016-04-11 10:47:28 +08:00
return rollup.rollup({
2016-04-20 08:13:47 +08:00
entry: opts.entry,
plugins: plugins,
external: opts.external
}).then(function (bundle) {
2016-04-11 10:47:28 +08:00
var code = bundle.generate({
2016-04-20 08:13:47 +08:00
format: opts.format,
moduleName: 'Vue',
banner: opts.banner ? banner : null
2016-04-11 10:47:28 +08:00
}).code
2016-04-20 08:13:47 +08:00
if (opts.env === 'production') {
var minified = (opts.banner ? banner + '\n' : '') + uglify.minify(code, {
fromString: true,
output: {
ascii_only: true
}
}).code
return write(opts.out, minified).then(zip(opts.out))
} else {
return write(opts.out, code)
}
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)
console.log(blue(dest) + ' ' + getSize(code))
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'
}