-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathwatchified_bundle.js
82 lines (67 loc) · 1.95 KB
/
watchified_bundle.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
var fs = require('fs');
var browserify = require('browserify');
var watchify = require('watchify');
var prettySize = require('prettysize');
var constants = require('./constants');
var common = require('./common');
/**
* Make a plotly.js browserify bundle function watched by watchify.
*
* N.B. This module is meant for dev builds; the output bundle is placed
* in plotly.js/build/
* Use `npm run build` for dist builds.
*
* @param {function} onFirstBundleCallback executed when first bundle is completed
*
*/
module.exports = function makeWatchifiedBundle(onFirstBundleCallback) {
var b = browserify(constants.pathToPlotlyIndex, {
debug: true,
standalone: 'Plotly',
transform: [],
cache: {},
packageCache: {},
plugin: [watchify]
});
var firstBundle = true;
if(firstBundle) {
console.log([
'***',
'Building the first bundle, this should take ~10 seconds',
'***\n'
].join(' '));
}
b.on('update', bundle);
formatBundleMsg(b, 'plotly.js');
function bundle() {
b.bundle(function(err) {
if(err) console.error(JSON.stringify(String(err)));
if(firstBundle) {
onFirstBundleCallback();
firstBundle = false;
}
})
.pipe(
fs.createWriteStream(constants.pathToPlotlyBuild)
);
}
return bundle;
};
function formatBundleMsg(b, bundleName) {
var msgParts = [
bundleName, ':', '',
'written', 'in', '', 'sec',
'[', '', ']'
];
b.on('bytes', function(bytes) {
msgParts[2] = prettySize(bytes, true);
});
b.on('time', function(time) {
msgParts[5] = (time / 1000).toFixed(2);
});
b.on('log', function() {
var formattedTime = common.formatTime(new Date());
msgParts[msgParts.length - 2] = formattedTime;
console.log(msgParts.join(' '));
});
}