This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
266 lines (236 loc) · 8.87 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
'use strict';
/****** DEPENDENCIES ********/
var autoprefixer = require('autoprefixer'),
browserSync = require('browser-sync').create(),
color = require('colors'),
del = require('del'),
gulp = require('gulp'),
imagemin = require('gulp-imagemin'),
jsHint = require('gulp-jshint'),
jsHintStylish = require('jshint-stylish'),
postCss = require('gulp-postcss'),
process = require('yargs').argv,
sass = require('gulp-sass'),
sassGlob = require('gulp-sass-glob'),
sassLint = require('gulp-sass-lint'),
sourceMaps = require('gulp-sourcemaps');
/********** VARIABLES *************/
// Hosts - change localhost for see it in browsersync
var path = 'localhost';
// Paths
var srcAssets = {
images: 'assets/images/',
styles: 'assets/sass/'
};
var distAssets = {
images: 'images/',
js: 'js/',
styles: 'css/'
};
/********** TASKS ***************/
gulp.task('default', function () {
console.log('');
console.log('Cleaning tasks'.yellow);
console.log('gulp ' + 'clean:styles'.cyan + ' ' + '# Clean css files from css directory'.grey);
console.log('gulp ' + 'clean:images'.cyan + ' ' + '# Clean image files from images directory'.grey);
console.log('');
console.log('Compiling tasks'.yellow);
console.log('gulp ' + 'imagemin'.cyan + ' ' + '# Minifiy your images in ./assets/images into ./images'.grey);
console.log('gulp ' + 'mainStyles:dev'.cyan + ' ' + '# Compile expanded css except "pages" directory and create a maps file.'.grey);
console.log('gulp ' + 'pageStyles:dev'.cyan + ' ' + '# Compile expanded css from "pages" directory exclusively and create a maps file.'.grey);
console.log('gulp ' + 'mainStyles:pro'.cyan + ' ' + '# Compile compressed css except "pages" directory, apply autoprefixer to result.'.grey);
console.log('gulp ' + 'pageStyles:pro'.cyan + ' ' + '# Compile compressed css from "pages" directory exclusively, apply autoprefixer to result.'.grey);
console.log('');
console.log('Debugging tasks'.yellow);
console.log('gulp ' + 'sasslint'.cyan + ' ' + '# Check sass files looking for a bad code practises .'.grey);
console.log('gulp ' + 'jshint'.cyan + ' ' + '# Check js files looking for a wrong syntaxis.'.grey);
console.log('');
console.log('Watching tasks'.yellow);
console.log('gulp ' + 'watch'.cyan + ' ' + '# Run a defined tasks if any specified files are changed.'.grey);
console.log('gulp ' + 'watch -h'.cyan + ' yourhost'.green + ' ' + '# Modifies your host to use BrowserSync.'.grey);
console.log('gulp ' + 'browsersync'.cyan + ' ' + '# Synchronize browser and device in realtime and reload browser if any specified files are changed.'.grey);
console.log('');
console.log('Developing task'.yellow);
console.log('gulp ' + 'dev:watch'.cyan + ' ' + '# Run a development task list: imagemin, mainStyles:dev, pagesStyles:dev and watch.'.grey);
console.log('gulp ' + 'dev:browser'.cyan + ' ' + '# Run a development task list: imagemin, mainStyles:dev, pagesStyles:dev and browserSync.'.grey);
console.log('gulp ' + 'pro'.cyan + ' ' + '# Run a production task list: imagemin, mainStyles:pro, pagesStyles:pro'.grey);
console.log('');
console.log('Watching task example'.yellow);
console.log('gulp ' + 'watch -h'.cyan + ' localhost'.green + ' ' + '# To configure hosts as davinci.local.'.grey);
console.log('');
});
/************* CLEANING *****************/
// Clean css
gulp.task('clean:styles', function () {
return del([
distAssets.styles + '*.css',
distAssets.styles + 'maps/'
]).then(paths => {
console.log('Deleting css from:', distAssets.styles.magenta, '\n', paths.join('\n').magenta);
});
});
// Clean images
gulp.task('clean:images', function () {
return del([
distAssets.images + '*',
'!' + distAssets.images + '*.txt',
'!' + distAssets.images + '*.md'
]).then(paths => {
console.log('Deleting images from:', distAssets.images.magenta, '\n', paths.join('\n').magenta);
});
});
/************* COMPILING *****************/
// Minify images
gulp.task('imagemin', gulp.series(gulp.parallel('clean:images', () =>
gulp.src(srcAssets.images + '*')
.pipe(imagemin([
imagemin.gifsicle({
interlaced: true
}),
imagemin.mozjpeg({
progressive: true
}),
imagemin.optipng({
optimizationLevel: 5
}),
imagemin.svgo({
plugins: [
{
removeViewBox: true
},
{
cleanupIDs: false
}
]
})
], {
verbose: true
}))
.pipe(gulp.dest(distAssets.images))
)));
// Main styles to development
gulp.task('mainStyles:dev', function () {
return gulp.src([
'!' + srcAssets.styles + 'pages/**/*.s+(a|c)ss',
srcAssets.styles + '**/*.s+(a|c)ss'
])
.pipe(sourceMaps.init())
.pipe(sassGlob())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'expanded'
}).on('error', sass.logError))
.pipe(sourceMaps.write('maps'))
.pipe(gulp.dest(distAssets.styles))
.pipe(browserSync.stream());
});
// Pages styles to development
gulp.task('pagesStyles:dev', function () {
return gulp.src(srcAssets.styles + 'pages/**/*.s+(a|c)ss')
.pipe(sourceMaps.init())
.pipe(sassGlob())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'expanded'
}).on('error', sass.logError))
.pipe(sourceMaps.write('maps'))
.pipe(gulp.dest(distAssets.styles))
.pipe(browserSync.stream());
});
// Main styles to producction
gulp.task('mainStyles:pro', function () {
return gulp.src([
'!' + srcAssets.styles + 'pages/**/*.s+(a|c)ss',
srcAssets.styles + '**/*.s+(a|c)ss'
])
.pipe(sassGlob())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'compressed'
}).on('error', sass.logError))
.pipe(postCss([
autoprefixer({
browsers: ['> 1%', 'ie 8', 'last 2 versions']
})
]))
.pipe(gulp.dest(distAssets.styles));
});
// Pages styles to producction
gulp.task('pagesStyles:pro', function () {
return gulp.src(srcAssets.styles + 'pages/**/*.s+(a|c)ss')
.pipe(sassGlob())
.pipe(sass({
errLogToConsole: true,
outputStyle: 'compressed'
}).on('error', sass.logError))
.pipe(postCss([
autoprefixer({
browsers: ['> 1%', 'ie 8', 'last 2 versions']
})
]))
.pipe(gulp.dest(distAssets.styles));
});
/************* DEBUGGING *****************/
// Sass lint
gulp.task('sasslint', function () {
return gulp.src(srcAssets.styles + '**/*.s+(a|c)ss')
.pipe(sassLint({
options: {
configFile: 'da_vinci.sass-lint.yml'
}
}))
.pipe(sassLint.format())
.pipe(sassLint.failOnError());
});
// jsHint
gulp.task('jshint', function () {
return gulp.src([distAssets.js + '*.js'])
.pipe(jsHint())
.pipe(jsHint.reporter(jsHintStylish))
.pipe(browserSync.stream());
});
/************** DEMONS **********************/
// WATCH
gulp.task('watch', function () {
gulp.watch(srcAssets.styles + '**/*.s+(a|c)ss', gulp.series(gulp.parallel('mainStyles:dev', 'pagesStyles:dev')))
.on('change', function (path) {
console.log('');
console.log('-> File ' + path.magenta.bold + ' was ' + 'changed'.green + ', running tasks css...');
});
gulp.watch(distAssets.js + '**/*.js', gulp.series('jshint'))
.on('change', function (path) {
console.log('');
console.log('-> File ' + path + ' was ' + 'changed'.green + ', running tasks js...');
});
gulp.watch(srcAssets.images + '**/*', gulp.series('imagemin'))
.on('change', function (path) {
console.log('');
console.log('-> File ' + path + ' was ' + 'changed'.green + ', running tasks images...');
});
});
// Browser Sync
gulp.task('browsersync', function () {
var openPath = 'local';
if (process.h) {
path = process.h;
openPath = 'external';
console.log(path.green + ' configured as new hosts.'.yellow);
}
browserSync.init({
host: path,
open: openPath,
proxy: path
});
gulp.watch(srcAssets.styles + '**/*.s+(a|c)ss', gulp.series(gulp.parallel('mainStyles:dev', 'pagesStyles:dev')))
.on('change', function (path) {
console.log('');
console.log('-> File ' + path.magenta.bold + ' was ' + 'changed'.green + ', running tasks...');
browserSync.reload();
});
});
/************** TIME TO WORK ***********************/
// Development enviroment
gulp.task('dev:watch', gulp.series(gulp.parallel('imagemin', 'mainStyles:dev', 'pagesStyles:dev', 'watch')));
gulp.task('dev:browsersync', gulp.series(gulp.parallel('imagemin', 'mainStyles:dev', 'pagesStyles:dev', 'browsersync')));
// Production enviroment
gulp.task('pro', gulp.series(gulp.parallel('imagemin', 'mainStyles:pro', 'pagesStyles:pro')));