Created
December 24, 2019 22:53
-
-
Save rruprai/d7a6008614f1bd1ddf7ddde6baa03120 to your computer and use it in GitHub Desktop.
Gulpjs separating css by media query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Required packages. | |
var gulp = require('gulp'), | |
sass = require('gulp-sass'), | |
del = require('del'), | |
minifyCss = require('gulp-clean-css'), | |
renameFile = require('gulp-rename'), | |
mediaQueriesSplitter = require('gulp-media-queries-splitter'); | |
// Set Source File directories. Should be modified based on setup. | |
var src = { | |
scss: '../scss/**/*.scss', | |
css: '../css/**/*.css', | |
deleteBuild: '../css/build/**/*' | |
} | |
// Set Destination File directories. Should be modified based on setup. | |
var dest = { | |
css: '../css/build', | |
excludeBuild: '!../css/build', | |
} | |
// Remove build folder so it doesn't split twice. | |
function removeBuild() { | |
return del([src.deleteBuild], { force: true }); | |
} | |
// Function for minifying scss. | |
function extractMediaQuery() { | |
return gulp.src([src.css, dest.excludeBuild]) | |
.pipe(mediaQueriesSplitter([ | |
{media: 'none', filename: 'mobile-small.css'}, | |
{media: {min: '480px', minUntil: '767px'}, filename: 'mobile.css'}, | |
{media: {min: '768px', minUntil: '1023px'}, filename: 'tablet-small.css'}, | |
{media: {min: '1024px', minUntil: '1439px'}, filename: 'tablet.css'}, | |
{media: {min: '1440px', minUntil: '1679px'}, filename: 'desktop-small.css'}, | |
{media: {min: '1680px'}, filename: 'desktop.css'} | |
])) | |
.pipe(minifyCss()) | |
.pipe(renameFile({ | |
suffix: '.min' | |
})) | |
.pipe(gulp.dest(dest.css)); | |
} | |
// Gulp task. | |
gulp.task('extract-media-query', gulp.series(removeBuild,extractMediaQuery), function(done) { | |
console.log('Finished extract-media-query task.'); | |
// set done signal. | |
done(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment