@@ -8,6 +8,8 @@ var gulp = require('gulp'),
88 gutil = require ( 'gulp-util' ) ,
99 flatten = require ( 'gulp-flatten' ) ,
1010 npmPath = require ( 'path' ) ,
11+ concat = require ( 'gulp-concat' ) ,
12+ bufferFrom = require ( 'buffer-from' )
1113 File = gutil . File ,
1214 exec = require ( 'child_process' ) . exec ,
1315 license = '' +
@@ -113,7 +115,7 @@ function treatMergedData(data) {
113115 mergedOrdered [ k ] = merged [ k ] ;
114116 } ) ;
115117
116- return new Buffer ( JSON . stringify ( mergedOrdered , null , 4 ) ) ;
118+ return bufferFrom ( JSON . stringify ( mergedOrdered , null , 4 ) ) ;
117119}
118120
119121/**
@@ -257,7 +259,7 @@ gulp.task('config', function(done) {
257259
258260 contents += '}\n' ;
259261
260- file . contents = new Buffer ( contents ) ;
262+ file . contents = bufferFrom ( contents ) ;
261263 this . emit ( 'data' , file ) ;
262264 } ) )
263265 . pipe ( rename ( 'configconstants.ts' ) )
@@ -296,3 +298,129 @@ gulp.task('copy-component-templates', function(done) {
296298 . on ( 'end' , done ) ;
297299} ) ;
298300
301+ /**
302+ * Finds the file and returns its content.
303+ *
304+ * @param {string } capture Import file path.
305+ * @param {string } baseDir Directory where the file was found.
306+ * @param {string } paths Alternative paths where to find the imports.
307+ * @param {Array } parsedFiles Yet parsed files to reduce size of the result.
308+ * @return {string } Partially combined scss.
309+ */
310+ function getReplace ( capture , baseDir , paths , parsedFiles ) {
311+ var parse = path . parse ( path . resolve ( baseDir , capture + '.scss' ) ) ;
312+ var file = parse . dir + '/' + parse . name ;
313+
314+
315+ if ( ! fs . existsSync ( file + '.scss' ) ) {
316+ // File not found, might be a partial file.
317+ file = parse . dir + '/_' + parse . name ;
318+ }
319+
320+ // If file still not found, try to find the file in the alternative paths.
321+ var x = 0 ;
322+ while ( ! fs . existsSync ( file + '.scss' ) && paths . length > x ) {
323+ parse = path . parse ( path . resolve ( paths [ x ] , capture + '.scss' ) ) ;
324+ file = parse . dir + '/' + parse . name ;
325+
326+ x ++ ;
327+ }
328+
329+ file = file + '.scss' ;
330+
331+ if ( ! fs . existsSync ( file ) ) {
332+ // File not found. Leave the import there.
333+ console . log ( 'File "' + capture + '" not found' ) ;
334+ return '@import "' + capture + '";' ;
335+ }
336+
337+ if ( parsedFiles . indexOf ( file ) >= 0 ) {
338+ console . log ( 'File "' + capture + '" already parsed' ) ;
339+ // File was already parsed, leave the import commented.
340+ return '// @import "' + capture + '";' ;
341+ }
342+
343+ parsedFiles . push ( file ) ;
344+ var text = fs . readFileSync ( file ) ;
345+
346+ // Recursive call.
347+ return scssCombine ( text , parse . dir , paths , parsedFiles ) ;
348+ }
349+
350+ /**
351+ * Combine scss files with its imports
352+ *
353+ * @param {string } content Scss string to read.
354+ * @param {string } baseDir Directory where the file was found.
355+ * @param {string } paths Alternative paths where to find the imports.
356+ * @param {Array } parsedFiles Yet parsed files to reduce size of the result.
357+ * @return {string } Scss string with the replaces done.
358+ */
359+ function scssCombine ( content , baseDir , paths , parsedFiles ) {
360+
361+ // Content is a Buffer, convert to string.
362+ if ( typeof content != "string" ) {
363+ content = content . toString ( ) ;
364+ }
365+
366+ // Search of single imports.
367+ var regex = / @ i m p o r t [ ] * [ ' " ] ( .* ) [ ' " ] [ ] * ; / g;
368+
369+ if ( regex . test ( content ) ) {
370+ return content . replace ( regex , function ( m , capture ) {
371+ if ( capture == "bmma" ) {
372+ return m ;
373+ }
374+
375+ return getReplace ( capture , baseDir , paths , parsedFiles ) ;
376+ } ) ;
377+ }
378+
379+ // Search of multiple imports.
380+ regex = / @ i m p o r t (?: [ \n ] + [ ' " ] ( .* ) [ ' " ] [ , ] ? [ \n ] * ) + ; / gm;
381+ if ( regex . test ( content ) ) {
382+ return content . replace ( regex , function ( m , capture ) {
383+ var text = "" ;
384+
385+ // Divide the import into multiple files.
386+ regex = / [ ' " ] ( [ ^ ' " ] * ) [ ' " ] / g;
387+ var captures = m . match ( regex ) ;
388+ for ( var x in captures ) {
389+ text += getReplace ( captures [ x ] . replace ( / [ ' " ] + / g, '' ) , baseDir , paths , parsedFiles ) + "\n" ;
390+ }
391+
392+ return text ;
393+ } ) ;
394+ }
395+
396+ return content ;
397+ }
398+
399+ gulp . task ( 'combine-scss' , function ( done ) {
400+ var paths = [
401+ 'node_modules/ionic-angular/themes/' ,
402+ 'node_modules/font-awesome/scss/' ,
403+ 'node_modules/ionicons/dist/scss/'
404+ ] ;
405+
406+ var parsedFiles = [ ] ;
407+
408+ gulp . src ( [
409+ './src/theme/variables.scss' ,
410+ './node_modules/ionic-angular/themes/ionic.globals.*.scss' ,
411+ './node_modules/ionic-angular/themes/ionic.components.scss' ,
412+ './src/**/*.scss' ] ) // define a source files
413+ . pipe ( through ( function ( file , encoding , callback ) {
414+ if ( file . isNull ( ) ) {
415+ return ;
416+ }
417+
418+ parsedFiles . push ( file ) ;
419+ file . contents = bufferFrom ( scssCombine ( file . contents , path . dirname ( file . path ) , paths , parsedFiles ) ) ;
420+
421+ this . emit ( 'data' , file ) ;
422+ } ) ) // combine them based on @import and save it to stream
423+ . pipe ( concat ( 'combined.scss' ) ) // concat the stream output in single file
424+ . pipe ( gulp . dest ( '.' ) ) // save file to destination.
425+ . on ( 'end' , done ) ;
426+ } ) ;
0 commit comments