-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
70 lines (62 loc) · 2.45 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
const gulp = require('gulp'), // gulp package
sass = require('gulp-sass'), // sass compiler
cssmin = require('gulp-clean-css'), // minify css
imageResize = require('gulp-image-resize'), // resize images
path = require('path'), // node.js module that serve path
rename = require("gulp-rename"), //rename module
autoprefixer = require('gulp-autoprefixer'); //prefixes for cross-browser compatibility
var sassOptions = { //sass compiler options
errLogToConsole: true,
outputStyle: 'expanded'
},
rootPath = './'; //project root path
gulp.task('sass', function() {
var src = path.join(rootPath, '**', 'static/', '**', 'scss');
console.log(src);
return gulp.src(path.join(src, '*.scss'), {base: '.'})
.pipe(sass(sassOptions).on('error', sass.logError)) // sass to css compilation
.pipe(autoprefixer()) // prefixes for cross-browsers compatibility
.pipe(cssmin()) // minimize css file size
.pipe(rename(function (path) { // here is the place where rename of scss folder in source path to css and send it to destination path
path.dirname = path.dirname.replace('/scss', '/css');
path.extname = '.css';
}))
.pipe(gulp.dest('.')); //write compilation result
});
const img_sizes = {
xs: 200,
sm: 400,
md: 800,
lg: 1600,
xlg: 2000
};
var imgResizeTasks = [];
for (var size in img_sizes) {
if (!img_sizes.hasOwnProperty(size)) {
continue
}
var taskName = 'resize_' + size;
function createTask(taskName, img_size, width) {
gulp.task(taskName, function() {
var src = path.join(rootPath, './static', '**', 'img/src');
return gulp.src(path.join(src, '*.jpg'), {base: '.'})
.pipe(imageResize({
width: width,
crop: false
}))
.pipe(rename(function (path) {
path.dirname = path.dirname.replace('/src', '/dist/' + img_size);
path.extname = '.jpg';
}))
.pipe(gulp.dest('.'))
});
}
createTask(taskName, size, img_sizes[size]);
imgResizeTasks.push(taskName);
}
gulp.task('watch', function () { // watch for changes in all scss files
var src = path.join(rootPath, '**', '*.scss');
gulp.watch(src, ['sass']); //recompile all files on scss change
});
gulp.task('imgs', imgResizeTasks);
gulp.task('default', ['sass', 'imgs', 'watch']);