-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
145 lines (128 loc) · 3.06 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
/* eslint-env node */
const { src, dest, series, parallel, watch } = require("gulp");
const browserSync = require("browser-sync").create();
const del = require("del");
const htmlMinify = require("gulp-htmlmin");
const sourcemaps = require("gulp-sourcemaps");
const browserify = require("browserify");
const babelify = require("babelify");
const source = require("vinyl-source-stream");
const buffer = require("vinyl-buffer");
const sass = require("gulp-sass")(require("sass"));
const cssAutoPrefixer = require("gulp-autoprefixer");
const concat = require("gulp-concat");
const uglify = require("gulp-uglify");
// Options
const browserSyncOptions = {
open: false,
browser: false,
ui: false,
host: "0.0.0.0",
server: {
baseDir: "./dist",
port: 3000,
},
};
const htmlOptions = {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true
};
const browserifyOptions = {
entries: ["./src/scripts/main.js"],
debug: true,
transform: "babelify"
};
const babelOptions = {
babelrc: true
};
const cssOptions = {
outputStyle: "compressed",
sourceComments: false,
sourceMap: false,
};
// Tasks
function reload() {
return browserSync.reload({ stream: true });
}
function handleHtml() {
return src("src/**/*.html")
.pipe(htmlMinify(htmlOptions))
.pipe(dest("./dist"))
.pipe(reload());
}
function watchHtml() {
return watch("src/**/*.html", handleHtml);
}
function handleJs() {
return browserify(browserifyOptions)
.transform(babelify, babelOptions)
.bundle()
.pipe(source("script.min.js"))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(sourcemaps.write("./"))
.pipe(dest("./dist/scripts"))
.pipe(reload());
}
function watchJs() {
return watch("src/**/*.js", handleJs, {
delay: 1500,
awaitWriteFinish: true,
});
}
function handleAssets() {
return src(
[
"./src/image/**.*",
"./src/audio/**.*",
"./src/font/**.*",
"src/lang/**.*",
"src/assets/**.*",
],
{ base: "./src" }
)
.pipe(dest("./dist"))
.pipe(reload());
}
function watchAssets() {
return watch(
[
"src/image/**.*",
"src/audio/**.*",
"src/font/**.*",
"src/lang/**.*",
"src/assets/**.*",
],
handleAssets
);
}
function handleSCSS() {
return src("./src/styles/**.scss")
.pipe(sourcemaps.init())
.pipe(sass(cssOptions).on("error", sass.logError))
.pipe(cssAutoPrefixer())
.pipe(concat("style.min.css"))
.pipe(sourcemaps.write("./"))
.pipe(dest("./dist/styles"))
.pipe(reload());
}
function watchSCSS() {
return watch("./src/styles/**.scss", handleSCSS);
}
function clean() {
return del("dist");
}
function initialize() {
return browserSync.init(browserSyncOptions);
}
// Export tasks
module.exports.assets = handleAssets;
module.exports.html = handleHtml;
module.exports.js = handleJs;
module.exports.scss = handleSCSS;
module.exports.clean = clean;
module.exports.build = series(clean, handleAssets, handleSCSS, handleHtml, handleJs);
module.exports.dev = series(module.exports.build, parallel(watchAssets, watchSCSS, watchHtml, watchJs, initialize));
module.exports.default = module.exports.build;