Skip to content

Commit e16d2f7

Browse files
committed
tasks: factor out _bundle wrapper in own tasks/util/ module
- so that both bundle.js and cibundle.js can use it.
1 parent 2acb387 commit e16d2f7

File tree

2 files changed

+73
-51
lines changed

2 files changed

+73
-51
lines changed

tasks/bundle.js

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
var fs = require('fs');
2-
var path = require('path');
3-
4-
var browserify = require('browserify');
5-
var UglifyJS = require('uglify-js');
6-
71
var constants = require('./util/constants');
82
var common = require('./util/common');
9-
var compressAttributes = require('./util/compress_attributes');
10-
var patchMinified = require('./util/patch_minified');
11-
var doesFileExist = common.doesFileExist;
3+
var _bundle = require('./util/browserify_wrapper');
124

135
/*
146
* This script takes one argument
@@ -26,6 +18,7 @@ var DEV = (arg === 'dev') || (arg === '--dev');
2618

2719

2820
// Check if style and font build files are there
21+
var doesFileExist = common.doesFileExist;
2922
if(!doesFileExist(constants.pathToCSSBuild) || !doesFileExist(constants.pathToFontSVG)) {
3023
throw new Error([
3124
'build/ is missing one or more files',
@@ -59,45 +52,3 @@ constants.partialBundlePaths.forEach(function(pathObj) {
5952
pathToMinBundle: pathObj.distMin
6053
});
6154
});
62-
63-
function _bundle(pathToIndex, pathToBundle, opts) {
64-
opts = opts || {};
65-
66-
// do we output a minified file?
67-
var pathToMinBundle = opts.pathToMinBundle,
68-
outputMinified = !!pathToMinBundle && !opts.debug;
69-
70-
var browserifyOpts = {};
71-
browserifyOpts.standalone = opts.standalone;
72-
browserifyOpts.debug = opts.debug;
73-
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
74-
75-
var b = browserify(pathToIndex, browserifyOpts),
76-
bundleWriteStream = fs.createWriteStream(pathToBundle);
77-
78-
bundleWriteStream.on('finish', function() {
79-
logger(pathToBundle);
80-
});
81-
82-
b.bundle(function(err, buf) {
83-
if(err) throw err;
84-
85-
if(outputMinified) {
86-
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
87-
minifiedCode = patchMinified(minifiedCode);
88-
89-
fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
90-
if(err) throw err;
91-
92-
logger(pathToMinBundle);
93-
});
94-
}
95-
})
96-
.pipe(bundleWriteStream);
97-
}
98-
99-
function logger(pathToOutput) {
100-
var log = 'ok ' + path.basename(pathToOutput);
101-
102-
console.log(log);
103-
}

tasks/util/browserify_wrapper.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
4+
var browserify = require('browserify');
5+
var UglifyJS = require('uglify-js');
6+
7+
var constants = require('./constants');
8+
var compressAttributes = require('./compress_attributes');
9+
var patchMinified = require('./patch_minified');
10+
11+
/** Convenience browserify wrapper
12+
*
13+
* @param {string} pathToIndex path to index file to bundle
14+
* @param {string} pathToBunlde path to destination bundle
15+
*
16+
* @param {object} opts
17+
*
18+
* Browserify options:
19+
* - standalone {string}
20+
* - debug {boolean} [optional]
21+
*
22+
* Additional option:
23+
* - pathToMinBundle {string} path to destination minified bundle
24+
*
25+
* Outputs one bundle (un-minified) file if opts.pathToMinBundle is omitted
26+
* or opts.debug is true. Otherwise outputs two file: one un-minified bundle and
27+
* one minified bundle.
28+
*
29+
* Logs basename of bundle when completed.
30+
*/
31+
module.exports = function _bundle(pathToIndex, pathToBundle, opts) {
32+
opts = opts || {};
33+
34+
// do we output a minified file?
35+
var pathToMinBundle = opts.pathToMinBundle,
36+
outputMinified = !!pathToMinBundle && !opts.debug;
37+
38+
var browserifyOpts = {};
39+
browserifyOpts.standalone = opts.standalone;
40+
browserifyOpts.debug = opts.debug;
41+
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
42+
43+
var b = browserify(pathToIndex, browserifyOpts),
44+
bundleWriteStream = fs.createWriteStream(pathToBundle);
45+
46+
bundleWriteStream.on('finish', function() {
47+
logger(pathToBundle);
48+
});
49+
50+
b.bundle(function(err, buf) {
51+
if(err) throw err;
52+
53+
if(outputMinified) {
54+
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
55+
minifiedCode = patchMinified(minifiedCode);
56+
57+
fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
58+
if(err) throw err;
59+
60+
logger(pathToMinBundle);
61+
});
62+
}
63+
})
64+
.pipe(bundleWriteStream);
65+
};
66+
67+
function logger(pathToOutput) {
68+
var log = 'ok ' + path.basename(pathToOutput);
69+
70+
console.log(log);
71+
}

0 commit comments

Comments
 (0)