-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
140 lines (111 loc) · 3.38 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
/*
Require Gulp modules
*/
const gulp = require("gulp");
const sass = require("gulp-sass");
const autoprefixer = require("gulp-autoprefixer");
const concat = require("gulp-concat-css");
const cleanCSS = require("gulp-clean-css");
const imagemin = require("gulp-imagemin");
const sourcemaps = require("gulp-sourcemaps");
const browserSync = require("browser-sync").create();
/*
Set resource Input directory and Output Directory
Input - resources/scss and all sub folders
Output - resources/stylesheets
*/
const resourceInput = ['resources/scss/**/*.scss', 'resources/scss/**/*_.scss'];
const resourceOutput = 'resources/stylesheets';
/*
Set the minified output folder - assets/stylesheets/bundle.min.scss
Output - assets/stylesheets
*/
const minifiedOutput = 'assets/stylesheets';
/*
Set the images input folder
*/
const imageInput = 'resources/images/*';
/*
Set the minified images output folder
*/
const imageOutput = 'assets/images';
/*
Index.html file location
*/
const htmlLocation = './';
/*
Set the sassOptions
errlogToConsole - Log errors the the console
outputStyle - Set to expanded in case the user wants to edit the css file directly
*/
const sassOptions = {
errLogToConsole: true,
outputStyle: "expanded"
};
/*
Set the autoprefixer options
browsers - Uses the last 2 versions of Firefox ESR with a greater than 5% accepting rate
*/
const autoprefixerOptions = {
browsers: ['last 2 version'],
cascade: false,
grid: true
};
/*
Gulp SCSS task
Grabs scss files from resourcesInput folder, pipes scss it through the autoprefixer then sends it to
the destination folder
*/
gulp.task('scss', function () {
return gulp
.src(resourceInput)
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(autoprefixer(autoprefixerOptions))
.pipe(gulp.dest(resourceOutput));
});
/*
Gulp concat task
Grabs all of the css files in the resource output folder, combines them, and then minifys them and sends them
to the production assets folder with source maps
*/
gulp.task('concat', ['scss'], function() {
return gulp
.src(resourceOutput + '/*.css')
.pipe(concat('bundle.min.css'))
.pipe(sourcemaps.init())
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(minifiedOutput))
.pipe(browserSync.stream());
});
/*
Gulp Imagemin
Minifies all of the images in the resources/images folder and pipes them to the assets/images folder
*/
gulp.task('images', function() {
return gulp
.src(imageInput)
.pipe(imagemin({ optimizationLevel: 7 }))
.pipe(gulp.dest(imageOutput));
});
/*
Gulp watch
Gulp file watcher task to run images, scss, concat tasks, and auto-reloading with browser-sync.
*/
gulp.task('watch', function() {
// Browser Sync Init
browserSync.init({
server: htmlLocation
});
// Run Images First
gulp.watch(imageInput, ['images']);
// Watches SCSS changes, runs concat which compiles scss first then creates the bundle.min.css file, then reloads browser
gulp.watch([resourceInput], ['concat']).on('finish', browserSync.reload);
// Watches for HTML file changes, reloads browser
gulp.watch(["*.html"]).on('change', browserSync.reload);
});
/*
Gulp Default
Default task that is ran on gulp start
*/
gulp.task('default', ['watch']);