-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
133 lines (125 loc) · 2.96 KB
/
build.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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 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
.readFileSync('src/index.js', 'utf-8')
.replace(/Vue\.version = '[\d\.]+'/, "Vue.version = '" + version + "'")
fs.writeFileSync('src/index.js', main)
// CommonJS build.
// this is used as the "main" field in package.json
// and used by bundlers like Webpack and Browserify.
rollup.rollup({
entry: 'src/index.js',
plugins: [
babel({
loose: 'all'
})
]
})
.then(function (bundle) {
return write('dist/vue.common.js', bundle.generate({
format: 'cjs',
banner: banner
}).code)
})
// Standalone Dev Build
.then(function () {
return rollup.rollup({
entry: 'src/index.js',
plugins: [
replace({
'process.env.NODE_ENV': "'development'"
}),
babel({
loose: 'all'
})
]
})
.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({
entry: 'src/index.js',
plugins: [
replace({
'process.env.NODE_ENV': "'production'"
}),
babel({
loose: 'all'
})
]
})
.then(function (bundle) {
var code = bundle.generate({
format: 'umd',
moduleName: 'Vue',
banner: banner
}).code
var res = uglify.minify(code, {
fromString: true,
outSourceMap: 'vue.min.js.map',
output: {
preamble: banner,
ascii_only: true
}
})
// fix uglifyjs sourcemap
var map = JSON.parse(res.map)
map.sources = ['vue.js']
map.sourcesContent = [code]
map.file = 'vue.min.js'
return [
write('dist/vue.min.js', res.code),
write('dist/vue.min.js.map', JSON.stringify(map))
]
})
.then(zip)
})
.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()
})
})
}
function zip () {
return new Promise(function (resolve, reject) {
fs.readFile('dist/vue.min.js', function (err, buf) {
if (err) return reject(err)
zlib.gzip(buf, function (err, buf) {
if (err) return reject(err)
write('dist/vue.min.js.gz', buf).then(resolve)
})
})
})
}
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'
}