-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
93 lines (83 loc) · 2.98 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Requiring Gulp
var gulp = require('gulp'),
sass = require('gulp-sass'), // Requiring gulp-sass (compiles SCSS)
sourcemaps = require('gulp-sourcemaps'), // Requiring sourcemaps (helps working locally)
autoprefixer = require('gulp-autoprefixer'), // Requiring autoprefixer (adds browser prefixes)
cssnano = require('gulp-cssnano'), // Requiring cssnano (minifies CSS)
imagemin = require('gulp-imagemin'), // Requiring imagemin (lossless image optimization)
plumber = require('gulp-plumber'), // Requiring plumber ()
notify = require("gulp-notify"), // Requiring notify ()
browserSync = require('browser-sync'), // Requiring browser-sync (browser refresh)
shell = require('gulp-shell'), // Requiring gulp-shell (used for KSS node)
kssNode = 'node ' + __dirname + '/node_modules/kss/bin/kss-node '; // Require kss-node
var server = browserSync.create();
var paths = {
styles: {
src: 'source/assets/stylesheets/*.scss',
dest: 'build/assets/stylesheets'
},
images: {
src: 'source/assets/images/**',
dest: 'build/assets/images'
}
}
// Start KSS (style guide) task
function kss(done) {
shell.task(
[kssNode + '--config source/kss-config.json']);
done();
}
// Start browserSync server
function startServer(done) {
server.init({
// Display the build folder first
startPath: 'styleguide',
server: {
// Start in root (important for relative paths between build and style guide folders)
baseDir: ''
}
});
done();
}
// Reload browserSync server
function reloadServer(done) {
server.reload();
done();
}
// Stylesheets task
function stylesheets() {
return gulp
.src(paths.styles.src) // Get all *.scss files
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")})) // Show error
.pipe(sourcemaps.init()) // Initialize sourcemap plugin
.pipe(sass().on('error', sass.logError)) // Compiling sass
.pipe(autoprefixer('last 2 version')) // Adding browser prefixes
.pipe(sourcemaps.write()) // Writing sourcemaps
.pipe(cssnano()) // Compress
.pipe(gulp.dest(paths.styles.dest))
.pipe(browserSync.stream());
}
// Images task
function images() {
return gulp
.src(paths.images.src)
.pipe(imagemin())
.pipe(gulp.dest(paths.images.dest));
}
// Watch files
function watchFiles() {
gulp.watch('source/assets/stylesheets/**/*.scss', stylesheets); // Watch for SCSS changes
gulp.watch('source/assets/images/**/*', images); // Watch for image changes
gulp.watch('source/**', kss); // Watch for style guide changes
gulp.watch(['build/**.html', 'styleguide/**.html'], reloadServer);
}
// Define build tasks
var watch = gulp.parallel(startServer, watchFiles);
var build = gulp.parallel(stylesheets, images, kss);
// Exports
exports.stylesheets = stylesheets;
exports.images = images;
exports.watchFiles = watchFiles;
exports.watch = watch;
exports.build = build;
exports.default = watch;