You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.3 KiB

9 years ago
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')
var aliasPlugin = require('rollup-plugin-alias')
var baseAlias = require('./alias')
9 years ago
var version = process.env.VERSION || require('../package.json').version
9 years ago
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist')
}
9 years ago
var banner =
'/*!\n' +
' * Vue.js v' + version + '\n' +
9 years ago
' * (c) 2014-' + new Date().getFullYear() + ' Evan You\n' +
9 years ago
' * Released under the MIT License.\n' +
' */'
// Update main file
9 years ago
var main = fs
.readFileSync('src/core/index.js', 'utf-8')
9 years ago
.replace(/Vue\.version = '[^']+'/, "Vue.version = '" + version + "'")
fs.writeFileSync('src/core/index.js', main)
9 years ago
var builds = [
// Runtime only (CommonJS). Used by bundlers e.g. Webpack & Browserify
{
entry: 'src/entries/web-runtime.js',
9 years ago
format: 'cjs',
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',
format: 'umd',
env: 'development',
out: 'dist/vue.js',
banner: true,
alias: {
entities: './entity-decoder'
9 years ago
}
},
// Runtime+compiler standalone production build.
{
9 years ago
entry: 'src/entries/web-runtime-with-compiler.js',
format: 'umd',
env: 'production',
out: 'dist/vue.min.js',
banner: true,
alias: {
entities: './entity-decoder'
}
},
// Web compiler (CommonJS).
{
entry: 'src/entries/web-compiler.js',
format: 'cjs',
external: ['entities', 'de-indent'],
out: 'packages/vue-template-compiler/build.js'
},
// Web server renderer (CommonJS).
{
entry: 'src/entries/web-server-renderer.js',
format: 'cjs',
external: ['stream', 'module', 'vm', 'entities', 'de-indent'],
out: 'packages/vue-server-renderer/build.js'
}
]
// filter builds via command line arg
if (process.argv[2]) {
var filters = process.argv[2].split(',')
builds = builds.filter(b => {
return filters.some(f => b.out.indexOf(f) > -1)
})
}
build(builds)
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),
'process.env.VUE_ENV': JSON.stringify('client')
}))
}
var alias = baseAlias
if (opts.alias) {
alias = Object.assign({}, baseAlias, opts.alias)
}
plugins.push(aliasPlugin(alias))
9 years ago
return rollup.rollup({
entry: opts.entry,
plugins: plugins,
external: opts.external
}).then(function (bundle) {
9 years ago
var code = bundle.generate({
format: opts.format,
moduleName: 'Vue',
banner: opts.banner ? banner : null
9 years ago
}).code
if (opts.env === 'production') {
var minified = (opts.banner ? banner + '\n' : '') + uglify.minify(code, {
fromString: true,
output: {
9 years ago
screw_ie8: true,
ascii_only: true
},
compress: {
pure_funcs: ['makeMap']
}
}).code
return write(opts.out, minified).then(zip(opts.out))
} else {
return write(opts.out, code)
}
9 years ago
})
}
9 years ago
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()
})
})
}
9 years ago
function zip (file) {
return function () {
return new Promise(function (resolve, reject) {
fs.readFile(file, function (err, buf) {
9 years ago
if (err) return reject(err)
9 years ago
zlib.gzip(buf, function (err, buf) {
if (err) return reject(err)
write(file + '.gz', buf).then(resolve)
})
9 years ago
})
})
9 years ago
}
9 years ago
}
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'
}