-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
140 lines (127 loc) · 3.54 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
const gulp = require('gulp');
const path = require('path');
const del = require('del');
const $ = require('gulp-load-plugins')();
const browserSync = require('browser-sync');
// Directory paths
const paths = {
public: 'dist',
jade: {
base: '**/*.pug',
src: 'src/*.pug',
dist: 'dist',
data: './src/_data/es/index.pug.json'
},
sass: {
base: 'src/assets/css',
src: 'src/assets/css/*.scss',
dist: 'dist/css',
clean: 'dist/css/*'
},
js: {
base: 'src/assets/js/*.js',
src: ['src/assets/js/*.js', '!src/assets/js/*.min.js'],
dist: 'dist/js',
clean: 'dist/js/*'
},
images: {
src: 'src/assets/img/**/*',
dist: 'dist/img',
clean: 'dist/img/*'
}
};
// De-caching function for Data files
function requireUncached( $module ) {
delete require.cache[require.resolve( $module )];
return require( $module );
}
// Configure static server with BrowserSync
// and wait for Sass, Jade and Scripts tasks to launch it.
gulp.task('browser-sync', ['sass', 'jade', 'scripts', 'images'], () => {
browserSync.init({
server: {
baseDir: paths.public,
browser: ['google chrome']
}
});
});
// Reload browser
gulp.task('bs-reload', () => {
browserSync.reload();
});
// Compile .jade files and pass in data from json file
// matching file name. index.jade - index.jade.json
gulp.task('jade', () => {
gulp.src(paths.jade.src)
.pipe($.plumber())
.pipe($.data(function (file) {
return requireUncached(paths.jade.data);
}))
.pipe($.pug({
pretty: true,
}))
.pipe($.plumber.stop())
.pipe(gulp.dest(paths.jade.dist))
.pipe(browserSync.stream());
});
// Compile Sass into CSS with vendor prefixes – then reload the browser.
gulp.task('sass', () => {
del([paths.sass.clean]);
gulp.src(paths.sass.src)
.pipe($.plumber())
.pipe($.sass({
includePaths: [paths.sass.base],
outputStyle: 'compressed'
}))
.on('error', $.sass.logError)
.pipe($.autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe($.plumber.stop())
.pipe(gulp.dest(paths.sass.dist))
.pipe(browserSync.stream());
});
// Parse and compress JavaScript files – then reload the browser.
gulp.task('scripts', () => {
del([paths.js.clean]);
gulp.src(paths.js.base)
.pipe(gulp.dest(paths.js.dist))
gulp.src(paths.js.src)
.pipe($.plumber())
.pipe($.uglify())
.pipe($.rename({ suffix: '.min' }))
.pipe($.plumber.stop())
.pipe(gulp.dest(paths.js.dist))
.pipe(browserSync.stream());
});
// Optimize and compress images
gulp.task('images', () => {
gulp.src(paths.images.src)
.pipe($.plumber())
.pipe($.newer(paths.images.dist))
.pipe($.imagemin([
$.imagemin.gifsicle({interlaced: true}),
$.imagemin.jpegtran({progressive: true}),
$.imagemin.optipng({optimizationLevel: 5}),
$.imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
.pipe($.plumber.stop())
.pipe(gulp.dest(paths.images.dist));
});
// Complete build sequence
gulp.task('build', ['sass', 'jade', 'scripts', 'images']);
// Launch the BrowserSync static server and watch files for changes
gulp.task('default', ['browser-sync'], () => {
gulp.watch(paths.sass.base + '/**/*.scss', ['sass']);
gulp.watch([paths.jade.base, paths.jade.data], ['jade']);
gulp.watch(paths.js.base, ['scripts', 'bs-reload']);
var imgWatcher = gulp.watch(paths.images.src, ['images'])
// One-way sync – When images are deleted
imgWatcher.on('change', (ev) => {
if (ev.type === 'deleted') {
del(path.relative('', ev.path).replace('src/', 'dist/'));
}
})
});