-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
285 lines (232 loc) · 6.58 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// gulp
var gulp = require('gulp');
// clean files - clean compiled files before new compilation
var clean = require('gulp-clean');
// show alerts in corner of screen
var notify = require('gulp-notify');
// run a series of gulp tasks in order
var runSequence = require('run-sequence');
// server (localhost) and livereload
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var a_pp = require('adaptive-pixel-perfect').create();
// pug (jade) compilation - html-preprocessor
var pug = require('gulp-pug');
// process only changed or new files
// var changed = require('gulp-changed');
// var newer = require('gulp-newer');
// cache files and their contents - process only different (changed) files
var cached = require('gulp-cached');
var filter = require('gulp-filter');
// sass/scss compilation
var sass = require('gulp-sass');
// sass/scss sourcemaps
var sourcemaps = require('gulp-sourcemaps');
// sass/scss global import - you can import all files in directory without writing names - @import "some-folder/**/*"
var sassGlob = require('gulp-sass-glob');
//vendor autoprefixer
var autoprefixer = require('gulp-autoprefixer');
// images optimization - jpg, png, svg
var image = require('gulp-image');
// Paths
var paths = {
dist: {
html: './', // same directory
js: 'js',
css: 'css',
img: 'img',
server: './' // same directory
},
src: {
pug: ['pug/*.pug'],
pugDir: 'pug',
js: 'js/**/*.js',
sass: 'sass/**/*.sass',
img: ['img/**/*']
},
watch: {
pug: 'pug/*.pug',
pugLib: 'pug/*/**/*.pug',
js: 'js/**/*.js',
sass: 'sass/**/*',
img: 'img/**/*'
},
clean: {
css: 'css',
cssMap: 'css/**/*.map',
html: '*.html',
templates: 'templates'
}
};
// pug compilation
gulp.task('pug', function() {
return gulp.src(paths.src.pug)
/*.pipe(changed(paths.dist.html, {
extension: '.html'
}))*/
.pipe(cached('pug'))
.pipe(pug({
pretty: '\t'
}))
.on('error', notify.onError(function (error) {
return error.message;
}))
.pipe(gulp.dest(paths.dist.html))
.on('end', reload);
});
gulp.task('pug-nocache', function() {
return gulp.src(paths.src.pug)
/*.pipe(changed(paths.dist.html, {
extension: '.html'
}))*/
.pipe(pug({
pretty: '\t'
}))
.on('error', notify.onError(function (error) {
return error.message;
}))
.pipe(gulp.dest(paths.dist.html))
.on('end', reload);
});
// sass compilation
gulp.task('sass', function () {
return gulp.src(paths.src.sass)
.pipe(sassGlob())
.pipe(sourcemaps.init())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'expanded'
}))
.on('error', notify.onError(function (error) {
return error.message;
}))
.pipe(autoprefixer({
browsers: ['last 5 versions'],
cascade: false
}))
.on('error', notify.onError(function (error) {
return error.message;
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.dist.css))
.pipe(reload({stream: true}));
});
//clean-sass-map
gulp.task('clean-sass-map', function () {
return gulp.src(
[
paths.clean.cssMap
],
{
read: false
}
)
.pipe(clean());
});
// sass compilation production - without soursemaps, minified
gulp.task('sass-production', function () {
return gulp.src(paths.src.sass)
.pipe(sassGlob())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'compressed'
}))
.on('error', notify.onError(function (error) {
return error.message;
}))
.pipe(autoprefixer({
browsers: ['last 5 versions'],
cascade: false
}))
.on('error', notify.onError(function (error) {
return error.message;
}))
.pipe(gulp.dest(paths.dist.css))
.pipe(reload({stream: true}));
});
// images optimization
/*gulp.task('img', function () {
return gulp.src(paths.src.img)
// take only changed images
.pipe(cached(paths.src.img))
.pipe(image())
.pipe(gulp.dest(paths.dist.img));
});*/
// clean
gulp.task('clean', function () {
return gulp.src(
[
paths.clean.css,
paths.clean.html,
paths.clean.templates
],
{
read: false
}
)
.pipe(clean());
});
// server (browserSync) settings
var port = 3010;
var folderForDesignScreenshots = "design";
var portForBrowserSync = 3000;
var settings = {
server: paths.dist.server,
cors: true,
middleware: function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
},
socket: {
domain: 'localhost:' + portForBrowserSync
},
scriptPath: function (path, port, options) {
return "http://" + options.getIn(['socket', 'domain']) + path;
}
};
// browserSync server (localhost)
gulp.task('server', function() {
browserSync(settings);
a_pp.start(port, folderForDesignScreenshots, portForBrowserSync);
});
// watch common files changes for default and production tasks - and re-compile, reload
gulp.task('watch-common', ['server'], function(){
// watch pug
gulp.watch(paths.watch.pug, function(event, cb) {
gulp.start('pug');
}, reload);
gulp.watch(paths.watch.pugLib, function(event, cb) {
gulp.start('pug-nocache');
}, reload);
// watch js
gulp.watch(paths.watch.js).on('change', reload);
// watch img
//gulp.watch(paths.watch.img).on('change', reload);
});
// watch files changes and re-compile, reload
gulp.task('watch', ['server', 'watch-common'], function(){
// watch sass
gulp.watch(paths.watch.sass, function(event, cb) {
gulp.start('sass');
});
});
// watch files changes and re-compile, reload
gulp.task('watch-production', ['server', 'watch-common'], function() {
// watch sass and do sass-production
gulp.watch(paths.watch.sass, function(event, cb) {
gulp.start('sass-production');
});
});
// default task
gulp.task('default', ['sass', 'watch']);
// production
gulp.task('prod', function(cb) {
// run functions in order - first clean (delete) files, then others
runSequence(
//'clean',
'clean-sass-map',
[/*'img',*/ 'pug', 'sass-production'],
//'watch-production',
cb
);
});