-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
335 lines (269 loc) · 8.74 KB
/
Copy pathgulpfile.js
File metadata and controls
335 lines (269 loc) · 8.74 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// GULP TEMPLATE - Gulfile.js - Victor Allegret
//
// - $ gulp
// - $ gulp build
// - $ gulp clean
//
// --------------------------------------------------------
////////////////////
// VARIABLES
////////////////////////////////////////////////////////////////////////////////
// REQUIRE
// ---------------------------------------------------------
var gulp = require('gulp'),
$ = require('gulp-load-plugins')({pattern: '*'})
var gulpSequence = require('gulp-sequence');
// PATH
// ---------------------------------------------------------
///// PATHS FOR DEV
var slim_dev = './dev/views/',
sass_dev = './dev/assets/stylesheets/',
coffee_dev = './dev/assets/javascripts/',
fonts_dev = './dev/assets/fonts/',
img_dev = './dev/assets/images/',
dev = './dev';
///// PATH FOR PROD
var slim_build = './build/views/',
sass_build = './build/assets/stylesheets/',
coffee_build = './build/assets/javascripts/',
fonts_build = './build/assets/fonts/',
img_build = './build/assets/images/',
build = './build';
////////////////////
// TASKS
////////////////////////////////////////////////////////////////////////////////
// COMPILE SLIM TO HTML
// ---------------------------------------------------------
gulp.task('slim', function () {
return gulp.src(slim_dev + '/**/*.slim')
// prevent server from crashing
.pipe($.plumber({ errorHandler: function(err) {
$.notify.onError({
title: "Gulp error in " + err.plugin
})(err);
}}))
// Keep non-updated files infos in cache
.pipe($.cached(slim_dev + '/**/*.slim'))
// Ignore partials
.pipe($.filter(function (file) {
return !/\/_/.test(file.path) && !/^_/.test(file.relative);
}))
// Wrap files into application
.pipe($.wrap({ src: slim_dev + 'layout/application.slim'}))
// compile slim to html
.pipe($.slim({
pretty: false,
include: true
}))
// remove all folder
.pipe($.rename({dirname: ''}))
// copy result to build folder
.pipe(gulp.dest(build))
// notify when task completed
.pipe($.notify({message: 'Slim compilation completed !', onLast: true}));
});
// COMPILE SASS TO CSS
// ---------------------------------------------------------
gulp.task('sass', function () {
return gulp.src(sass_dev + 'main.sass')
// prevent server from crashing
.pipe($.plumber({ errorHandler: function(err) {
$.notify.onError({
title: "Gulp error in " + err.plugin,
})(err);
}}))
// init sourcemaps
.pipe($.sourcemaps.init())
// add sass glob import
.pipe($.sassGlob())
// compile sass to css
.pipe($.sass())
// add auto-prefixes
.pipe($.autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
// concat all files
.pipe($.concat('main.css'))
// rename to .min
.pipe($.rename('main.min.css'))
// write sourcemaps
.pipe($.sourcemaps.write())
// copy result to build folder
.pipe(gulp.dest(sass_build))
// notify when task completed
.pipe($.notify({message: 'Sass compilation completed !', onLast: true}));
});
// REMOVE UNUSED CSS
// ---------------------------------------------------------
gulp.task('optimizeCss', function () {
return gulp.src(sass_build + '/*.min.css')
// prevent server from crashing
.pipe($.plumber({ errorHandler: function(err) {
$.notify.onError({
title: "Gulp error in " + err.plugin,
})(err);
}}))
// remove unused css
.pipe($.uncss({
html: [build + '/**/*.html']
}))
// minify css
.pipe($.cleanCss())
// copy result to build folder
.pipe(gulp.dest(sass_build))
// notify when task completed
.pipe($.notify({message: 'Css are optimized !', onLast: true}));
});
// CONCAT CSS VENDORS
// ---------------------------------------------------------
gulp.task('cssVendors', function () {
return gulp.src(sass_dev + '/vendors/**/*.{css,scss,sass}')
// compile sass to css
.pipe($.sass())
// concat all files
.pipe($.concat('vendors.css'))
// rename to .min
.pipe($.rename('vendors.min.css'))
// copy result to build folder
.pipe(gulp.dest(sass_build))
// notify when task completed
.pipe($.notify({message: 'Css vendors compilation completed !', onLast: true}));
});
// COMPILE COFFEE TO JS
// ---------------------------------------------------------
gulp.task('coffee', function() {
return gulp.src(coffee_dev + '/main.coffee')
// prevent server from crashing
.pipe($.plumber({ errorHandler: function(err) {
$.notify.onError({
title: "Gulp error in " + err.plugin,
})(err);
}}))
// add include for coffee
.pipe($.include({ extensions: "coffee" }))
// compile coffee to js
.pipe($.coffee())
// concat all files
.pipe($.concat('main.js'))
// rename to .min
.pipe($.rename('main.min.js'))
// minify js
.pipe($.uglify())
// // copy result to build folder
.pipe(gulp.dest(coffee_build))
// notify when task completed
.pipe($.notify({message: 'Coffee compilation completed !', onLast: true}));
});
// INSTALL VENDORS WITH BROWSERIFY
// ---------------------------------------------------------
gulp.task('jsVendors', function() {
return gulp.src(coffee_dev + '/vendors.js')
// require node packages
.pipe($.browserify({
insertGlobals: true
}))
// minify js
.pipe($.uglify())
// concat all files
.pipe($.concat('vendors.js'))
// rename to .min
.pipe($.rename('vendors.min.js'))
// copy result to build folder
.pipe(gulp.dest(coffee_build))
// notify when task completed
.pipe($.notify({message: 'Js vendors compilation completed !', onLast: true}));
});
// FONTS
// ---------------------------------------------------------
gulp.task('fonts', function() {
return gulp.src(fonts_dev + '/**/*.{eot,svg,ttf,woff,woff2}')
// remove under-folder
.pipe($.rename({dirname: ''}))
// copy result to build folder
.pipe(gulp.dest(fonts_build))
// notify when task completed
.pipe($.notify({message: 'Fonts compilation completed !', onLast: true}));
});
// MINIFY IMAGES
// ---------------------------------------------------------
gulp.task('img', function () {
return gulp.src(img_dev + '/**/*.{png,jpg,jpeg,gif,svg,ico}')
// run task only for new images
.pipe($.cached(img_dev + '/**/*.{png,jpg,jpeg,gif,svg,ico}'))
// minify images
.pipe($.imagemin())
// copy result to build folder
.pipe(gulp.dest(img_build))
// notify when task completed
.pipe($.notify({message: 'Image are optimized !', onLast: true}));
});
// TASK CLEAN TRASH
// ---------------------------------------------------------
gulp.task('trash', function () {
return gulp.src(build + '/application.html', { read: false })
// remove folder build
.pipe($.rimraf())
// notify when task completed
.pipe($.notify('Useless html files removed !'));
});
// RELOAD
// ---------------------------------------------------------
///// RELOAD SLIM
gulp.task('reload-slim', ['slim'], function(){
$.browserSync.reload();
});
///// RELOAD SASS
gulp.task('reload-sass', ['sass'], function(){
$.browserSync.reload();
});
///// RELOAD COFFEE
gulp.task('reload-coffee', ['coffee'], function(){
$.browserSync.reload();
});
////////////////////
// COMMANDS
////////////////////////////////////////////////////////////////////////////////
// TASK CLEAN ($ gulp clean)
// ---------------------------------------------------------
gulp.task('clean', function () {
return gulp.src(build, {read: false})
// remove folder build
.pipe($.rimraf())
// notify when task completed
.pipe($.notify('Prod folder deleted !'));
});
// TASK DEV ($ gulp dev)
// ---------------------------------------------------------
gulp.task('dev', gulpSequence('clean', 'slim', 'sass', 'cssVendors', 'coffee', 'jsVendors', 'fonts', 'img'));
// TASK BUILD ($ gulp build)
// ---------------------------------------------------------
gulp.task('build', gulpSequence('dev', 'optimizeCss', 'trash'));
// RUN SERVER ($ gulp)
// ---------------------------------------------------------
///// WATCH
gulp.task('watch', ['dev'], function () {
$.browserSync.init({
port: 3000,
server: {
baseDir: build,
index: "index.html"
},
online: true,
scrollProportionally: true,
notify: false,
snippetOptions: {
rule: {
match: /<\/head>/i,
fn: function (snippet, match) {
return snippet + match;
}
}
}
})
gulp.watch(dev + '/**/*.slim', ['reload-slim']);
gulp.watch(dev + '/**/*.sass', ['reload-sass']);
gulp.watch(dev + '/**/*.coffee', ['reload-coffee']);
});
////// COMMAND
gulp.task('default', ['watch'])