-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
139 lines (126 loc) · 3.77 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
const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const babel = require("gulp-babel");
const sourcemaps = require("gulp-sourcemaps");
const concat = require("gulp-concat");
const terser = require("gulp-terser");
const rename = require("gulp-rename");
const del = require("del");
const browserSync = require("browser-sync").create();
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
const replace = require("gulp-replace");
const imagemin = require("gulp-imagemin");
const plumber = require("gulp-plumber");
const paths = {
html: {
src: "./app/**/*.html",
dest: "./build",
},
styles: {
src: "./app/scss/**/*.scss",
dest: "./build/assets/css",
},
scripts: {
src: "./app/js/**/*.js",
dest: "./build/assets/js",
},
images: {
src: "./app/images/**/*",
dest: "./build/assets/images",
},
favicon: {
src: "./app/favicon.ico",
dest: "./build",
},
};
const clean = () => del(["./build"]);
// Cache busting to prevent browser caching issues
const curTime = new Date().getTime();
const cacheBust = () =>
gulp
.src(paths.html.src)
.pipe(plumber())
.pipe(replace(/cb=\d+/g, "cb=" + curTime))
.pipe(gulp.dest(paths.html.dest));
// Copies all html files
const html = () =>
gulp.src(paths.html.src).pipe(plumber()).pipe(gulp.dest(paths.html.dest));
// Convert scss to css, auto-prefix, minify and rename into styles.min.css
const styles = () =>
gulp
.src(paths.styles.src)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(
sass({
includePaths: require("scss-resets").includePaths,
}).on("error", sass.logError)
)
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(
rename({
basename: "styles",
suffix: ".min",
})
)
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(paths.styles.dest))
.pipe(browserSync.stream());
// Minify all javascript files and concat them into a single app.min.js
const scripts = () =>
gulp
.src(paths.scripts.src)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ["@babel/preset-env"],
})
)
.pipe(terser())
.pipe(concat("app.min.js"))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(paths.scripts.dest));
// Copy and minify images
const images = () =>
gulp
.src(paths.images.src)
.pipe(plumber())
.pipe(imagemin())
.pipe(gulp.dest(paths.images.dest));
// Copy the favicon
const favicon = () =>
gulp
.src(paths.favicon.src)
.pipe(plumber())
.pipe(gulp.dest(paths.favicon.dest));
// Watches all .scss, .js and .html changes and executes the corresponding task
const watchFiles = () => {
browserSync.init({
server: {
baseDir: "./build",
},
notify: false,
});
gulp.watch(paths.styles.src, styles);
gulp.watch(paths.favicon.src, favicon).on("change", browserSync.reload);
gulp.watch(paths.scripts.src, scripts).on("change", browserSync.reload);
gulp.watch(paths.images.src, images).on("change", browserSync.reload);
gulp.watch(paths.html.src, html).on("change", browserSync.reload);
};
const build = gulp.series(
clean,
gulp.parallel(styles, scripts, images, favicon),
cacheBust
);
const watch = gulp.series(build, watchFiles);
exports.clean = clean;
exports.styles = styles;
exports.scripts = scripts;
exports.images = images;
exports.favicon = favicon;
exports.watch = watch;
exports.build = build;
exports.default = build;