This repository was archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
127 lines (116 loc) · 2.92 KB
/
Copy pathgulpfile.js
File metadata and controls
127 lines (116 loc) · 2.92 KB
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
const gulp = require('gulp');
const postcss = require('gulp-postcss');
// Compile SCSS to CSS
const sass = require('gulp-sass');
// Automatically add any browser prefixes to CSS
const autoprefixer = require('autoprefixer');
// Run a local server
const bs = require('browser-sync').create();
// For minifying CSS
const cleanCSS = require('gulp-clean-css');
// Babel for JS
const babel = require('gulp-babel');
// Minimize JS
const uglify = require('gulp-uglify');
// Minimize images
const imagemin = require('gulp-imagemin');
// Only deal with files that have changed since the last run
const changed = require('gulp-changed');
const size = require('gulp-size');
// *** FILE TASKS
// Port root directory files to dist folder (index.html & favicon package files)
function rootFiles() {
return gulp
.src('src/*.{html,ico,png,xml,svg,webmanifest}')
.pipe(changed('dist'))
.pipe(
size({
showFiles: true,
})
)
.pipe(gulp.dest('dist'))
.pipe(bs.stream());
}
// Build SCSS files to CSS files in dist folder
function styles() {
return (
gulp
.src('src/styles/*.scss')
.pipe(changed('dist/styles'))
// Compile SCSS to CSS
.pipe(sass())
.pipe(
postcss([
// Add browser prefixes
autoprefixer({}),
])
)
// minify CSS
.pipe(cleanCSS({}))
.pipe(
size({
showFiles: true,
})
)
// Send compiled CSS to dist folder
.pipe(gulp.dest('dist/styles'))
// Hot reloading for browser-sync
.pipe(bs.stream())
);
}
// Compiles JS files to dist folder
function js() {
return (
gulp
.src('src/js/*.js')
.pipe(changed('dist/js'))
.pipe(
babel({
presets: ['@babel/env'],
})
)
.pipe(uglify())
.pipe(
size({
showFiles: true,
})
)
.pipe(gulp.dest('dist/js'))
// Hot reloading for browser-sync
.pipe(bs.stream())
);
}
// Minify images and send to dist folder
function images() {
return gulp
.src('src/images/*.{png,gif,jpg,jpeg}')
.pipe(changed('dist/images'))
.pipe(imagemin())
.pipe(
size({
showFiles: true,
})
)
.pipe(gulp.dest('dist/images'));
}
// *** SERVER TASK
// Boot up browser-sync server and hot reload when files change
function serve() {
bs.init({
port: 8080,
server: {
baseDir: './dist',
},
});
// Watch for file changes
gulp.watch('src/*.{html,ico,png,xml,svg,webmanifest}').on('change', bs.reload);
gulp.watch('src/styles/*.scss', styles);
gulp.watch('src/js/*.js').on('change', bs.reload);
gulp.watch('src/images/*.{png,gif,jpg,jpeg').on('change', bs.reload);
}
// gulp.task('serve', ['root', 'styles', 'js', 'images'], function() {
// Development task
exports.dev = serve;
// Production build task
exports.build = gulp.parallel(rootFiles, styles, js, images)
// gulp.task('default', ['root', 'styles', 'js', 'images']);