This repository was archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
474 lines (398 loc) · 12.9 KB
/
gulpfile.js
File metadata and controls
474 lines (398 loc) · 12.9 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//--------------------------------------------------------------------------------------------------------------------------------------
// GULP OPTIONS
//--------------------------------------------------------------------------------------------------------------------------------------
/*
Tweak various options to suit the needs of your project
*/
// MINIFY
//--------------------------------------------------------------------------------------------------------------------------------------
/*
If minify is true then CSS & JS will be minified once compiled and will have a .min suffix before the file
extension. For example 'style.min.css'.
*/
const minify = true;
// SASS LINTING
//--------------------------------------------------------------------------------------------------------------------------------------
/*
If lint is true then SASS will be linted by stylelint to enforce style guidelines. These rules can be tweaked
in '.stylelintrc'.
*/
const lint = true;
// CONFIGURE PATHS
//--------------------------------------------------------------------------------------------------------------------------------------
/*
Here you can configure the paths used by Gulp to align with your project's directory structure.
*/
// Development root
const dev = "dev";
// Distribution root
const dist = "dist";
// HTML directories
const htmlDev = "dev/html";
const htmlDist = dist;
// Image directories
const imgDev = "dev/img";
const imgDist = "dist/img";
// SASS directories
const sassDev = "dev/sass";
const sassDist = "dist/css";
// JS directories
const jsDev = "dev/js";
const jsDist = "dist/js";
//--------------------------------------------------------------------------------------------------------------------------------------
// SET DEPENDENCIES
//--------------------------------------------------------------------------------------------------------------------------------------
// Required for all tasks
const gulp = require('gulp');
// Required for SASS tags
const sass = require('gulp-sass');
// Required for SASS sourcemaps
const sourcemaps = require('gulp-sourcemaps');
// Used to lint SASS
const gulpStylelint = require('gulp-stylelint');
// Adds support for SASS globbing
const sassGlob = require('gulp-sass-glob');
// Minifies images
const imagemin = require('gulp-imagemin');
// Used to minify JS
const uglify = require('gulp-uglify');
// Used to rename CSS and JS depending if minified
const rename = require("gulp-rename");
// Used to add conditional functionality
const gulpif = require('gulp-if');
// Used to remove unused CSS styles post compile
const uncss = require('gulp-uncss');
// Used to create synchronous build tasks
const runSequence = require('run-sequence');
// Used to delete folders during build process
const del = require('del');
// Used to add autoprefixer to SASS task
const autoprefixer = require('gulp-autoprefixer');
// Used to compile JS modules
const browserify = require("browserify");
const source = require('vinyl-source-stream');
const glob = require('glob');
const streamify = require('gulp-streamify');
// Used to generate svg icon system and to minify
const svgstore = require('gulp-svgstore');
const svgmin = require('gulp-svgmin');
const path = require('path');
const inject = require('gulp-inject');
// Used to compile nunjacks templates if present
const nunjucks = require('gulp-nunjucks');
// Used to make directories
const mkdirp = require('mkdirp');
//--------------------------------------------------------------------------------------------------------------------------------------
// FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------
/*
Create all the individual functions which will contribute towards our production functions
*/
// SETUP TASK
//--------------------------------------------------------------------------------------------------------------------------------------
/*
Task which you run on project start to setup SASS directory and copy required files from OrionCSS dependancy
Creates SASS directory in dev directory and adds the following:-
- Creates ITCSS directory structure
- sample.main-orion-framework renamed to main and copied to SASS root
- sample.component.mycomponent added to "06 - components" directory
*/
const folders = [
sassDev + "/01 - settings",
sassDev + "/02 - tools",
sassDev + "/03 - generic",
sassDev + "/04 - elements",
sassDev + "/05 - objects",
sassDev + "/06 - components",
sassDev + "/07 - utilities",
jsDev
]
gulp.task('setup', function(){
// Generate directories
for(var i = 0; i < folders.length; i++) {
mkdirp(folders[i], function (err) {
if(err){
console.error(err);
}
});
}
// Grab main sample and move to SASS root
gulp.src('node_modules/orioncss/sample.main-orion-bp.scss')
.pipe(rename('main.scss'))
.pipe(gulp.dest(sassDev))
// Grab sample component and move to new components dir
gulp.src('node_modules/orioncss/06 - components/_sample.component.mycomponent.scss')
.pipe(gulp.dest(sassDev + '/06 - components/'));
// Gram sample JS main, rename and then
gulp.src('node_modules/orionjs/sample.main.js')
.pipe(rename('main.js'))
.pipe(gulp.dest(jsDev))
});
// Developer task to clear content added by setup task so we don't accidently commit it.
gulp.task('unsetup', function(){
del(sassDev);
del(jsDev);
});
// DELETE DIST DIRECTORY
//--------------------------------------------------------------------------------------------------------------------------------------
// Delete any existing Dist directory so old files don't contaminate our new build
gulp.task('deleteDist', function(){
return del(dist);
});
// SASS
//--------------------------------------------------------------------------------------------------------------------------------------
/*
Compile SASS, add autoprefixer and filter out unused CSS styles, that way we can have unlimited utility
classes and only have the ones we're actually using in our compiled CSS file.
We also tell uncss to ignore styles with stateful modifiers as these are typically added into the DOM
dynamically which UNCSS is unable to detect.
*/
gulp.task('sass', function () {
return gulp.src(sassDev + '/*.scss')
.pipe(sassGlob())
.pipe(sourcemaps.init())
.pipe(gulpif(minify, sass({outputStyle: 'compressed', precision: 8}), sass({outputStyle: 'expanded', precision: 8})))
.pipe(gulpif(minify, rename({ suffix: '.min' })))
.on('error', sass.logError)
.pipe(autoprefixer({
browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'],
cascade: false
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./' + sassDist))
});
// A SASS task for debug use. Compiles an unminified stylesheet but uses canon naming structure as
// per minify variable so no links are broken on the page
gulp.task('sass-debug', function () {
return gulp.src(sassDev + '/*.scss')
.pipe(sassGlob())
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'expanded', precision: 8}))
.pipe(gulpif(minify, rename({ suffix: '.min' })))
.on('error', sass.logError)
.pipe(autoprefixer({
browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'],
cascade: false
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./' + sassDist))
});
// Lint SASS Task
gulp.task('sass-lint', function lintCssTask() {
return gulp.src(sassDev + '/**/*.scss')
.pipe(gulpStylelint({
reportOutputDir: 'reports/lint',
reporters: [{
formatter: 'verbose',
console: true,
save: 'report.txt'
}]
}));
});
// UNCSS Task
gulp.task('uncss', function () {
return gulp.src(sassDist + '/*.css')
.pipe(uncss({
html: [htmlDist + '/*.html'],
ignore: [
/\.is-*/,
/\.has-*/,
/\.in-*/,
/\#icon-*/,
/\.js-*/,
/\.ua-*/,
/\.no-*/
]
}))
.pipe(gulp.dest('./' + sassDist))
});
// Watch task for SASS. Lints and then runs standard SASS functions. No UNCSS to speed things up..
gulp.task('sass-watch', function(){
if(lint) {
runSequence(
"sass-lint",
"sass"
);
}
else {
runSequence(
"sass"
);
}
});
// Create seperate sass build task which lints and then runs standard SASS functions followed by UNCSS
// This will be used on Build task. It won't be used on watch task to speed things up.
gulp.task('sass-build', function(){
if(lint) {
runSequence(
"sass-lint",
"sass",
"uncss"
);
}
else {
runSequence(
"sass",
"uncss"
);
}
});
gulp.task('sass-build-debug', function(){
if(lint) {
runSequence(
"sass-lint",
"sass-debug",
"uncss"
);
}
else {
runSequence(
"sass",
"uncss"
);
}
});
// IMAGES
//--------------------------------------------------------------------------------------------------------------------------------------
// Image MIN & with CACHE to stop repeat compressed images
gulp.task('bitmap', function(){
gulp.src(imgDev + '/*.+(png|jpg|gif)')
.pipe(imagemin({optimizationLevel: 3, progressive: true}))
.pipe(gulp.dest(imgDist));
});
// Compile all SVG icons into one large svg icon and inject as inline svg into page header
// We can then directly reference these icons without making an extra request
gulp.task('svg', function () {
var svgs = gulp
.src(imgDev + '/**/*.svg')
.pipe(svgmin(function (file) {
var prefix = path.basename(file.relative, path.extname(file.relative));
return {
plugins: [
{
removeDoctype: true
},
{
removeUselessDefs: true
},
{
removeTitle: true
},
{
cleanupIDs: {
prefix: prefix + '-',
minify: true
}
}
]
}
}))
.pipe(svgstore({ inlineSvg: true }));
function fileContents (filePath, file) {
return file.contents.toString();
}
return gulp
.src(htmlDist + '/*.html')
.pipe(inject(svgs, { transform: fileContents }))
.pipe(gulp.dest(htmlDist + '/'));
});
gulp.task('images', function(){
runSequence(
"bitmap",
"svg"
);
});
// JS
//--------------------------------------------------------------------------------------------------------------------------------------
gulp.task('js', function() {
var files = glob.sync('./' + jsDev + '/*.js');
files.map(function(file) {
var name = file.replace('./' + jsDev + '/', '');
name = name.replace('.js', '');
return browserify({entries: file})
.transform('babelify', {presets: ['es2015']})
.bundle()
.pipe(source(file))
.pipe(gulpif(minify, rename({
dirname: "",
basename: name,
suffix: ".min",
extname: ".js"
}), rename({
dirname: "",
basename: name,
extname: ".js"
})))
.pipe(gulpif(minify, streamify(uglify())))
.pipe(gulp.dest('./' + jsDist + '/'));
});
});
// HTML
//--------------------------------------------------------------------------------------------------------------------------------------
gulp.task('html-copy', function() {
return gulp.src('./' + htmlDev +'/*.html')
.pipe(nunjucks.compile())
.pipe(gulp.dest(htmlDist + '/'));
});
/*
HTML task need to also run SVG task as SVG task copies master file into html document.
It also needs to run SASS task as new CSS classes may have been used which need to be added to
the compiled CSS file.
*/
gulp.task('html', function(){
runSequence(
"html-copy",
"svg",
"sass"
);
});
// PHP / SQL
//--------------------------------------------------------------------------------------------------------------------------------------
/* Copy PHP & SQL files across and keep their directory structure intact */
gulp.task("php", function() {
return gulp.src('./' + dev + '/**/*.+(php|sql)')
.pipe(gulp.dest(dist + '/'));
});
// MISC
//--------------------------------------------------------------------------------------------------------------------------------------
// Copy Misc Files Task
gulp.task('copy', function() {
// Copy all non-directory files
gulp.src(dev + '/*.+(xml|txt|json)')
.pipe(gulp.dest(dist + '/'));
// Copy specified folders and contents
gulp.src('*/+(fonts)/**', {base:"./" + dev + "/"})
.pipe(gulp.dest(dist + '/'));
// Copy HTACCESS file seperately as it wouldn't play nice
gulp.src(dev + '/.htaccess')
.pipe(gulp.dest(dist + '/'));
});
//--------------------------------------------------------------------------------------------------------------------------------------
// PRODUCTION FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------
/*
Here we pull everything together into generic watch and build functions
*/
// WATCH FUNCTION
gulp.task("watch", function() {
// HTML
gulp.watch(htmlDev + '/**/*.html',['html']);
// PHP & MYSQL
gulp.watch(dev + '/**/*.(php|sql)',['php']);
// Images
gulp.watch(imgDev + '/*.+(png|jpg|gif|svg)',['images']);
// SASS
// UNCSS doesn't run for watch task to speed things up
gulp.watch(sassDev + '/**/*.scss',['sass-watch']);
// JS
gulp.watch(jsDev + '/**/*.js',['js']);
});
// BUILD FUNCTION
gulp.task('build',function() {
runSequence(
// Delete Dist Folder
"deleteDist",
// Run other tasks asynchronously
["html", "php", "sass-build", "js", "copy", "images"]
);
});