-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
214 lines (184 loc) · 6.19 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
/*
Need to:
- Load a web server
- Watch changes in files
- Reload browser when changes are made
- Compile scss to css and serve to dist
*/
// -----------------------------------------------------------------------------
// Required libs
// -----------------------------------------------------------------------------
var gulp = require('gulp'),
size = require('gulp-size'),
plumber = require('gulp-plumber'),
notify = require('gulp-notify'),
sassLint = require('gulp-sass-lint'),
del = require('del'),
vinylPaths = require('vinyl-paths'),
sourcemaps = require('gulp-sourcemaps'),
colors = require('colors'),
// Temporary solution until gulp 4
// https://github.com/gulpjs/gulp/issues/355
runSequence = require('run-sequence'),
strip = require('gulp-strip-comments');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var cleanCSS = require('gulp-clean-css');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
// -----------------------------------------------------------------------------
// Folder structure
// -----------------------------------------------------------------------------
var bases = {
app: 'src/',
dist: 'dist/'
};
// -----------------------------------------------------------------------------
// Minify HTML settings
// -----------------------------------------------------------------------------
var settings = {
minHtml: false,
removeHtmlComment: true
};
// -----------------------------------------------------------------------------
// Gulp error handling
// -----------------------------------------------------------------------------
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
var displayError = function(error) {
// Initial building up of the error
var errorString = '[' + error.plugin.error.bold + ']';
errorString += ' ' + error.message.replace('\n', ''); // Removes new line at the end
// If the error contains the filename or line number add it to the string
if (error.fileName) errorString += ' in ' + error.fileName;
if (error.lineNumber) errorString += ' on line ' + error.lineNumber.bold;
// This will output an error like the following:
// [gulp-sass] error message in file_name on line 1
console.error(errorString);
};
var onError = function(err) {
notify.onError({
title: 'Gulp',
subtitle: 'Failure!',
message: 'Error: <%= error.message %>',
sound: 'Basso'
})(err);
this.emit('end');
};
var sassOptions = {
outputStyle: 'expanded'
};
// -----------------------------------------------------------------------------
// Clean dist folder
// -----------------------------------------------------------------------------
gulp.task('clean:dist', function() {
return gulp.src(bases.dist).pipe(vinylPaths(del));
});
// -----------------------------------------------------------------------------
// Style Compiling
// -----------------------------------------------------------------------------
gulp.task('styles', function() {
return gulp
.src(bases.app + 'scss/styles.scss')
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(sass(sassOptions))
.pipe(size({ gzip: true, showFiles: true }))
.pipe(postcss([autoprefixer()]))
.pipe(rename('styles.css'))
.pipe(gulp.dest(bases.dist + 'css'))
.pipe(reload({ stream: true }))
.pipe(
cleanCSS({ debug: true }, function(details) {
console.log(details.name + ': ' + details.stats.originalSize);
console.log(details.name + ': ' + details.stats.minifiedSize);
})
)
.pipe(size({ gzip: true, showFiles: true }))
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(bases.dist + 'css'));
});
// -----------------------------------------------------------------------------
// Sass/SCSS linting
// -----------------------------------------------------------------------------
gulp.task('sass-lint', function() {
gulp
.src([
bases.app + 'scss/**/*.scss',
'!' + bases.app + 'scss/libs/**/*.scss',
'!' + bases.app + 'scss/libs/bootstrap4/_print.scss'
])
.pipe(sassLint())
.pipe(sassLint.format())
.pipe(sassLint.failOnError());
});
// -----------------------------------------------------------------------------
// HTML Minification - Default setting: HTML not minified but comments removed
// -----------------------------------------------------------------------------
gulp.task('minify-html', function() {
var pipelin = gulp.src(bases.app + './*.html');
if (settings.removeHtmlComment) {
pipelin.pipe(strip());
}
if (settings.minHtml) {
pipelin.pipe(htmlmin({ collapseWhitespace: true }));
}
pipelin.pipe(gulp.dest(bases.dist)).pipe(reload({ stream: true }));
});
// -----------------------------------------------------------------------------
// Deploy HTML files to dist folder
// -----------------------------------------------------------------------------
gulp.task('html-deploy', function() {
// Gets .html in pages
return (
gulp
.src(bases.app + 'pages/**/*.html')
// output files to dist folder
.pipe(gulp.dest(bases.dist))
.pipe(reload({ stream: true }))
);
});
// -----------------------------------------------------------------------------
// Local server
// -----------------------------------------------------------------------------
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: bases.dist
}
});
});
// -----------------------------------------------------------------------------
// Watchers
// -----------------------------------------------------------------------------
gulp.task('watch', function() {
gulp
.watch(bases.app + 'scss/**/*.scss', ['styles'])
.on('change', browserSync.reload);
gulp
.watch(bases.app + 'pages/*.html', ['minify-html', 'html-deploy'])
.on('change', browserSync.reload);
});
gulp.task('default', function(done) {
runSequence(
'clean:dist',
'styles',
'minify-html',
'html-deploy',
'browser-sync',
'watch',
done
);
});