Last active
October 6, 2021 15:36
-
-
Save dyarfi/993bcb66052076424d40b0983dede147 to your computer and use it in GitHub Desktop.
Gulp 3 gulpfile.js config
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
/*global require*/ | |
"use strict"; | |
var gulp = require('gulp'), | |
gulpCopy = require('gulp-copy'), | |
path = require('path'), | |
data = require('gulp-data'), | |
// pug = require('gulp-pug'), | |
twig = require('gulp-twig'), // Decided to use twig, because already familiar with it | |
prefix = require('gulp-autoprefixer'), | |
sass = require('gulp-sass'), | |
plumber = require('gulp-plumber'), | |
concat = require('gulp-concat'), | |
sourcemaps = require('gulp-sourcemaps'), | |
browserSync = require('browser-sync'), | |
fs = require('fs'), | |
del = require('del'); | |
/* | |
* Directories here | |
*/ | |
var paths = { | |
src: './src/', | |
build: './build/', | |
sass: './scss/', | |
css: './build/assets/css/', | |
data: './client/data/', | |
assets: './src/assets/' | |
}; | |
/** | |
* Clean assets directory | |
*/ | |
gulp.task('clean', function () { | |
return del(['build/assets/**/*', '!build/assets/js/*.*']); | |
}); | |
/** | |
* Copy assets directory | |
*/ | |
gulp.task('copy-assets', function () { | |
return gulp | |
.src(['./src/assets/**/*', '!./src/assets/js/*.*']) | |
.pipe(gulpCopy(paths.build + 'assets', { prefix: 2 })) | |
.pipe(browserSync.reload({ | |
stream: true | |
}) | |
); | |
}); | |
/** | |
* Compile .twig files and pass in data from json file | |
* matching file name. index.twig - index.twig.json | |
*/ | |
gulp.task('twig', function () { | |
return gulp.src(['./client/templates/*.twig']) | |
// Stay live and reload on error | |
.pipe(plumber({ | |
handleError: function (err) { | |
console.log(err); | |
this.emit('end'); | |
} | |
})) | |
// Load template pages json data | |
.pipe(data(function (file) { | |
return JSON.parse(fs.readFileSync(paths.data + path.basename(file.path) + '.json')); | |
})) | |
// Load default json data | |
.pipe(data(function () { | |
return JSON.parse(fs.readFileSync(paths.data + path.basename('default.twig.json'))); | |
})) | |
.on('error', function (err) { | |
process.stderr.write(err.message + '\n'); | |
this.emit('end'); | |
}) | |
.pipe(twig()) | |
.on('error', function (err) { | |
process.stderr.write(err.message + '\n'); | |
this.emit('end'); | |
}) | |
.pipe(gulp.dest(paths.build)) | |
.pipe(browserSync.reload({ | |
stream: true | |
})); | |
}); | |
/** | |
* Recompile .twig files and live reload the browser | |
*/ | |
gulp.task('rebuild', ['twig'], function () { | |
// BrowserSync Reload | |
browserSync.reload(); | |
}); | |
/** | |
* Wait for twig, js and sass tasks, then launch the browser-sync Server | |
*/ | |
gulp.task('browser-sync', ['sass', 'twig', 'js'], function () { | |
browserSync({ | |
server: { | |
baseDir: paths.build | |
}, | |
notify: false, | |
browser:"google chrome" | |
}); | |
}); | |
/** | |
* Compile .scss files into build css directory With autoprefixer no | |
* need for vendor prefixes then live reload the browser. | |
*/ | |
gulp.task('sass', function () { | |
return gulp.src(paths.sass + 'main.scss') | |
.pipe(sourcemaps.init()) | |
// Stay live and reload on error | |
.pipe(plumber({ | |
handleError: function (err) { | |
console.log(err); | |
this.emit('end'); | |
} | |
})) | |
.pipe(sass({ | |
includePaths: [paths.sass, paths.sass + 'decoroom/'], | |
outputStyle: 'expanded' | |
}) | |
.on('error', function (err) { | |
console.log(err.message); | |
// sass.logError | |
this.emit('end'); | |
}) | |
) | |
.pipe(prefix(['last 15 versions','> 1%','ie 8','ie 7','iOS >= 9','Safari >= 9','Android >= 4.4','Opera >= 30'], { | |
cascade: true | |
})) | |
.pipe(sourcemaps.write('.')) | |
.pipe(gulp.dest(paths.css)); | |
}); | |
/** | |
* Compile .js files into build js directory With app.min.js | |
*/ | |
gulp.task('js', function(){ | |
return gulp.src('src/assets/js/script.js') | |
.pipe(sourcemaps.init()) | |
.pipe(concat('script.js')) | |
.on('error', function (err) { | |
console.log(err.toString()); | |
this.emit('end'); | |
}) | |
.pipe(browserSync.reload({ | |
stream: true | |
})) | |
.pipe(sourcemaps.write('.')) | |
.pipe(gulp.dest(paths.build + 'assets/js')); | |
}); | |
/** | |
* Watch scss files for changes & recompile | |
* Watch .twig files run twig-rebuild then reload BrowserSync | |
*/ | |
gulp.task('watch', function () { | |
// Script JS | |
gulp.watch(paths.assets + 'js/script.js', ['js', browserSync.reload]); | |
// SCSS files or main.scss | |
gulp.watch([paths.sass + 'vendors/*.scss', paths.sass + '*.scss'], ['sass', browserSync.reload]); | |
gulp.watch(['client/templates/**/*.twig','client/data/*.twig.json'], {cwd:'./'}, ['rebuild']); | |
// Assets Watch and copy to build in some file changes | |
gulp.watch(['src/assets/**/*', '!src/assets/js/*.js', '!src/assets/css/*.css'], ['copy-assets', browserSync.reload]); | |
}); | |
// Build task compile sass and twig. | |
gulp.task('build', [ 'clean','sass', 'js','twig','copy-assets']); | |
/** | |
* Default task, running just `gulp` will compile the sass, | |
* compile the project site, launch BrowserSync then watch | |
* files for changes | |
*/ | |
gulp.task('default', ['browser-sync', 'watch']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment