vue/build/build.js

150 lines
3.5 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-13 06:03:45 +08:00
var alias = require('rollup-plugin-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' +
' * (c) ' + new Date().getFullYear() + ' Evan You\n' +
' * Released under the MIT License.\n' +
' */'
// update main file
var main = fs
2016-04-13 07:42:15 +08:00
.readFileSync('src/runtime/index.js', 'utf-8')
2016-04-11 10:47:28 +08:00
.replace(/Vue\.version = '[\d\.]+'/, "Vue.version = '" + version + "'")
2016-04-13 07:42:15 +08:00
fs.writeFileSync('src/runtime/index.js', main)
2016-04-11 10:47:28 +08:00
// CommonJS build.
// this is used as the "main" field in package.json
// and used by bundlers like Webpack and Browserify.
2016-04-13 07:42:15 +08:00
// runtime only, because it's meant to be
2016-04-12 10:53:13 +08:00
// used with vue-loader which pre-compiles the template.
2016-04-11 10:47:28 +08:00
rollup.rollup({
2016-04-13 07:42:15 +08:00
entry: 'src/runtime/index.js',
2016-04-13 06:03:45 +08:00
plugins: [babel()]
2016-04-11 10:47:28 +08:00
})
.then(function (bundle) {
2016-04-13 16:34:37 +08:00
var code = bundle.generate({
2016-04-11 10:47:28 +08:00
format: 'cjs',
banner: banner
2016-04-13 16:34:37 +08:00
}).code
var minified = banner + '\n' + uglify.minify(code, {
fromString: true,
output: {
ascii_only: true
}
}).code
return Promise.all([
write('dist/vue.common.js', code),
write('dist/vue.common.min.js', minified)
])
2016-04-11 10:47:28 +08:00
})
2016-04-13 16:34:37 +08:00
.then(zip('dist/vue.common.min.js'))
2016-04-13 18:45:34 +08:00
// Compiler CommonJS build.
// Used in Node loaders/transforms.
.then(function () {
return rollup.rollup({
entry: 'src/compiler/index.js',
plugins: [babel()]
})
.then(function (bundle) {
write('dist/compiler/compiler.js', bundle.generate({
format: 'cjs'
}).code)
})
})
2016-04-11 10:47:28 +08:00
// Standalone Dev Build
.then(function () {
return rollup.rollup({
2016-04-13 07:42:15 +08:00
entry: 'src/runtime-with-compiler.js',
2016-04-11 10:47:28 +08:00
plugins: [
2016-04-13 06:03:45 +08:00
alias({
entities: './entity-decoder'
}),
2016-04-11 10:47:28 +08:00
replace({
'process.env.NODE_ENV': "'development'"
2016-04-13 06:03:45 +08:00
}),
babel()
]
2016-04-11 10:47:28 +08:00
})
.then(function (bundle) {
return write('dist/vue.js', bundle.generate({
format: 'umd',
banner: banner,
moduleName: 'Vue'
}).code)
})
})
.then(function () {
// Standalone Production Build
return rollup.rollup({
2016-04-13 07:42:15 +08:00
entry: 'src/runtime-with-compiler.js',
2016-04-11 10:47:28 +08:00
plugins: [
2016-04-13 06:03:45 +08:00
alias({
entities: './entity-decoder'
}),
2016-04-11 10:47:28 +08:00
replace({
'process.env.NODE_ENV': "'production'"
2016-04-13 06:03:45 +08:00
}),
babel()
]
2016-04-11 10:47:28 +08:00
})
.then(function (bundle) {
var code = bundle.generate({
format: 'umd',
moduleName: 'Vue'
}).code
var minified = banner + '\n' + uglify.minify(code, {
fromString: true,
output: {
ascii_only: true
}
}).code
return write('dist/vue.min.js', minified)
})
2016-04-13 16:34:37 +08:00
.then(zip('dist/vue.min.js'))
2016-04-11 10:47:28 +08:00
})
.catch(logError)
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'
}