@@ -24,16 +24,16 @@ var buildVersion = null;
24
24
gulp . task ( "default" , [ "browser-sync" , "build-default" , "watch" ] ) ;
25
25
gulp . task ( "build-default" , [ "build-js" , "build-sass" ] , browserSync . reload ) ;
26
26
27
- gulp . task ( "build" , function ( cb ) {
28
- runSequence ( [ "build-js-prod" , "build-sass" ] , function ( ) {
27
+ gulp . task ( "build-deploy " , function ( cb ) {
28
+ runSequence ( [ "build-js-prod" , "build-sass-dark " ] , function ( ) {
29
29
browserSync . reload ( ) ;
30
30
cb ( ) ;
31
31
} ) ;
32
32
} ) ;
33
33
34
34
gulp . task ( "deploy" , function ( cb ) {
35
35
buildVersion = getBuildVersion ( ) ;
36
- runSequence ( "build" , "clean-build" , "copy-build" , "inject-file-versions" , "inject-build-version" , "minify-html" , cb ) ;
36
+ runSequence ( "build-deploy " , "clean-build" , "copy-build" , "inject-file-versions" , "inject-build-version" , "minify-html" , cb ) ;
37
37
} ) ;
38
38
39
39
gulp . task ( "browser-sync" , function ( ) {
@@ -74,7 +74,7 @@ gulp.task("build-js", function() {
74
74
} ) ;
75
75
76
76
gulp . task ( "build-js-prod" , function ( ) {
77
- return rollup ( {
77
+ return rollup ( {
78
78
entry : "dev/src/app.js" ,
79
79
moduleContext : { "dev/lib/codemirror.js" :"window" } ,
80
80
plugins : [ babel ( {
@@ -142,7 +142,6 @@ gulp.task("inject-build-version", function(cb) {
142
142
fs . writeFile ( "build/deploy/regexr.js" , js , cb ) ;
143
143
} ) ;
144
144
145
-
146
145
gulp . task ( "minify-html" , function ( ) {
147
146
return gulp . src ( "build/index.html" )
148
147
. pipe ( htmlmin ( { collapseWhitespace : true , conservativeCollapse : true , removeComments : true } ) )
@@ -202,4 +201,114 @@ function getDateString() {
202
201
var now = new Date ( ) ;
203
202
var months = [ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" ] ;
204
203
return months [ now . getMonth ( ) ] + " " + now . getDate ( ) + ", " + now . getFullYear ( ) ;
204
+ }
205
+
206
+ // dark theme:
207
+ // TODO: this whole approach can be cleaned up, ideally when moving to gulp v4
208
+ var dark = "" , root = "" ;
209
+ gulp . task ( "build-sass-dark0" , function ( cb ) {
210
+ root = fs . readFileSync ( "dev/sass/regexr.scss" , "utf-8" ) ;
211
+ fs . writeFile ( "dev/sass/regexr.scss" , root . replace ( '@import "colors";' , '@import "colors_dark";' ) , cb ) ;
212
+ } ) ;
213
+ gulp . task ( "build-sass-dark1" , function ( cb ) {
214
+ dark = fs . readFileSync ( "deploy/regexr.css" , "utf-8" ) ;
215
+ fs . writeFile ( "dev/sass/regexr.scss" , root , cb ) ;
216
+ } ) ;
217
+ gulp . task ( "build-sass-dark2" , function ( cb ) {
218
+ var def = fs . readFileSync ( "deploy/regexr.css" , "utf-8" ) ;
219
+ var diff = ( new CSSDiff ( ) ) . diff ( def , dark , false ) ;
220
+ fs . writeFile ( "assets/themes/dark.css" , diff , cb ) ;
221
+ dark = root = "" ; // clean up.
222
+ } ) ;
223
+
224
+ gulp . task ( "build-sass-dark" , function ( cb ) {
225
+ runSequence ( "build-sass-dark0" , "build-sass" , "build-sass-dark1" , "build-sass" , "build-sass-dark2" , cb ) ;
226
+ } ) ;
227
+
228
+
229
+ // CSSDiff:
230
+ class CSSDiff {
231
+ constructor ( ) { }
232
+
233
+ diff ( base , targ , pretty = false ) {
234
+ let diff = this . compare ( this . parse ( base ) , this . parse ( targ ) ) ;
235
+ return this . _writeDiff ( diff , pretty ) ;
236
+ }
237
+
238
+ parse ( s , o = { } ) {
239
+ this . _parse ( s , / ( [ ^ \n \r \{ \} ] + ?) \s * \{ \s * / g, / \} / g, o ) ;
240
+ for ( let n in o ) {
241
+ if ( n === " keys" ) { continue ; }
242
+ o [ n ] = this . parseBlock ( o [ n ] ) ;
243
+ }
244
+ return o ;
245
+ }
246
+
247
+ parseBlock ( s , o = { } ) {
248
+ return this . _parse ( s , / ( [ ^ \s : ] + ) \s * : / g, / (?: ; | $ ) / g, o ) ;
249
+ }
250
+
251
+ compare ( o0 , o1 , o = { } ) {
252
+ let keys = o1 [ " keys" ] , l = keys . length , arr = [ ] ;
253
+ for ( let i = 0 ; i < l ; i ++ ) {
254
+ let n = keys [ i ] ;
255
+ if ( ! o0 [ n ] ) { o [ n ] = o1 [ n ] ; arr . push ( n ) ; continue ; }
256
+ let diff = this . _compareBlock ( o0 [ n ] , o1 [ n ] ) ;
257
+ if ( diff ) { o [ n ] = diff ; arr . push ( n ) ; }
258
+ }
259
+ o [ " keys" ] = arr ;
260
+ return o ;
261
+ }
262
+
263
+ _compareBlock ( o0 , o1 ) {
264
+ let keys = o1 [ " keys" ] , l = keys . length , arr = [ ] , o ;
265
+ for ( let i = 0 ; i < l ; i ++ ) {
266
+ let n = keys [ i ] ;
267
+ if ( o0 [ n ] === o1 [ n ] ) { continue ; }
268
+ if ( ! o ) { o = { } ; }
269
+ o [ n ] = o1 [ n ] ;
270
+ arr . push ( n ) ;
271
+ }
272
+ if ( o ) { o [ " keys" ] = arr ; }
273
+ return o ;
274
+ }
275
+
276
+ _parse ( s , keyRE , closeRE , o ) {
277
+ let i , match , arr = [ ] ;
278
+ while ( match = keyRE . exec ( s ) ) {
279
+ let key = match [ 1 ] ;
280
+ i = closeRE . lastIndex = keyRE . lastIndex ;
281
+ if ( ! ( match = closeRE . exec ( s ) ) ) { console . log ( "couldn't find close" , key ) ; break ; }
282
+ o [ key ] = s . substring ( i , closeRE . lastIndex - match [ 0 ] . length ) . trim ( ) ;
283
+ i = keyRE . lastIndex = closeRE . lastIndex ;
284
+ arr . push ( key ) ;
285
+ }
286
+ o [ " keys" ] = arr ;
287
+ return o ;
288
+ }
289
+
290
+ _writeDiff ( o , pretty = false ) {
291
+ let diff = "" , ln = "\n" , s = " " ;
292
+ if ( ! pretty ) { ln = s = "" ; }
293
+ let keys = o [ " keys" ] , l = keys . length ;
294
+ for ( let i = 0 ; i < l ; i ++ ) {
295
+ let n = keys [ i ] ;
296
+ if ( diff ) { diff += ln + ln ; }
297
+ diff += n + s + "{" + ln ;
298
+ diff += this . _writeBlock ( o [ n ] , pretty ) ;
299
+ diff += "}" ;
300
+ }
301
+ return diff ;
302
+ }
303
+
304
+ _writeBlock ( o , pretty = false ) {
305
+ let diff = "" , ln = "\n" , t = "\t" , s = " " ;
306
+ if ( ! pretty ) { ln = t = s = "" ; }
307
+ let keys = o [ " keys" ] , l = keys . length ;
308
+ for ( let i = 0 ; i < l ; i ++ ) {
309
+ let n = keys [ i ] ;
310
+ diff += t + n + ":" + s + o [ n ] + ";" + ln ;
311
+ }
312
+ return diff ;
313
+ }
205
314
}
0 commit comments