-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
181 lines (162 loc) · 5.16 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
const gulp = require('gulp');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const rename = require("gulp-rename");
const cleanCSS = require('gulp-clean-css');
const del = require('del');
const browsersync = require('browser-sync').create();
const uglify = require('gulp-uglify');
const wait = require('gulp-wait');
const critical = require('critical').stream;
const useref = require('gulp-useref');
const gulpif = require('gulp-if');
// Initialize
// =============================================================================
// Clean dist
function cleanDist() {
return del(['dist/**', '!dist']);
}
// Clean vendor (src)
function cleanVendor() {
return del(['src/vendor/**', '!src/vendor']);
}
// Populate vendor (src)
function populateVendor() {
return gulp.src([
'node_modules/materialize-css/dist/js/materialize.min.js',
'node_modules/materialize-css/sass/components/**/*',
'node_modules/jquery/dist/jquery.min.js',
'node_modules/echarts/dist/echarts.min.js',
'node_modules/@fortawesome/fontawesome-free/css/all.min.css',
'node_modules/@fortawesome/fontawesome-free/webfonts/**/*',
'node_modules/simplebar/dist/simplebar.min.js',
'node_modules/simplebar/dist/simplebar.min.css',
'node_modules/aos/dist/aos.js',
'node_modules/aos/dist/aos.css',
'node_modules/headroom.js/dist/headroom.min.js'
], {
base: 'node_modules/'
})
.pipe(gulp.dest('src/vendor/'));
}
// Copy images to dist
function copyImages() {
return gulp.src('src/images/**/*')
.pipe(gulp.dest('dist/images'));
}
// Copy vendor to dist
function copyVendor() {
return gulp.src('src/vendor/**/*')
.pipe(gulp.dest('dist/vendor'));
}
// Copy Html to dist
function copyHtml() {
return gulp.src('src/*.html')
.pipe(gulp.dest('dist'));
}
// Development
// =============================================================================
// Static server (src)
function browserSyncSrc(cb) {
browsersync.init({
server: {
baseDir: "src"
},
open: "external"
});
cb();
}
// Browser reload
function browserReload(cb) {
browsersync.reload();
cb(); // Signal completion
}
// Compile Sass files
function compileSass() {
return gulp.src('src/sass/developerportfolio.scss')
.pipe(sass().on('error', sass.logError)) // Compile to CSS
.pipe(gulp.dest('src/css/')) // Save to src
.pipe(browsersync.stream()); // Inject changes without refreshing the page.
}
// Watch scss/js/html/ files
function watchFiles() {
gulp.watch("src/sass/**/*.scss", gulp.series(compileSass));
gulp.watch(["src/js/**/*.js"], gulp.series(browserReload));
gulp.watch("src/*.html", gulp.series(browserReload));
}
// Production
// =============================================================================
// CSS optimization
function css() {
return gulp.src('src/css/*.css')
.pipe(autoprefixer({
cascade: false
})) // Add vendor prefixes
.pipe(cleanCSS({
compatibility: 'ie8'
})) // Minify CSS
.pipe(gulp.dest('dist/css/'));
}
// JS optimization
function js() {
return gulp.src('src/js/*.js')
.pipe(uglify()) // Minify JS
.pipe(gulp.dest('dist/js/'));
}
// Critical CSS
function criticalCSS() {
return gulp.src('dist/*.html')
.pipe(
critical({
inline: true,
base: 'dist/',
// ignore: ['@font-face'],
dimensions: [{
// 9:16 (Mobile)
height: 1022,
width: 575,
},
{
// 9:16 (Tablet)
height: 1364,
width: 767,
},
{
// 9:16 (Tablet)
height: 1762,
width: 991,
},
{
// 9:16 (Tablet)
height: 2132,
width: 1199,
},
{
// 9:16 (Tablet)
height: 2132,
width: 1199,
},
{
// 3:2 (Desktop)
height: 800,
width: 1200,
},
]
})
)
.on('error', function (err) {
document(error);
})
.pipe(gulp.dest('dist/'));
}
// Tasks
// =============================================================================
// Define tasks
const init = gulp.series(cleanDist, cleanVendor, populateVendor, copyImages, copyVendor, copyHtml);
// const build = gulp.series(init, compileSass, css, js, criticalCSS);
const build = gulp.series(init, compileSass, css, js);
const watch = gulp.series(build, gulp.parallel(watchFiles, browserSyncSrc));
// Register public tasks
exports.init = init;
exports.build = build;
exports.watch = watch;