@@ -12,6 +12,20 @@ var browserify = require('browserify'),
12
12
13
13
module . exports = writeBundles ;
14
14
15
+ var uglifyifyOptions = {
16
+ global : true ,
17
+ exts : [ '.js' , '.json' ] ,
18
+ output : {
19
+ // Keep important comments when minifying
20
+ comments : / ^ ! | ^ \* ! | @ p r e s e r v e | @ l i c e n s e | @ c c _ o n /
21
+ }
22
+ } ;
23
+
24
+ var istanbulOptions = {
25
+ ignore : [ '**/*.json' , '**/*.html' , '**/*.md' , '**/*.txt' ] ,
26
+ defaultIgnore : false
27
+ } ;
28
+
15
29
/**
16
30
* Writes Browserify bundles for the given entry file.
17
31
* At least one bundle is created (the outputFile), but additional ones may be
@@ -22,42 +36,23 @@ module.exports = writeBundles;
22
36
* @param {Options } options - Bundling options
23
37
*/
24
38
function writeBundles ( mainFiles , events , options ) {
25
- var bundles = [ ] ;
26
- var bundleFiles = [ ] ;
39
+ var bundlers = [ ] ;
27
40
28
- // Create the main bundle
29
- var mainBundle = newify ( mainFiles , events , options ) ;
30
- bundles . push ( mainBundle ) ;
31
- bundleFiles . push ( mainFiles ) ;
41
+ // If no output options are specified, then default to -- bundle
42
+ if ( ! options . bundle && ! options . minify && ! options . test ) {
43
+ options . bundle = true ;
44
+ }
32
45
33
- if ( options . minify ) {
34
- // The files for the minified bundle
35
- var minifiedFiles = new FileSet ( ) ;
36
- minifiedFiles . entryFile = mainFiles . entryFile ;
37
- minifiedFiles . outputFile = util . appendToFileName ( mainFiles . outputFile , '.min' ) ;
38
- if ( options . debug ) {
39
- minifiedFiles . mapFile = minifiedFiles . outputFile + '.map' ;
40
- }
41
- bundleFiles . push ( minifiedFiles ) ;
46
+ if ( options . bundle ) {
47
+ bundlers . push ( createMainBundler ( mainFiles , events , options ) ) ;
48
+ }
42
49
43
- // Create the minified bundle
44
- var minifiedBundle = newify ( minifiedFiles , events , options ) ;
45
- addMinifyTransform ( minifiedBundle ) ;
46
- bundles . push ( minifiedBundle ) ;
50
+ if ( options . minify ) {
51
+ bundlers . push ( createMinifiedBundler ( mainFiles , events , options ) ) ;
47
52
}
48
53
49
54
if ( options . test ) {
50
- // The files for the test bundle
51
- var testFiles = new FileSet ( ) ;
52
- testFiles . entryFile = mainFiles . entryFile ;
53
- testFiles . outputFile = util . appendToFileName ( mainFiles . outputFile , '.test' ) ;
54
- bundleFiles . push ( testFiles ) ;
55
-
56
- // Create the test bundle
57
- var testBundle = newify ( testFiles , events , options ) ;
58
- addCoverageTransform ( testBundle ) ;
59
- addMinifyTransform ( testBundle ) ;
60
- bundles . push ( testBundle ) ;
55
+ bundlers . push ( createTestBundler ( mainFiles , events , options ) ) ;
61
56
}
62
57
63
58
/**
@@ -67,12 +62,12 @@ function writeBundles(mainFiles, events, options) {
67
62
* @param {number } index - The bundle to build (from the {@link bundles} array)
68
63
**/
69
64
function writeBundle ( index ) {
70
- if ( bundles [ index ] ) {
65
+ if ( bundlers [ index ] ) {
71
66
// Write this bundle
72
- bundle ( bundles [ index ] , bundleFiles [ index ] ) ;
67
+ bundle ( bundlers [ index ] ) ;
73
68
74
69
// Write the next bundle when this one finishes
75
- bundles [ index ] . once ( 'end' , function ( ) {
70
+ bundlers [ index ] . once ( 'end' , function ( ) {
76
71
writeBundle ( index + 1 ) ;
77
72
} ) ;
78
73
}
@@ -81,6 +76,78 @@ function writeBundles(mainFiles, events, options) {
81
76
writeBundle ( 0 ) ;
82
77
}
83
78
79
+ /**
80
+ * Creates a Browserify instance that outputs the main (non-minified) bundle for the given entry file.
81
+ *
82
+ * @param {FileSet } mainFiles - The input & output files
83
+ * @param {EventEmitter } events - Browserify events will be propagated to this EventEmitter
84
+ * @param {Options } options - Bundling options
85
+ * @returns {Browserify }
86
+ */
87
+ function createMainBundler ( mainFiles , events , options ) {
88
+ return newify ( mainFiles , events , options ) ;
89
+ }
90
+
91
+ /**
92
+ * Creates a Browserify instance that outputs the minified bundle for the given entry file.
93
+ *
94
+ * @param {FileSet } mainFiles - The input & output files
95
+ * @param {EventEmitter } events - Browserify events will be propagated to this EventEmitter
96
+ * @param {Options } options - Bundling options
97
+ * @returns {Browserify }
98
+ */
99
+ function createMinifiedBundler ( mainFiles , events , options ) {
100
+ var minifiedFiles = new FileSet ( ) ;
101
+
102
+ if ( options . bundle || options . test ) {
103
+ // We're creating multiple output files, so append ".min" to the minified file
104
+ minifiedFiles . entryFile = mainFiles . entryFile ;
105
+ minifiedFiles . outputFile = util . appendToFileName ( mainFiles . outputFile , '.min' ) ;
106
+ if ( options . debug ) {
107
+ minifiedFiles . mapFile = minifiedFiles . outputFile + '.map' ;
108
+ }
109
+ }
110
+ else {
111
+ // We're ONLY creating a minified file, so this is the main output file
112
+ minifiedFiles = mainFiles ;
113
+ }
114
+
115
+ var bundler = newify ( minifiedFiles , events , options ) ;
116
+ bundler . transform ( uglifyify , uglifyifyOptions ) ;
117
+ return bundler ;
118
+ }
119
+
120
+ /**
121
+ * Creates a Browserify instance that outputs the test bundle (with code-coverage instrumentation)
122
+ * for the given entry file.
123
+ *
124
+ * @param {FileSet } mainFiles - The input & output files
125
+ * @param {EventEmitter } events - Browserify events will be propagated to this EventEmitter
126
+ * @param {Options } options - Bundling options
127
+ * @returns {Browserify }
128
+ */
129
+ function createTestBundler ( mainFiles , events , options ) {
130
+ var testFiles = new FileSet ( ) ;
131
+
132
+ if ( options . bundle || options . minify ) {
133
+ // We're creating multiple output files, so append ".test" to the test file
134
+ testFiles . entryFile = mainFiles . entryFile ;
135
+ testFiles . outputFile = util . appendToFileName ( mainFiles . outputFile , '.test' ) ;
136
+ }
137
+ else {
138
+ // We're ONLY creating a test file, so this is the main output file
139
+ testFiles = mainFiles ;
140
+
141
+ // Don't produce source maps for test files (Istanbul doesn't support source maps anyway)
142
+ testFiles . mapFile = '' ;
143
+ }
144
+
145
+ var bundler = newify ( testFiles , events , options ) ;
146
+ bundler . transform ( uglifyify , uglifyifyOptions ) ;
147
+ bundler . transform ( istanbul ( istanbulOptions ) ) ;
148
+ return bundler ;
149
+ }
150
+
84
151
/**
85
152
* Creates a new Browserify or Watchify instance
86
153
*
@@ -122,54 +189,28 @@ function newify(fileSet, events, options) {
122
189
} ) ;
123
190
}
124
191
125
- return b ;
126
- }
192
+ // Remember the input/output files for this bundler
193
+ b . files = fileSet ;
127
194
128
- /**
129
- * Uses the Uglifyify transform to minify the given Browserify bundle
130
- *
131
- * @param {Browserify } b - The Browserify object to transform
132
- */
133
- function addMinifyTransform ( b ) {
134
- b . transform ( uglifyify , {
135
- global : true ,
136
- exts : [ '.js' , '.json' ] ,
137
- output : {
138
- // Keep important comments when minifying
139
- comments : / ^ ! | ^ \* ! | @ p r e s e r v e | @ l i c e n s e | @ c c _ o n /
140
- }
141
- } ) ;
142
- }
143
-
144
- /**
145
- * Uses the Istanbul transform to add code-coverage instrumentation the given Browserify bundle
146
- *
147
- * @param {Browserify } b - The Browserify object to transform
148
- */
149
- function addCoverageTransform ( b ) {
150
- b . transform ( istanbul ( {
151
- ignore : [ '**/*.json' , '**/*.html' , '**/*.md' , '**/*.txt' ] ,
152
- defaultIgnore : false
153
- } ) ) ;
195
+ return b ;
154
196
}
155
197
156
198
/**
157
199
* Writes the output file (and possibly its .map file) for the given Browserify object
158
200
*
159
201
* @param {Browserify } b - The Browserify object to bundle
160
- * @param {FileSet } fileSet - The input & output files for this bundle
161
202
*/
162
- function bundle ( b , fileSet ) {
203
+ function bundle ( b ) {
163
204
var stream = b . bundle ( ) ;
164
205
stream . on ( 'end' , b . emit . bind ( b , 'end' ) ) ;
165
206
stream . on ( 'error' , b . emit . bind ( b , 'error' ) ) ;
166
207
167
- if ( fileSet . mapFile ) {
168
- util . ensureFileExists ( fileSet . mapFile ) ;
169
- var dirname = path . dirname ( fileSet . mapFile ) ;
170
- stream = stream . pipe ( exorcist ( fileSet . mapFile , null , null , dirname ) ) ;
208
+ if ( b . files . mapFile ) {
209
+ util . ensureFileExists ( b . files . mapFile ) ;
210
+ var dirname = path . dirname ( b . files . mapFile ) ;
211
+ stream = stream . pipe ( exorcist ( b . files . mapFile , null , null , dirname ) ) ;
171
212
}
172
213
173
- util . ensureFileExists ( fileSet . outputFile ) ;
174
- stream . pipe ( fs . createWriteStream ( fileSet . outputFile ) ) ;
214
+ util . ensureFileExists ( b . files . outputFile ) ;
215
+ stream . pipe ( fs . createWriteStream ( b . files . outputFile ) ) ;
175
216
}
0 commit comments