-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
189 lines (172 loc) · 4.63 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
const { src, dest, watch, series, parallel } = require('gulp');
const browserSync = require('browser-sync').create();
const panini = require('panini');
const babel = require('gulp-babel');
const del = require('del');
const sourcemaps = require('gulp-sourcemaps');
const removeCode = require('gulp-remove-code');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const concat = require('gulp-concat');
const cssmin = require('gulp-cssmin');
const htmlreplace = require('gulp-html-replace');
const sass = require('gulp-sass')(require('sass')); // Updated to use Dart Sass
/* ---------------------------------------------------
Dev tasks
--------------------------------------------------- */
// compile SCSS into CSS
function compileSCSS() {
console.log('----COMPILING SCSS!----');
return src('src/assets/scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'expanded',
}).on('error', sass.logError))
.pipe(sourcemaps.write('.'))
.pipe(dest('dist/assets/css'))
.pipe(browserSync.stream());
}
// compile HTML files and partials using Panini
function compileHTML() {
console.log('----COMPILING HTML WITH PANINI!----');
panini.refresh();
return src('src/pages/**/*.html')
.pipe(panini({
root: 'src/pages/',
layouts: 'src/layouts/',
partials: 'src/partials/'
}))
.pipe(dest('dist'))
.pipe(browserSync.stream());
}
// compile JS
function compileJS() {
console.log('----COMPILING JS!----');
return src('src/assets/js/**/*.js')
.pipe(babel())
.pipe(sourcemaps.write('./'))
.pipe(dest('dist/assets/js'))
.pipe(browserSync.stream());
}
// resets Panini's cache of layouts and partials
function resetPages(done) {
console.log('----CLEARING PANINI CACHE!----');
panini.refresh();
done();
}
// browserSync
function browserSyncInit(done) {
console.log('----BROWSER SYNC!----');
browserSync.init({
notify: false,
server: './dist'
});
done();
}
// copy and minify images to dist
function copyImages() {
console.log('----COMPILING IMAGES!----');
return src('src/assets/images/**/*.+(png|jpg|jpeg|gif|svg)')
.pipe(dest('dist/assets/images/'))
.pipe(browserSync.stream());
}
// places font files in dist
function copyFont() {
console.log('----COPYING FONTS INTO DIST FOLDER!----');
return src([
'src/assets/fonts/*',
])
.pipe(dest('dist/assets/fonts'))
.pipe(browserSync.stream());
}
// deletes dist folder
function cleanDist(done) {
console.log('----REMOVING OLD FILES FROM DIST!----');
del.sync('dist');
done();
}
// watch files
function watchFiles() {
watch('src/**/*.html', compileHTML);
watch(['src/assets/scss/**/*.scss', 'src/assets/scss/*.scss'], compileSCSS);
watch(['src/assets/js/**/*.js', 'src/assets/js/*.js'], compileJS);
watch('src/assets/images/**/*', copyImages);
}
/* ---------------------------------------------------
Build tasks
--------------------------------------------------- */
// concatenate all js files
function concatScripts() {
console.log('----CONCATINATING SCRIPTS!----');
return src([
'src/assets/js/vendor/bootstrap.bundle.js',
'src/assets/js/vendor/emergence.js',
'src/assets/js/vendor/swiper-bundle.js',
'src/assets/js/*'
])
.pipe(sourcemaps.init())
.pipe(concat('main.js'))
.pipe(sourcemaps.write('./'))
.pipe(dest('dist/assets/js'))
.pipe(browserSync.stream());
}
// minify scripts
function minifyScripts() {
console.log('----MINIFYING SCRIPTS!----');
return src('dist/assets/js/main.js')
.pipe(removeCode({
production: true
}))
.pipe(uglify().on('error', console.error))
.pipe(rename('main.min.js'))
.pipe(dest('dist/assets/js'));
}
// minify css
function minifyCss() {
console.log('----MINIFYING CSS!----');
return src([
'dist/assets/css/main.css'
])
.pipe(sourcemaps.init())
.pipe(concat('main.css'))
.pipe(cssmin())
.pipe(rename('main.min.css'))
.pipe(dest('dist/assets/css'));
}
// change to minified versions of js and css
function renameSources() {
console.log('----RENAMING SOURCES!----');
return src('dist/*.html')
.pipe(htmlreplace({
'js': 'assets/js/main.min.js',
'css': 'assets/css/main.min.css'
}))
.pipe(dest('dist/'));
}
// TASK: $ gulp dev
exports.dev = series(
cleanDist,
copyFont,
copyImages,
resetPages,
compileHTML,
compileJS,
compileSCSS,
browserSyncInit,
watchFiles
);
// TASK: $ gulp build
exports.build = series(
cleanDist,
compileSCSS,
copyFont,
copyImages,
resetPages,
compileHTML,
concatScripts,
minifyScripts,
minifyCss,
renameSources,
browserSyncInit
);