-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
142 lines (123 loc) · 3.75 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
// Requirement packages
var gulp = require('gulp'),
sass = require('gulp-sass'),
browserSync = require('browser-sync').create(),
autoPrefixer = require('gulp-autoprefixer'),
MinifyCss = require('gulp-clean-css'),
htmlMinify = require('gulp-htmlmin'),
sourceMap = require('gulp-sourcemaps'),
rename = require('gulp-rename'),
plumber = require('gulp-plumber'),
uglifyJS = require('gulp-uglify'),
imagemin = require('gulp-imagemin');
var config = {
imgSRC: './src/assets/images/*',
imgDEST: './dist/assets/images',
sassSRC: './src/assets/sass/*.sass',
sassDEST: './dist/assets/sass',
jsSRC: './src/assets/javascripts/*.js',
jsDEST: './src/assets/javascripts',
jsDEST2: './dist/assets/javascripts',
cssSRC: './src/assets/stylesheets/*.css',
cssDEST: './src/assets/stylesheets',
cssDEST2: './dist/assets/stylesheets',
htmlSRC: './src/*.html',
htmlDEST: './dist',
}
// Performing taks like compiling sass, auto prefixing, sourcemap writting, minifying css, minifying images, reloading broswer tab...
gulp.task('performActions', function() {
gulp.src(config.sassSRC)
.pipe(plumber())
.pipe(sourceMap.init())
.pipe(sass().on('error', sass.logError))
.pipe(autoPrefixer({
browsers: ['last 4 versions'],
cascade: false
}))
.pipe(gulp.dest(config.cssDEST))
.pipe(gulp.dest(config.cssDEST2))
.pipe(MinifyCss({
compatibility: 'ie8'
}))
.pipe(sourceMap.write())
.pipe(rename({
suffix: '.min',
extname: ".css"
}))
.pipe(gulp.dest(config.cssDEST))
.pipe(gulp.dest(config.cssDEST2))
gulp.src(config.htmlSRC)
.pipe(plumber())
.pipe(htmlMinify({
collapseWhitespace: true
}))
.pipe(gulp.dest(config.htmlDEST))
gulp.src(config.imgSRC)
.pipe(imagemin([
imagemin.gifsicle({
interlaced: true
}),
imagemin.jpegtran({
progressive: true
}),
imagemin.optipng({
optimizationLevel: 5
}),
imagemin.svgo({
plugins: [{
removeViewBox: true
},
{
cleanupIDs: false
}
]
})
]))
.pipe(gulp.dest(config.imgDEST))
gulp.src(config.sassSRC)
.pipe(plumber())
.pipe(gulp.dest(config.sassDEST))
.pipe(browserSync.stream());
});
// Minifying JS
gulp.task('uglify', function() {
gulp.src([config.jsSRC, '!src/assets/javascripts/*.min.js'])
.pipe(plumber())
.pipe(gulp.dest(config.jsDEST2, {
overwrite: true
}))
.pipe(uglifyJS())
.pipe(rename(function(path) {
path.extname = ".min.js";
}))
.pipe(gulp.dest(config.jsDEST, {
overwrite: true
}))
.pipe(gulp.dest(config.jsDEST2, {
overwrite: true
}));
});
// Browser Initialize.
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "./dist/",
files: [config.cssSRC, config.jsSRC]
},
// Reload the same opened tab.
open: "local"
})
});
// Task Watch.
gulp.task('watch', ['browser-sync', 'performActions'], function() {
gulp.watch([config.sassSRC], ['performActions']);
gulp.watch(config.jsSRC, ['js-watch']);
gulp.watch("./src/*.html").on('change', browserSync.reload);
});
// Watch JS files.
gulp.task('js-watch', ['uglify'], function(done) {
browserSync.reload();
done();
});
// Default task.
gulp.task('default', ['watch']);