-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
138 lines (125 loc) · 4.15 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
// Get things set up
// -------------------------------------------------------------------
// Include Gulp
var gulp = require("gulp"),
// HTML plugins
fileinclude = require("gulp-file-include"),
htmlmin = require("gulp-htmlmin"),
// CSS plugins
sass = require("gulp-sass"),
autoprefixer = require("gulp-autoprefixer"),
cssmin = require("gulp-clean-css"),
rename = require("gulp-rename"),
// JS plugins
concat = require("gulp-concat"),
uglify = require("gulp-uglify"),
// Image plugin
imagemin = require("gulp-imagemin"),
// General plugins
gutil = require("gulp-util"),
plumber = require("gulp-plumber"),
size = require("gulp-size"),
watch = require("gulp-watch"),
browserSync = require("browser-sync"),
reload = browserSync.reload;
// Tasks
// -------------------------------------------------------------------
// Start server
gulp.task("browser-sync", function() {
browserSync({
server: {
baseDir: "dist"
}
});
});
// Notify on error with a beep
var onError = function(error) {
console.log(gutil.colors.red(error.message));
// https://github.com/floatdrop/gulp-plumber/issues/17
this.emit("end");
gutil.beep();
};
// HTML task
gulp.task("html", function() {
return gulp.src("src/html/*.html")
// Prevent gulp.watch from crashing
.pipe(plumber(onError))
// Set up HTML templating
.pipe(fileinclude({
prefix: "@@",
basepath: "src/html"
}))
// Clean up HTML a little
.pipe(htmlmin({
removeCommentsFromCDATA: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
caseSensitive: true,
minifyCSS: true
}))
// Where to store the finalized HTML
.pipe(gulp.dest("dist"));
});
// CSS task
gulp.task("css", function() {
return gulp.src("src/scss/main.scss")
// Prevent gulp.watch from crashing
.pipe(plumber(onError))
// Compile Sass
.pipe(sass({ style: "compressed", noCache: true }))
// parse CSS and add vendor-prefixed CSS properties
.pipe(autoprefixer({
browsers: ["last 2 versions"]
}))
// Minify CSS
.pipe(cssmin())
// Rename the file
.pipe(rename("production.css"))
// Show sizes of minified CSS files
.pipe(size({ showFiles: true }))
// Where to store the finalized CSS
.pipe(gulp.dest("dist/css"));
});
// JS task
gulp.task("js", function() {
return gulp.src("src/js/**/*")
// Prevent gulp.watch from crashing
.pipe(plumber(onError))
// Concatenate all JS files into one
.pipe(concat("production.js"))
// Where to store the finalized JS
.pipe(gulp.dest("dist/js"));
});
// Image task
gulp.task("images", function() {
return gulp.src("src/img/**/*.+(png|jpeg|jpg|gif|svg)")
// Prevent gulp.watch from crashing
.pipe(plumber(onError))
// Minify the images
.pipe(imagemin())
// Where to store the finalized images
.pipe(gulp.dest("dist/img"));
});
// Use default task to launch BrowserSync and watch all files
gulp.task("default", ["browser-sync"], function () {
// All browsers reload after tasks are complete
// Watch HTML files
watch("src/html/**/*", function () {
gulp.start("html", reload);
});
// Watch Sass files
watch("src/scss/**/*", function () {
gulp.start('css', reload);
});
// Watch JS files
watch("src/js/**/*", function () {
gulp.start("js", reload);
});
// Watch image files
watch("src/img/**/*.+(png|jpeg|jpg|gif|svg)", function () {
gulp.start("images", reload);
});
});
gulp.task('build', ['html', 'css', 'js', 'images']);