-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
127 lines (101 loc) · 3.46 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
//Define all plugins
var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var cache = require('gulp-cached');
var imagemin = require('gulp-imagemin');
var filter = require('gulp-filter');
var autoprefixer = require('gulp-autoprefixer');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var notify = require("gulp-notify");
var plumber = require('gulp-plumber');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
//Define file paths
var js_src = 'js/*.js';
var js_dest = 'js/min';
var sass_src = 'css/*.scss';
var sass_dest = 'css';
var img_src = 'images/*';
var img_dest = 'images/optimised';
//Sass
gulp.task('sass', function () {
gulp.src(sass_src)
//Notify on error
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
//Source maps init
.pipe(sourcemaps.init())
//Process sass
.pipe(sass({
outputStyle: 'compressed'
}))
//Autoprefix that css!
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
//Write source map
.pipe(sourcemaps.write('./'))
//Output the compiled sass to this directory
.pipe(gulp.dest(sass_dest))
//Filtering stream to only css files
.pipe(filter('**/*.css'))
//Inject changes via browsersync
.pipe(reload({stream: true}))
//Notify on successful compile
.pipe(notify("Compiled: <%= file.relative %>"));
});
//Scripts
gulp.task('scripts', function () {
gulp.src(js_src)
//Notify on error
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
//Cache files to avoid processing files that haven't changed
.pipe(cache('scripts'))
//Add .min suffix
.pipe(rename({ suffix: '.min' }))
//Minify
.pipe(uglify())
//Output the processed js to this directory
.pipe(gulp.dest(js_dest))
//Inject changes via browsersync
.pipe(reload({stream: true}))
//Notify on successful compile
.pipe(notify("Minified: <%= file.relative %>"));
});
//Images
gulp.task('images', function () {
return gulp.src(img_src)
//Notify on error
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
//Cache files to avoid processing files that haven't changed
.pipe(cache('images'))
//Optimise images
.pipe(imagemin({
progressive: true
}))
//Output the optimised images to this directory
.pipe(gulp.dest(img_dest))
//Inject changes via browsersync
.pipe(reload({stream: true}))
//Notify on successful compile
.pipe(notify("Optimised: <%= file.relative %>"));
});
//Browsersync
gulp.task('browsersync', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
//Watch for changes
gulp.watch(sass_src, ['sass']);
gulp.watch(js_src, ['scripts']);
gulp.watch('images/**/*', ['images']);
gulp.watch("**/*.html").on("change", reload);
});
//Default
gulp.task('default', ['browsersync'], function() {
gulp.start('sass', 'scripts');
});