-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
264 lines (244 loc) · 6.32 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
var gulp = require("gulp");
var sass = require("gulp-sass");
var elm = require("gulp-elm");
var uglify = require("gulp-uglify");
var Vinyl = require("vinyl");
var inject = require("gulp-inject");
var concat = require("gulp-concat");
var hash = require("gulp-hash-filename");
var open = require('open');
var cleanCss = require("gulp-clean-css");
var fs = require("fs");
var path = require('path');
var log = require("./src/utils/log");
var connect = require("gulp-connect");
var templates = require('./src/utils/templates');
var paths = {
src: "src/**/*",
srcHTML: "src/**/*.html",
srcCSS: "src/**/*.css",
srcSCSS: "src/**/*.scss",
mainElm: "src/Main.elm",
envElm: "env/**/*.elm",
srcElm: "src/**/*.elm", tmp: "tmp",
pubIndex: "public/index.html",
tmpCSS: "**/*.css",
tmpJS: "**/*.js", dist: "dist",
assets: "assets/**/*",
};
function string_src(filename, string) {
var src = require("stream").Readable({ objectMode: true });
src._read = function() {
this.push(new Vinyl({ path: filename, contents: Buffer.from(string) }));
this.push(null);
};
return src;
}
function assets(cb, dest) {
gulp.src(paths.assets)
.pipe(gulp.dest("./assets", {cwd: dest}))
.on("end", function() { cb(); });
}
function scssDebug(cb, tmp) {
gulp.src(paths.srcSCSS)
.pipe(sass().on("error", sass.logError))
.pipe(gulp.dest(".", {cwd: tmp}))
.on("end", () => { cb(); });
};
function cssDebug(cb, tmp) {
gulp.src(paths.srcCSS)
.pipe(gulp.dest(".", {cwd: tmp}))
.on("end", () => { cb(); });
};
function cssRelease(cb, dist) {
gulp.src(paths.srcCSS)
.pipe(concat("style.min.css"))
.pipe(cleanCss())
.pipe(gulp.dest(dist));
gulp.src(paths.srcSCSS)
.pipe(sass().on("error", sass.logError))
.pipe(concat("style.min.css"))
.pipe(cleanCss())
.pipe(gulp.dest(dist))
.on("end", function() { cb(); });
};
function elmDebug(res, rej, tmp) {
gulp.src(paths.mainElm)
.pipe(elm.bundle("index.js", { filetype: "js", debug: true}))
.on("error", function(err) {
log.error("Error encountered while compiling Elm, see browser for details.");
rej(err);
})
.pipe(gulp.dest(".", {cwd: tmp}))
.on("end", function() {
res();
});
};
function elmRelease(cb, dist) {
gulp.src(paths.mainElm)
.pipe(elm.bundle("script.min.js", { optimize: true }))
.pipe(
uglify({
compress: {
pure_funcs: [
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"A2",
"A3",
"A4",
"A5",
"A6",
"A7",
"A8",
"A9"
],
pure_getters: true,
keep_fargs: false,
unsafe_comps: true,
drop_console: true,
unsafe: true,
},
mangle: false,
})
)
.pipe(
uglify({
warnings: false,
compress: false,
mangle: true,
})
)
.pipe(hash({
format: "{name}.{hash:10}{ext}"
}))
.pipe(gulp.dest(dist))
.on("end", function() { cb(); });
}
function indexDebug(cb, tmp) {
var css = gulp.src(paths.tmpCSS, {cwd: tmp});
var js = gulp.src(paths.tmpJS, {cwd: tmp});
gulp.src(paths.pubIndex)
.pipe(gulp.dest(".", {cwd: tmp}))
.pipe(inject(css, { quiet: true } ))
.pipe(inject(js, { quiet: true }))
.pipe(gulp.dest(".", {cwd: tmp}))
.pipe(connect.reload());
cb();
};
function indexError(cb, tmp, content) {
const htmlContent = templates.errorIndex.replace("{error_content}", content);
string_src("index.html", htmlContent)
.pipe(gulp.dest(".", { cwd: tmp}))
.pipe(connect.reload())
.on("end", function() { cb(); })
}
function indexRelease(cb, dist) {
var distCss = dist + "/**/*.css";
var distJs = dist + "/**/*.js";
var css = gulp.src(distCss);
var js = gulp.src(distJs);
gulp.src(paths.pubIndex, {cwd: process.cwd()})
.pipe(gulp.dest(dist))
.pipe(inject(css, { relative: true, quiet: true }))
.pipe(inject(js, { relative: true, quiet: true }))
.pipe(gulp.dest(dist))
.on("end", function() { cb(); });
}
function hashCss(cb, dist) {
gulp.src(dist + "/" + "style.min.css")
.pipe(hash({
format: "{name}.{hash:10}{ext}"
}))
.pipe(gulp.dest(dist))
.on("end", function() {
cb();
});
}
function delCss(dist) {
fs.unlinkSync(path.join(process.cwd(), dist, "style.min.css"));
}
async function release(dist) {
new Promise((resolve) => {
assets(resolve, dist);
});
await new Promise((resolve) => {
elmRelease(resolve, dist);
});
await new Promise((resolve) => {
cssRelease(resolve, dist);
});
await new Promise((resolve) => {
hashCss(resolve, dist);
});
await new Promise((resolve) => {
indexRelease(resolve, dist);
});
delCss(dist);
log.info("Build successfully created");
}
function serve(tmp, host, port) {
var host = host || "0.0.0.0";
log.info(`Serving @ http://${host}:${port}`);
connect.server({
host: host,
port: port,
silent: true,
root: tmp,
livereload: true,
fallback: path.join(tmp, "index.html")
});
}
async function copy(resolve, tmp) {
try {
await Promise.all([
new Promise((resolve) => {
assets(resolve, tmp);
}),
new Promise((resolve) => {
scssDebug(resolve, tmp);
}),
new Promise((resolve) => {
cssDebug(resolve, tmp);
}),
new Promise((resolve, reject) => {
elmDebug(resolve, reject, tmp);
}),
]);
await new Promise((resolve) => {
indexDebug(resolve, tmp);
});
} catch (e) {
await new Promise((resolve) => {
indexError(resolve, tmp, e.message.replace(/\n/g, "<br />"));
});
}
resolve();
}
async function serving(tmp, host, port) {
fs.mkdirSync(tmp, {recursive: true});
await new Promise((resolve) => {
copy(resolve, tmp);
});
serve(tmp, host, port);
await new Promise((resolve) => {
watching(resolve, tmp, host, port);
});
open(`http://localhost:${port}`);
}
function watching(cb, tmp, host, port) {
gulp.watch([paths.srcElm, paths.envElm, paths.srcSCSS, paths.srcCSS], (resolve) => {
console.clear();
log.info("Changes dected");
log.info(`Reloaded @ http://${host}:${port}`);
copy(resolve, tmp);
});
cb();
}
exports.build = release;
exports.serve = serving;