-
Notifications
You must be signed in to change notification settings - Fork 21
/
gulpfile.js
277 lines (242 loc) · 6.58 KB
/
gulpfile.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var fs = require('fs')
var async = require('async')
var gulp = require('gulp')
var gutil = require('gulp-util')
var concat = require('gulp-concat')
var cleanCSS = require('gulp-clean-css')
var rename = require('gulp-rename')
var webpack = require('webpack')
var uglify = require('uglify-js')
var rimraf = require('rimraf')
var argv = require('yargs').argv
var child_exec = require('child_process').exec
var ENTRY = './index.js'
var HEADER = './lib/header.js'
var DIST = __dirname + '/dist'
var VIS_JS = 'vis.js'
var VIS_MAP = 'vis.map'
var VIS_MIN_JS = 'vis.min.js'
var VIS_CSS = 'vis.css'
var VIS_MIN_CSS = 'vis.min.css'
var INDIVIDUAL_JS_BUNDLES = [
{ entry: './index-network.js', filename: 'vis-network.min.js' }
]
var INDIVIDUAL_CSS_BUNDLES = [
{
entry: ['./lib/shared/**/*.css', './lib/network/**/*.css'],
filename: 'vis-network.min.css'
}
]
/**
* Generate banner with today's date and correct version
*
* @returns {string} banner text
*/
function createBanner() {
var today = gutil.date(new Date(), 'yyyy-mm-dd') // today, formatted as yyyy-mm-dd
var version = require('./package.json').version
return String(fs.readFileSync(HEADER))
.replace('@@date', today)
.replace('@@version', version)
}
var bannerPlugin = new webpack.BannerPlugin({
banner: createBanner(),
entryOnly: true,
raw: true
})
var webpackModule = {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
cacheDirectory: true, // use cache to improve speed
babelrc: true // use the .baberc file
}
}
],
// exclude requires of moment.js language files
wrappedContextRegExp: /$^/
}
var webpackConfig = {
entry: ENTRY,
output: {
library: 'vis',
libraryTarget: 'umd',
path: DIST,
filename: VIS_JS,
sourcePrefix: ' '
},
module: webpackModule,
plugins: [bannerPlugin],
cache: true,
// generate details sourcempas of webpack modules
devtool: 'source-map'
//debug: true,
//bail: true
}
var uglifyConfig = {
outSourceMap: VIS_MAP,
output: {
comments: /@license/
}
}
// create a single instance of the compiler to allow caching
var compiler = webpack(webpackConfig)
/**
* Callback for handling errors for a compiler run
*
* @param {object} err
* @param {objects} stats
*/
function handleCompilerCallback(err, stats) {
if (err) {
gutil.log(err.toString())
}
if (stats && stats.compilation && stats.compilation.errors) {
// output soft errors
stats.compilation.errors.forEach(function(err) {
gutil.log(err.toString())
})
if (err || stats.compilation.errors.length > 0) {
gutil.beep() // TODO: this does not work on my system
}
}
}
// clean the dist/img directory
gulp.task('clean', function(cb) {
rimraf(DIST + '/img', cb)
})
gulp.task('bundle-js', function(cb) {
// update the banner contents (has a date in it which should stay up to date)
bannerPlugin.banner = createBanner()
compiler.run(function(err, stats) {
handleCompilerCallback(err, stats)
cb()
})
})
// create individual bundle for network
gulp.task('bundle-js-individual', function(cb) {
// update the banner contents (has a date in it which should stay up to date)
bannerPlugin.banner = createBanner()
async.each(
INDIVIDUAL_JS_BUNDLES,
function(item, callback) {
var webpackTimelineConfig = {
entry: item.entry,
output: {
library: 'vis',
libraryTarget: 'umd',
path: DIST,
filename: item.filename,
sourcePrefix: ' '
},
module: webpackModule,
plugins: [bannerPlugin, new webpack.optimize.UglifyJsPlugin()],
cache: true
}
var compiler = webpack(webpackTimelineConfig)
compiler.run(function(err, stats) {
handleCompilerCallback(err, stats)
callback()
})
},
cb
)
})
// bundle and minify css
gulp.task('bundle-css', function() {
return (
gulp
.src('./lib/**/*.css')
.pipe(concat(VIS_CSS))
.pipe(gulp.dest(DIST))
// TODO: nicer to put minifying css in a separate task?
.pipe(cleanCSS())
.pipe(rename(VIS_MIN_CSS))
.pipe(gulp.dest(DIST))
)
})
// bundle and minify individual css
gulp.task('bundle-css-individual', function(cb) {
async.each(
INDIVIDUAL_CSS_BUNDLES,
function(item, callback) {
return gulp
.src(item.entry)
.pipe(concat(item.filename))
.pipe(cleanCSS())
.pipe(rename(item.filename))
.pipe(gulp.dest(DIST))
.on('end', callback)
},
cb
)
})
gulp.task('copy', ['clean'], function() {
var network = gulp
.src('./lib/network/img/**/*')
.pipe(gulp.dest(DIST + '/img/network'))
return network
})
gulp.task('minify', ['bundle-js'], function(cb) {
var result = uglify.minify([DIST + '/' + VIS_JS], uglifyConfig)
// note: we add a newline '\n' to the end of the minified file to prevent
// any issues when concatenating the file downstream (the file ends
// with a comment).
fs.writeFileSync(DIST + '/' + VIS_MIN_JS, result.code + '\n')
fs.writeFileSync(
DIST + '/' + VIS_MAP,
result.map.replace(/"\.\/dist\//g, '"')
)
cb()
})
gulp.task('bundle', [
'bundle-js',
'bundle-js-individual',
'bundle-css',
'bundle-css-individual',
'copy'
])
// read command line arguments --bundle and --minify
var bundle = 'bundle' in argv
var minify = 'minify' in argv
var watchTasks = []
if (bundle || minify) {
// do bundling and/or minifying only when specified on the command line
watchTasks = []
if (bundle) watchTasks.push('bundle')
if (minify) watchTasks.push('minify')
} else {
// by default, do both bundling and minifying
watchTasks = ['bundle', 'minify']
}
// The watch task (to automatically rebuild when the source code changes)
gulp.task('watch', watchTasks, function() {
gulp.watch(['index.js', 'lib/**/*'], watchTasks)
})
// Generate the documentation files
gulp.task('docs', function(cb) {
var targetDir = 'gen/docs'
// Not sure if this is the best way to handle 'cb'; at least it works.
var hasError = false
var onError = function(error) {
if (error !== undefined && error !== null) {
console.error('Error while running task: ' + error)
hasError = true
cb()
}
}
rimraf(__dirname + '/' + targetDir, onError) // Clean up previous generation
if (!hasError) {
var params = '-c ./jsdoc.json -r -t docs -d ' + targetDir
child_exec(
'node ./node_modules/jsdoc/jsdoc.js ' + params + ' lib',
undefined,
cb
)
}
})
// The default task (called when you run `gulp`)
gulp.task('default', ['clean', 'bundle', 'minify'])