Skip to content

Commit 925a4b9

Browse files
committed
Merge pull request #183 from jakemmarsh/fix-bundle-logger
Code cleanup, use bundleLogger
2 parents dbc2fb4 + 41acb43 commit 925a4b9

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

gulp/tasks/browserify.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict';
22

3-
import config from '../config';
43
import gulp from 'gulp';
54
import gulpif from 'gulp-if';
6-
import gutil from 'gulp-util';
75
import source from 'vinyl-source-stream';
86
import sourcemaps from 'gulp-sourcemaps';
97
import buffer from 'vinyl-buffer';
@@ -12,21 +10,21 @@ import watchify from 'watchify';
1210
import browserify from 'browserify';
1311
import babelify from 'babelify';
1412
import uglify from 'gulp-uglify';
15-
import handleErrors from '../util/handleErrors';
1613
import browserSync from 'browser-sync';
1714
import debowerify from 'debowerify';
1815
import ngAnnotate from 'browserify-ngannotate';
19-
20-
function createSourcemap() {
21-
return !global.isProd || config.browserify.prodSourcemap;
22-
}
16+
import handleErrors from '../util/handleErrors';
17+
import bundleLogger from '../util/bundleLogger';
18+
import config from '../config';
2319

2420
// Based on: http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/
2521
function buildScript(file) {
2622

23+
const shouldCreateSourcemap = !global.isProd || config.browserify.prodSourcemap;
24+
2725
let bundler = browserify({
2826
entries: [config.sourceDir + 'js/' + file],
29-
debug: createSourcemap(),
27+
debug: shouldCreateSourcemap,
3028
cache: {},
3129
packageCache: {},
3230
fullPaths: !global.isProd
@@ -35,10 +33,7 @@ function buildScript(file) {
3533
if ( !global.isProd ) {
3634
bundler = watchify(bundler);
3735

38-
bundler.on('update', function() {
39-
rebundle();
40-
gutil.log('Rebundle...');
41-
});
36+
bundler.on('update', rebundle);
4237
}
4338

4439
const transforms = [
@@ -54,17 +49,21 @@ function buildScript(file) {
5449
});
5550

5651
function rebundle() {
52+
bundleLogger.start();
53+
5754
const stream = bundler.bundle();
5855
const sourceMapLocation = global.isProd ? './' : '';
5956

60-
return stream.on('error', handleErrors)
57+
return stream
58+
.on('error', handleErrors)
59+
.on('end', bundleLogger.end)
6160
.pipe(source(file))
62-
.pipe(gulpif(createSourcemap(), buffer()))
63-
.pipe(gulpif(createSourcemap(), sourcemaps.init({ loadMaps: true })))
61+
.pipe(gulpif(shouldCreateSourcemap, buffer()))
62+
.pipe(gulpif(shouldCreateSourcemap, sourcemaps.init({ loadMaps: true })))
6463
.pipe(gulpif(global.isProd, streamify(uglify({
6564
compress: { drop_console: true } // eslint-disable-line camelcase
6665
}))))
67-
.pipe(gulpif(createSourcemap(), sourcemaps.write(sourceMapLocation)))
66+
.pipe(gulpif(shouldCreateSourcemap, sourcemaps.write(sourceMapLocation)))
6867
.pipe(gulp.dest(config.scripts.dest))
6968
.pipe(browserSync.stream());
7069
}

gulp/util/bundleLogger.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
'use strict';
22

3-
/* bundleLogger
4-
* ------------
5-
* Provides gulp style logs to the bundle method in browserify.js
6-
*/
7-
83
import gutil from 'gulp-util';
94
import prettyHrtime from 'pretty-hrtime';
105

11-
var startTime;
6+
let startTime;
127

138
export default {
149

1510
start() {
1611
startTime = process.hrtime();
17-
gutil.log('Running', gutil.colors.green('\'bundle\'') + '...');
12+
gutil.log(`${gutil.colors.green('Rebundling')}...`);
1813
},
1914

2015
end() {
21-
var taskTime = process.hrtime(startTime);
22-
var prettyTime = prettyHrtime(taskTime);
23-
gutil.log('Finished', gutil.colors.green('\'bundle\''), 'in', gutil.colors.magenta(prettyTime));
16+
const taskTime = process.hrtime(startTime);
17+
const prettyTime = prettyHrtime(taskTime);
18+
gutil.log(`Finished ${gutil.colors.green('rebundling')} in ${gutil.colors.magenta(prettyTime)}`);
2419
}
2520

26-
};
21+
};

gulp/util/handleErrors.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict';
22

3+
import gutil from 'gulp-util';
34
import notify from 'gulp-notify';
45

56
export default function(error) {
67

78
if( !global.isProd ) {
89

9-
var args = Array.prototype.slice.call(arguments);
10+
const args = Array.prototype.slice.call(arguments);
1011

1112
// Send error to notification center with gulp-notify
1213
notify.onError({
@@ -20,8 +21,8 @@ export default function(error) {
2021
} else {
2122
// Log the error and stop the process
2223
// to prevent broken code from building
23-
console.log(error);
24+
gutil.log(gutil.colors.red(error));
2425
process.exit(1);
2526
}
2627

27-
};
28+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angularjs-gulp-browserify-boilerplate",
3-
"version": "1.5.7",
3+
"version": "1.5.8",
44
"author": "Jake Marsh <[email protected]>",
55
"description": "Boilerplate using AngularJS, SASS, Gulp, and Browserify while also utilizing best practices.",
66
"repository": {

0 commit comments

Comments
 (0)