-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
126 lines (118 loc) Β· 4.04 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
// Load plugins
var gulp = require("gulp"),
sass = require("gulp-sass"),
autoprefixer = require("gulp-autoprefixer"),
cleanCSS = require("gulp-clean-css"),
concat = require("gulp-concat"),
uglify = require("gulp-uglify"),
imagemin = require("gulp-imagemin"),
plumber = require("gulp-plumber"),
notify = require("gulp-notify"),
rename = require("gulp-rename"),
sourcemaps = require("gulp-sourcemaps"),
browserSync = require("browser-sync"),
reload = browserSync.reload;
// globs are basically pattern matches for files. We store them in an object
// because we use them both for gulp.src() as well as gulp.watch()
// if a change to the glob is needed, we simply edit it here rather than in 2 places
var globs = {
scripts: ["source/js/*.js"],
styles: ["source/css/**/*.scss"],
html: ["source/**/*.html"],
images: ["source/images/**/*"]
};
// HTML
gulp.task("html", function() {
return gulp
.src(globs.html)
.pipe(rename({ dirname: "" })) // flatten directory structure
.pipe(gulp.dest("_build"))
.pipe(reload({ stream: true }));
});
// Styles
gulp.task("styles", function() {
gulp
.src(globs.styles)
.pipe(sourcemaps.init())
.pipe(
plumber({
errorHandler: notify.onError("Error: <%= error.message %>")
})
)
.pipe(sass())
.pipe(
autoprefixer({
browsers: ["last 3 versions"]
})
)
.pipe(
cleanCSS({
level: {
2: {
mergeAdjacentRules: true, // controls adjacent rules merging; defaults to true
mergeIntoShorthands: true, // controls merging properties into shorthands; defaults to true
mergeMedia: true, // controls `@media` merging; defaults to true
mergeNonAdjacentRules: true, // controls non-adjacent rule merging; defaults to true
mergeSemantically: false, // controls semantic merging; defaults to false
overrideProperties: true, // controls property overriding based on understandability; defaults to true
removeEmpty: true, // controls removing empty rules and nested blocks; defaults to `true`
reduceNonAdjacentRules: true, // controls non-adjacent rule reducing; defaults to true
removeDuplicateFontRules: true, // controls duplicate `@font-face` removing; defaults to true
removeDuplicateMediaBlocks: true, // controls duplicate `@media` removing; defaults to true
removeDuplicateRules: true, // controls duplicate rules removing; defaults to true
removeUnusedAtRules: false, // controls unused at rule removing; defaults to false (available since 4.1.0)
restructureRules: false, // controls rule restructuring; defaults to false
skipProperties: [] // controls which properties won't be optimized, defaults to `[]` which means all will be optimized (since 4.1.0)
}
}
})
)
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("_build/css"))
.pipe(reload({ stream: true }));
});
// Scripts
gulp.task("scripts", function() {
return gulp
.src(globs.scripts)
.pipe(sourcemaps.init())
.pipe(
plumber({
errorHandler: notify.onError("Error: <%= error.message %>")
})
)
.pipe(concat("main.js"))
.pipe(uglify())
.pipe(gulp.dest("_build/scripts"));
// .pipe(reload({ stream: true }));
});
// Images
gulp.task("images", function() {
return gulp
.src(globs.images)
.pipe(
imagemin({
progressive: true,
svgoPlugins: [{ removeViewBox: false }]
})
)
.pipe(gulp.dest("_build/images"))
.pipe(reload({ stream: true }));
});
// Browser Sync
gulp.task("browser-sync", function() {
browserSync({
server: {
baseDir: "_build"
}
});
});
// Watch!
gulp.task("watch", ["browser-sync"], function() {
gulp.watch(globs.styles, ["styles"]);
gulp.watch(globs.scripts, ["scripts"]);
gulp.watch(globs.images, ["images"]);
gulp.watch(globs.html, ["html"]);
});
// the default tasks runs when you simply type 'gulp'
gulp.task("default", ["styles", "scripts", "images", "html", "watch"]);