forked from imjakechapman/CraftCMS-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
191 lines (166 loc) · 4.83 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
// Ah, Big Gulp's eh? Welp, see ya later.
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
chalk = require('chalk'),
concat = require('gulp-concat'),
changed = require('gulp-changed'),
del = require('del'),
gutil = require('gulp-util'),
imagemin = require('gulp-imagemin'),
jshint = require('gulp-jshint'),
livereload = require('gulp-livereload'),
minifycss = require('gulp-minify-css'),
notify = require('gulp-notify'),
plumber = require('gulp-plumber'),
pngquant = require('imagemin-pngquant'),
rename = require('gulp-rename'),
sass = require('gulp-sass'),
scsslint = require('gulp-scsslint'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify');
// Directories
var SRC = 'public/assets',
DIST_DIR = 'public/dist';
// JS files we'll be using
var JS = [
SRC + '/js/*.js'
];
var VENDOR_JS = [
SRC + '/js/vendor/*.js'
];
var IMAGE_SRC = [
SRC + '/images/**/*'
];
// SCSS Linting, Compiling and Minification
var scssLintReporter = function(file) {
if ( !file.scsslint.success ) {
gutil.beep();
notify().write({ message: file.scsslint.errorCount + ' error in scss' });
// Loop through the warnings/errors and spit them out
file.scsslint.results.forEach(function(result) {
var msg =
chalk.cyan(file.path) + ':' +
chalk.red(result.line) + ' ' +
('error' === result.severity ? chalk.red('[E]') : chalk.cyan('[W]')) + ' ' +
result.reason;
gutil.log(msg);
});
} else {
notify().write({ message: 'SCSS Linted' });
gulp.start('styles');
}
};
gulp.task('scss-lint', function() {
gulp.src(SRC + '/styles/app.scss')
.pipe(plumber({
handleError: function (err) {
console.log(err);
this.emit('end');
}
}))
.pipe(scsslint({
'config': '.scss-lint.yml'
}));
});
gulp.task('styles', function(){
gulp.src(SRC + '/styles/app.scss')
.pipe(plumber({
handleError: function (err) {
console.log(err);
this.emit('end');
}
}))
.pipe(
sass({
outputStyle: 'expanded',
debugInfo: true,
lineNumbers: true,
errLogToConsole: true,
onSuccess: function(){
notify().write({ message: "SCSS Compiled successfully!" });
},
onError: function(err) {
gutil.beep();
notify().write(err);
}
})
)
.pipe( sourcemaps.init() )
.pipe( autoprefixer('last 3 versions') )
.pipe( rename({ suffix: '.min' }) )
.pipe( minifycss() )
.pipe( sourcemaps.write('.') )
.pipe( gulp.dest(DIST_DIR + '/styles') )
.pipe( notify('Styles Finished') );
});
// JS Scripts
gulp.task("scripts", function() {
return gulp.src( JS )
.pipe(plumber({
handleError: function (err) {
console.log(err);
this.emit('end');
}
}))
.pipe( jshint() )
.pipe( jshint.reporter('jshint-stylish') )
.pipe( jshint.reporter('fail') )
.pipe( uglify() )
.pipe( gulp.dest(DIST_DIR + '/js') )
.pipe( notify('Scripts Finished') );
});
//Just copy over the vendor js. Don't need to jshint, uglify, etc.
gulp.task('vendor-js', function() {
return gulp.src( VENDOR_JS )
.pipe(plumber({
handleError: function (err) {
console.log(err);
this.emit('end');
}
}))
.pipe(gulp.dest(DIST_DIR + '/js/vendor'))
.pipe(notify('Vendor JS Copied'));
});
// Image Minification
gulp.task('image-min', function () {
return gulp.src( IMAGE_SRC )
.pipe( changed(DIST_DIR + '/images') )
.pipe( imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: true}],
use: [pngquant()]
})
)
.pipe( gulp.dest( DIST_DIR + '/images' ) )
.pipe( notify('Images Compressed') );
});
// Fonts
gulp.task('fonts', function() {
gulp.src(SRC + '/fonts/**/*')
.pipe(plumber({
handleError: function (err) {
console.log(err);
this.emit('end');
}
}))
.pipe(gulp.dest(DIST_DIR + '/fonts'));
});
// Clean dist directory for rebuild
gulp.task('clean', function() {
del(['tmp/*.js', '!tmp/unicorn.js', DIST_DIR], function (err, paths) {
console.log('Deleted files/folders:\n', paths.join('\n'));
});
});
// Do the creep, ahhhhhhh! (http://youtu.be/tLPZmPaHme0?t=7s)
gulp.task('watch', function() {
// Listen on port 35729
livereload.listen();
// Watch .scss files
gulp.watch(SRC + '/styles/**/*.scss', ['scss-lint', 'styles']);
// Watch .js files to lint and build
gulp.watch(SRC + '/js/**/*.js', ['scripts', 'vendor-js']);
// Watch image files
gulp.watch( 'public/images/**/*', ['image-min']);
});
// Gulp Default Task
gulp.task('default', ['scss-lint', 'scripts', 'vendor-js', 'fonts', 'image-min', 'watch']);