javascript - Map gulp.task over multidimensional array -
information project
i'm developing more complex website requires several stylesheets frontend , backend loaded if specific conditions match.
i'm developing in scss , compiling files using gulp.
in gulpfile declare array frontend stylesheets , array containing backend ones. here how looks like. it's not finished though.
const cssfront = [ 'style.scss' ]; const cssback = [ 'admin.scss', 'settings.admin.scss' ]; let cssfiles = [ cssfront, cssback ];
what code should do
gulp should map on files in cssfiles
, compile css code of files.
what have done
however map
-functions work 1 dimensional, tried reduce 2 dimensional array 1 dimensional 1 , map on it.
const sass = require( 'gulp-sass' ); const autoprefixer = require( 'gulp-autoprefixer' ); const stylesrc = './src/scss/'; const cssfront = [ 'style.scss' ]; const cssback = [ 'admin.scss', 'settings.admin.scss' ]; let cssfiles = [ cssfront, cssback ]; const styleurl = './assets/css/'; const mapurl = './'; gulp.task( 'scss', function() { cssfiles = cssfiles.reduce(function(a, b){ return a.concat(b); }); cssfiles.map( function( file ) { return gulp.src( stylesrc + file ) .pipe( sourcemaps.init() ) .pipe( sass({ errlogtoconsole: true, outputstyle: 'compressed' }) ) .on( 'error', console.error.bind( console ) ) .pipe( autoprefixer({ browsers: [ 'last 2 versions', '> 5%', 'firefox esr' ] }) ) .pipe( rename( { suffix: '.min' } ) ) .pipe( sourcemaps.write( mapurl ) ) .pipe( gulp.dest( styleurl ) ) .pipe( browsersync.stream() ); }); });
the problem
the reduce
-function works well. did console.log
, output was:
['style.scss', 'admin.scss', 'settings.admin.scss']
if running task directly ($ gulp scss
) code works perfectly, files compiled , saved under assets/css/
, if run $ gulp watch
, receive error message saying
cssfiles.map not function
due error message assume there better way of writing gulp.task
. i'd pleased if me out this.
problem in watch task.
when first run task - reduce
cssfiles
(several arrays [ cssfront, cssback ]
)
this result saved in variable cssfiles
new value ['style.scss', 'admin.scss', 'settings.admin.scss']
when watch task run next time reduce resulted array ['style.scss', 'admin.scss', 'settings.admin.scss']
, new result string "style.scssadmin.scsssettings.admin.scss"
, save in cssfiles
variable
after error cssfiles.map not function
because "style.scssadmin.scsssettings.admin.scss".map() -> not function
you need reduce
outside function task
cssfiles = cssfiles.reduce(function(a, b){ return a.concat(b); }); gulp.task( 'scss', function() { cssfiles.map( function( file ) { // ... }); });
Comments
Post a Comment