forked from Lucifier129/react-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
120 lines (111 loc) · 3.64 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
// copy from Vue
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' +
' * react-lite.js v' + version + '\n' +
' * (c) ' + new Date().getFullYear() + ' Jade Gu\n' +
' * Released under the MIT License.\n' +
' */'
runRollupTask('./src/index', 'react-lite')
// runRollupTask('./addons/ReactWithAddons', 'react-lite-with-addons')
function runRollupTask(entry, filename) {
// CommonJS build.
// this is used as the "main" field in package.json
// and used by bundlers like Webpack and Browserify.
rollup.rollup({
entry: entry,
plugins: [
babel({
loose: 'all'
})
]
})
.then(function(bundle) {
return write('dist/' + filename + '.common.js', bundle.generate({
format: 'cjs',
banner: banner
}).code)
})
// Standalone Dev Build
.then(function() {
return rollup.rollup({
entry: entry,
plugins: [
replace({
'process.env.NODE_ENV': "'development'"
}),
babel({
loose: 'all'
})
]
})
.then(function(bundle) {
return write('dist/' + filename + '.js', bundle.generate({
format: 'umd',
banner: banner,
moduleName: 'React'
}).code)
})
})
.then(function() {
// Standalone Production Build
return rollup.rollup({
entry: entry,
plugins: [
replace({
'process.env.NODE_ENV': "'production'"
}),
babel({
loose: 'all'
})
]
})
.then(function(bundle) {
var code = bundle.generate({
format: 'umd',
moduleName: 'React'
}).code
var minified = banner + '\n' + uglify.minify(code, {
fromString: true
}).code
return write('dist/' + filename + '.min.js', minified)
})
.then(zip)
})
.catch(logError)
function zip() {
return new Promise(function(resolve, reject) {
fs.readFile('dist/' + filename + '.min.js', function(err, buf) {
if (err) return reject(err)
zlib.gzip(buf, function(err, buf) {
if (err) return reject(err)
write('dist/' + filename + '.min.js.gz', buf).then(resolve)
})
})
})
}
}
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 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'
}