-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
294 lines (264 loc) · 7.18 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
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
(() => {
'use strict';
const cfg = require('./gulp-config.js'),
self = this,
gulp = require('gulp'),
del = require('del'),
path = require('path'),
notifier = require('node-notifier'),
gutil = require('gulp-util'),
runSequence = require('run-sequence'),
browserSync = require('browser-sync').create();
/**
* Require gulp task from file
* @param {string} taskName Task name
* @param {String} path Path to task file
* @param {Object} options Options for task
* @param [Array] dep Task dependencies
*/
function requireTask(taskName, path, options, dep) {
let settings = options || {},
dependencies = dep || [];
settings.taskName = taskName;
gulp.task(taskName, dependencies, function(callback) {
if(settings.checkProduction) {
settings.isProduction = this.seq.slice(-1)[0] === 'production';
}
let task = require(path + taskName + '.js').call(this, settings);
return task(callback);
});
}
/**
* Hint HTML
*/
requireTask(`${cfg.task.htmlHint}`, `./${cfg.folder.tasks}/`);
/**
* Hint JS
*/
requireTask(`${cfg.task.jsHint}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src
});
/**
* Build custom js
*/
requireTask(`${cfg.task.buildCustomJs}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src,
dest: cfg.folder.build,
mainJs: cfg.file.mainJs,
checkProduction: true,
showError: showError
});
/**
* Build js vendor (concatenate vendors array)
*/
requireTask(`${cfg.task.buildJsVendors}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src,
dest: cfg.folder.build,
vendorJs: cfg.file.vendorJs,
vendorJsMin: cfg.file.vendorJsMin
});
/**
* Build styles for application from SASS
*/
requireTask(`${cfg.task.buildSass}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src,
dest: cfg.folder.build,
mainScss: cfg.file.mainScss,
mainScssMin: cfg.file.mainScssMin,
versions: cfg.autoprefixer.versions,
self: self,
showError: showError
});
/**
* Build production styles for application from SASS
*/
requireTask(`${cfg.task.buildSassProd}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src,
dest: cfg.folder.build,
mainScss: cfg.file.mainScss,
mainScssMin: cfg.file.mainScssMin,
versions: cfg.autoprefixer.versions,
showError: showError
});
/**
* Build styles for vendor from SASS
*/
requireTask(`${cfg.task.buildStylesVendors}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src,
dest: cfg.folder.build,
vendorScss: cfg.file.vendorScss,
vendorScssMin: cfg.file.vendorScssMin,
showError: showError
});
/**
* Minify images
*/
requireTask(`${cfg.task.imageMin}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src,
dest: cfg.folder.build
});
/**
* Clean image build directory
*/
requireTask(`${cfg.task.imageClean}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.build
});
/**
* Clean build folder
*/
requireTask(`${cfg.task.cleanBuild}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.build
});
/**
* Clean production folder
*/
requireTask(`${cfg.task.cleanProd}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.prod
});
/**
* Copy folders to the build folder
*/
requireTask(`${cfg.task.copyFolders}`, `./${cfg.folder.tasks}/`, {
dest: cfg.folder.build,
foldersToCopy: cfg.foldersToCopy()
});
/**
* Copy folders to the production folder
*/
requireTask(`${cfg.task.copyFoldersProduction}`, `./${cfg.folder.tasks}/`, {
dest: cfg.folder.prod,
foldersToCopy: mergeArrays(
[
'./**/*'
],
cfg.ignoreProd()
)
});
/**
* Start browserSync server
*/
requireTask(`${cfg.task.browserSync}`, `./${cfg.folder.tasks}/`, {
mainHtml: cfg.file.mainHtml,
browserSync: browserSync
});
/**
* Watch for file changes
*/
requireTask(`${cfg.task.watch}`, `./${cfg.folder.tasks}/`, {
src: cfg.folder.src,
dest: cfg.folder.build,
imageExtensions: cfg.imageExtensions,
browserSync: browserSync,
deleteFile: deleteFile,
tasks: {
buildCustomJs: cfg.task.buildCustomJs,
buildSass: cfg.task.buildSass,
jsHint: cfg.task.jsHint,
htmlHint: cfg.task.htmlHint,
imageMin: cfg.task.imageMin
}
});
/**
* Default Gulp task
*/
gulp.task('default', (callback) => {
runSequence(
cfg.task.cleanBuild,
[
cfg.task.buildCustomJs,
cfg.task.buildJsVendors,
cfg.task.buildSass,
cfg.task.buildStylesVendors,
cfg.task.htmlHint,
cfg.task.jsHint,
cfg.task.imageMin
],
cfg.task.copyFolders,
[
cfg.task.browserSync,
cfg.task.watch
]
);
}
);
/**
* Dev Gulp task without usage of browserSync
*/
gulp.task('dev', (callback) => {
runSequence(
cfg.task.cleanBuild,
[
cfg.task.buildCustomJs,
cfg.task.buildJsVendors,
cfg.task.buildSass,
cfg.task.buildStylesVendors,
cfg.task.htmlHint,
cfg.task.jsHint,
cfg.task.imageMin
],
cfg.task.copyFolders,
cfg.task.watch
);
}
);
/**
* Creating production folder without unnecessary files
*/
gulp.task('production', (callback) => {
runSequence(
[
cfg.task.cleanProd,
cfg.task.cleanBuild
],
[
cfg.task.buildCustomJs,
cfg.task.buildJsVendors,
cfg.task.buildSassProd,
cfg.task.buildStylesVendors,
cfg.task.htmlHint,
cfg.task.jsHint,
cfg.task.imageMin
],
cfg.task.copyFolders,
cfg.task.copyFoldersProduction
);
}
);
/**
* Merge arrays
* @param {Array} event Event object
* @param {Array} src Name of the source folder
*/
function mergeArrays(array1, array2) {
return array1.concat(array2);
}
/**
* Remove image(s) from build folder if corresponding
* images were deleted from source folder
* @param {Object} event Event object
* @param {String} src Name of the source folder
* @param {String} dest Name of the destination folder
*/
function deleteFile(file, src, dest) {
let fileName = file.history.toString().split('/').pop();
let fileEventWord = file.event == 'unlink' ? 'deleted' : file.event;
let filePathFromSrc = path.relative(path.resolve(src), file.path);
let destFilePath = path.resolve(dest, filePathFromSrc);
try {
del.sync(destFilePath);
console.log(` \u{1b}[32m${fileEventWord}: ${fileName}\u{1b}[0m`);
} catch (error) {
console.log(` \u{1b}[31mFile has already deleted\u{1b}[0m`);
}
}
/**
* Show error in console
* @param {String} preffix Title of the error
* @param {String} err Error message
*/
function showError(preffix, err) {
gutil.log(gutil.colors.white.bgRed(' ' + preffix + ' '), gutil.colors.white.bgBlue(' ' + err.message + ' '));
notifier.notify({ title: preffix, message: err.message });
this.emit('end');
}
})();