Last active
December 8, 2019 22:23
-
-
Save erutan/702bb63f85215f8f4e51ac018ad6120b to your computer and use it in GitHub Desktop.
Gulp 4 d.o/project/zurb_foundation/
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'; | |
/**************** Gulp.js 4 configuration ****************/ | |
const | |
// development or production | |
devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() === 'development'), | |
// directory locations | |
dir = { | |
foundation : __dirname + '/node_modules/foundation-sites/scss', | |
motionui : __dirname + '/node_modules/motion-ui/src', | |
src : 'src/', | |
build : '/' | |
}, | |
// modules | |
gulp = require('gulp'), | |
del = require('del'), | |
noop = require('gulp-noop'), | |
newer = require('gulp-newer'), | |
size = require('gulp-size'), | |
imagemin = require('gulp-imagemin'), | |
sass = require('gulp-sass'), | |
postcss = require('gulp-postcss'), | |
sourcemaps = devBuild ? require('gulp-sourcemaps') : null, | |
browsersync = devBuild ? require('browser-sync').create() : null; | |
console.log('Gulp', devBuild ? 'development' : 'production', 'build'); | |
/**************** clean task ****************/ | |
function clean() { | |
return del([ dir.build ]); | |
} | |
exports.clean = clean; | |
exports.wipe = clean; | |
/**************** images task ****************/ | |
const imgConfig = { | |
src : 'images-source/**/*', | |
build : 'images/', | |
minOpts: { | |
optimizationLevel: 5 | |
} | |
}; | |
function images() { | |
return gulp.src(imgConfig.src) | |
.pipe(newer(imgConfig.build)) | |
.pipe(imagemin(imgConfig.minOpts)) | |
.pipe(size({ showFiles:true })) | |
.pipe(gulp.dest(imgConfig.build)); | |
} | |
exports.images = images; | |
/**************** CSS task ****************/ | |
const cssConfig = { | |
src : 'scss/STARTER.scss', | |
watch : 'scss/**/*', | |
build : 'css/', | |
sassOpts: { | |
sourceMap : devBuild, | |
outputStyle : 'nested', | |
imagePath : '/images/', | |
precision : 3, | |
errLogToConsole : true, | |
}, | |
postCSS: [ | |
require('postcss-assets')({ | |
loadPaths: ['images/'], | |
basePath: dir.build | |
}), | |
require('autoprefixer')({ | |
browsers: ['> 1%'] | |
}) | |
] | |
}; | |
// remove unused selectors and minify production CSS | |
if (!devBuild) { | |
cssConfig.postCSS.push( | |
require('usedcss')({ html: ['index.html'] }), | |
require('cssnano') | |
); | |
} | |
function css() { | |
return gulp.src(cssConfig.src) | |
.pipe(sourcemaps ? sourcemaps.init() : noop()) | |
.pipe(sass(cssConfig.sassOpts).on('error', sass.logError)) | |
.pipe(postcss(cssConfig.postCSS)) | |
.pipe(sourcemaps ? sourcemaps.write() : noop()) | |
.pipe(size({ showFiles:true })) | |
.pipe(gulp.dest(cssConfig.build)) | |
.pipe(browsersync ? browsersync.reload({ stream: true }) : noop()); | |
} | |
exports.css = gulp.series(images, css); | |
/**************** server task (now private) ****************/ | |
const syncConfig = { | |
server: { | |
baseDir : './', | |
index : 'index.html' | |
}, | |
port : 8000, | |
open : false | |
}; | |
// browser-sync | |
function server(done) { | |
if (browsersync) browsersync.init(syncConfig); | |
done(); | |
} | |
/**************** watch task ****************/ | |
function watch(done) { | |
// image changes | |
gulp.watch(imgConfig.src, images); | |
// CSS changes | |
gulp.watch(cssConfig.watch, css); | |
done(); | |
} | |
/**************** default task ****************/ | |
exports.default = gulp.series(exports.css, watch, server); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment