|
1 | 1 | var fs = require('fs');
|
| 2 | +var path = require('path'); |
2 | 3 |
|
3 | 4 | var browserify = require('browserify');
|
4 | 5 | var UglifyJS = require('uglify-js');
|
@@ -28,48 +29,76 @@ try {
|
28 | 29 | }
|
29 | 30 | catch(e) {
|
30 | 31 | throw new Error([
|
31 |
| - 'build/ is missing a or more files', |
| 32 | + 'build/ is missing one or more files', |
32 | 33 | 'Please run `npm run preprocess` first'
|
33 | 34 | ].join('\n'));
|
34 | 35 | }
|
35 | 36 |
|
36 | 37 |
|
37 | 38 | // Browserify plotly.js
|
38 |
| -browserify(constants.pathToPlotlyIndex, { |
39 |
| - debug: DEV, |
| 39 | +_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, { |
40 | 40 | standalone: 'Plotly',
|
41 |
| - transform: [compressAttributes] |
42 |
| -}) |
43 |
| -.bundle(function(err, buf) { |
44 |
| - if(err) throw err; |
45 |
| - |
46 |
| - // generate plotly.min.js |
47 |
| - if(!DEV) { |
48 |
| - fs.writeFile( |
49 |
| - constants.pathToPlotlyDistMin, |
50 |
| - UglifyJS.minify(buf.toString(), constants.uglifyOptions).code |
51 |
| - ); |
52 |
| - } |
53 |
| -}) |
54 |
| -.pipe(fs.createWriteStream(constants.pathToPlotlyDist)); |
55 |
| - |
| 41 | + debug: DEV, |
| 42 | + pathToMinBundle: constants.pathToPlotlyDistMin |
| 43 | +}); |
56 | 44 |
|
57 | 45 | // Browserify the geo assets
|
58 |
| -browserify(constants.pathToPlotlyGeoAssetsSrc, { |
| 46 | +_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, { |
59 | 47 | standalone: 'PlotlyGeoAssets'
|
60 |
| -}) |
61 |
| -.bundle(function(err) { |
62 |
| - if(err) throw err; |
63 |
| -}) |
64 |
| -.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist)); |
65 |
| - |
| 48 | +}); |
66 | 49 |
|
67 | 50 | // Browserify the plotly.js with meta
|
68 |
| -browserify(constants.pathToPlotlyIndex, { |
69 |
| - debug: DEV, |
70 |
| - standalone: 'Plotly' |
71 |
| -}) |
72 |
| -.bundle(function(err) { |
73 |
| - if(err) throw err; |
74 |
| -}) |
75 |
| -.pipe(fs.createWriteStream(constants.pathToPlotlyDistWithMeta)); |
| 51 | +_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, { |
| 52 | + standalone: 'Plotly', |
| 53 | + debug: DEV |
| 54 | +}); |
| 55 | + |
| 56 | +// Browserify the plotly.js partial bundles |
| 57 | +constants.partialBundlePaths.forEach(function(pathObj) { |
| 58 | + _bundle(pathObj.index, pathObj.dist, { |
| 59 | + standalone: 'Plotly', |
| 60 | + debug: DEV, |
| 61 | + pathToMinBundle: pathObj.distMin |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +function _bundle(pathToIndex, pathToBundle, opts) { |
| 66 | + opts = opts || {}; |
| 67 | + |
| 68 | + // do we output a minified file? |
| 69 | + var pathToMinBundle = opts.pathToMinBundle, |
| 70 | + outputMinified = !!pathToMinBundle && !opts.debug; |
| 71 | + |
| 72 | + var browserifyOpts = {}; |
| 73 | + browserifyOpts.standalone = opts.standalone; |
| 74 | + browserifyOpts.debug = opts.debug; |
| 75 | + browserifyOpts.transform = outputMinified ? [compressAttributes] : []; |
| 76 | + |
| 77 | + var b = browserify(pathToIndex, browserifyOpts), |
| 78 | + bundleWriteStream = fs.createWriteStream(pathToBundle); |
| 79 | + |
| 80 | + bundleWriteStream.on('finish', function() { |
| 81 | + logger(pathToBundle); |
| 82 | + }); |
| 83 | + |
| 84 | + b.bundle(function(err, buf) { |
| 85 | + if(err) throw err; |
| 86 | + |
| 87 | + if(outputMinified) { |
| 88 | + var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code; |
| 89 | + |
| 90 | + fs.writeFile(pathToMinBundle, minifiedCode, function(err) { |
| 91 | + if(err) throw err; |
| 92 | + |
| 93 | + logger(pathToMinBundle); |
| 94 | + }); |
| 95 | + } |
| 96 | + }) |
| 97 | + .pipe(bundleWriteStream); |
| 98 | +} |
| 99 | + |
| 100 | +function logger(pathToOutput) { |
| 101 | + var log = 'ok ' + path.basename(pathToOutput); |
| 102 | + |
| 103 | + console.log(log); |
| 104 | +} |
0 commit comments