Skip to content

Commit e535fe9

Browse files
committed
add append version util, use it to add version in bundles
1 parent d3b9f1f commit e535fe9

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

tasks/bundle.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var browserify = require('browserify');
44
var UglifyJS = require('uglify-js');
55

66
var compressAttributes = require('./util/compress_attributes');
7+
var appendVersion = require('./util/append_version');
78
var constants = require('./util/constants');
89

910
/*
@@ -24,7 +25,10 @@ try {
2425
fs.statSync(constants.pathToFontSVGBuild).isFile();
2526
}
2627
catch(e) {
27-
throw new Error('Please run `npm run preprocess` first');
28+
throw new Error([
29+
'build/ is missing a or more files',
30+
'Please run `npm run preprocess` first'
31+
].join('\n'));
2832
}
2933

3034

@@ -41,30 +45,43 @@ browserify(constants.pathToPlotlySrc, {
4145
if(!DEV) {
4246
fs.writeFile(
4347
constants.pathToPlotlyDistMin,
44-
UglifyJS.minify(buf.toString(), constants.uglifyOptions).code
48+
UglifyJS.minify(buf.toString(), constants.uglifyOptions).code,
49+
function() {
50+
appendVersion(
51+
constants.pathToPlotlyDistMin, {object: 'Plotly'}
52+
);
53+
}
4554
);
4655
}
4756
})
48-
.pipe(fs.createWriteStream(constants.pathToPlotlyDist));
57+
.pipe(fs.createWriteStream(constants.pathToPlotlyDist))
58+
.on('finish', function() {
59+
appendVersion(constants.pathToPlotlyDist, {object: 'Plotly', DEV: DEV});
60+
});
4961

5062

5163
// Browserify the geo assets
5264
browserify(constants.pathToPlotlyGeoAssetsSrc, {
5365
standalone: 'PlotlyGeoAssets'
5466
})
55-
.bundle(function(err, buf) {
67+
.bundle(function(err) {
5668
if(err) throw err;
5769
})
58-
.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist));
59-
70+
.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist))
71+
.on('finish', function() {
72+
appendVersion(constants.pathToPlotlyGeoAssetsDist, {object: 'PlotlyGeoAssets'});
73+
});
6074

6175

6276
// Browserify the plotly.js with meta
6377
browserify(constants.pathToPlotlySrc, {
6478
debug: DEV,
6579
standalone: 'Plotly'
6680
})
67-
.bundle(function(err, buf) {
81+
.bundle(function(err) {
6882
if(err) throw err;
6983
})
70-
.pipe(fs.createWriteStream(constants.pathToPlotlyDistWithMeta));
84+
.pipe(fs.createWriteStream(constants.pathToPlotlyDistWithMeta))
85+
.on('finish', function() {
86+
appendVersion(constants.pathToPlotlyDistWithMeta, {object: 'Plotly', DEV: DEV});
87+
});

tasks/util/append_version.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var fs = require('fs');
2+
3+
var pkg = require('../../package.json');
4+
5+
/**
6+
* Append package version to bundle
7+
*
8+
* @param {string} path path to bundle
9+
* @param {object} opts
10+
* - object {string} the standalone object in the bundle
11+
* - DEV {boolean} is this a DEV build?
12+
*/
13+
module.exports = function appendVersion(path, opts) {
14+
var txt = [
15+
opts.object,
16+
'.version=', '\'',
17+
pkg.version, opts.DEV ? '-dev' : '',
18+
'\'', ';'
19+
].join('');
20+
21+
fs.appendFile(path, txt, function(err) {
22+
if(err) throw err;
23+
}) ;
24+
};

tasks/util/make_watchified_bundle.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var browserify = require('browserify');
44
var watchify = require('watchify');
55

66
var compressAttributes = require('./compress_attributes');
7+
var appendVersion = require('./append_version');
78
var formatBundleMsg = require('./format_bundle_msg');
89
var constants = require('./constants');
910

@@ -47,7 +48,10 @@ module.exports = function makeWatchifiedBundle(onFirstBundleCallback) {
4748
})
4849
.pipe(
4950
fs.createWriteStream(constants.pathToPlotlyDist)
50-
);
51+
)
52+
.on('finish', function() {
53+
appendVersion(constants.pathToPlotlyDist, {object: 'Plotly', DEV: true});
54+
});
5155
}
5256

5357
return bundle;

0 commit comments

Comments
 (0)