1
1
'use strict' ;
2
2
3
- import config from '../config' ;
4
3
import gulp from 'gulp' ;
5
4
import gulpif from 'gulp-if' ;
6
- import gutil from 'gulp-util' ;
7
5
import source from 'vinyl-source-stream' ;
8
6
import sourcemaps from 'gulp-sourcemaps' ;
9
7
import buffer from 'vinyl-buffer' ;
@@ -12,21 +10,21 @@ import watchify from 'watchify';
12
10
import browserify from 'browserify' ;
13
11
import babelify from 'babelify' ;
14
12
import uglify from 'gulp-uglify' ;
15
- import handleErrors from '../util/handleErrors' ;
16
13
import browserSync from 'browser-sync' ;
17
14
import debowerify from 'debowerify' ;
18
15
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' ;
23
19
24
20
// Based on: http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/
25
21
function buildScript ( file ) {
26
22
23
+ const shouldCreateSourcemap = ! global . isProd || config . browserify . prodSourcemap ;
24
+
27
25
let bundler = browserify ( {
28
26
entries : [ config . sourceDir + 'js/' + file ] ,
29
- debug : createSourcemap ( ) ,
27
+ debug : shouldCreateSourcemap ,
30
28
cache : { } ,
31
29
packageCache : { } ,
32
30
fullPaths : ! global . isProd
@@ -35,10 +33,7 @@ function buildScript(file) {
35
33
if ( ! global . isProd ) {
36
34
bundler = watchify ( bundler ) ;
37
35
38
- bundler . on ( 'update' , function ( ) {
39
- rebundle ( ) ;
40
- gutil . log ( 'Rebundle...' ) ;
41
- } ) ;
36
+ bundler . on ( 'update' , rebundle ) ;
42
37
}
43
38
44
39
const transforms = [
@@ -54,17 +49,21 @@ function buildScript(file) {
54
49
} ) ;
55
50
56
51
function rebundle ( ) {
52
+ bundleLogger . start ( ) ;
53
+
57
54
const stream = bundler . bundle ( ) ;
58
55
const sourceMapLocation = global . isProd ? './' : '' ;
59
56
60
- return stream . on ( 'error' , handleErrors )
57
+ return stream
58
+ . on ( 'error' , handleErrors )
59
+ . on ( 'end' , bundleLogger . end )
61
60
. 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 } ) ) )
64
63
. pipe ( gulpif ( global . isProd , streamify ( uglify ( {
65
64
compress : { drop_console : true } // eslint-disable-line camelcase
66
65
} ) ) ) )
67
- . pipe ( gulpif ( createSourcemap ( ) , sourcemaps . write ( sourceMapLocation ) ) )
66
+ . pipe ( gulpif ( shouldCreateSourcemap , sourcemaps . write ( sourceMapLocation ) ) )
68
67
. pipe ( gulp . dest ( config . scripts . dest ) )
69
68
. pipe ( browserSync . stream ( ) ) ;
70
69
}
0 commit comments