-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
87 lines (73 loc) · 2.62 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
const gulp = require("gulp");
const webpack = require("webpack");
const webpackStream = require("webpack-stream");
const clone = require("gulp-clone");
const sass = require("gulp-sass");
const sourcemaps = require("gulp-sourcemaps");
const imagemin = require("gulp-imagemin");
const html = require("gulp-htmlclean");
const del = require("del");
const fs = require("fs");
const Canvas = require("canvas-prebuilt");
gulp.task("js", () => {
return gulp.src("./src/js/main.js")
.pipe(webpackStream(require("./webpack.config.js"), webpack))
.pipe(gulp.dest("./build/"));
});
gulp.task("sass", () => {
return gulp.src("./src/sass/main.scss")
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: "compressed" })
.on("error", sass.logError))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("./build/css/"));
});
gulp.task("html", () => {
return gulp.src("./src/**.html", { base: "./src" })
.pipe(html())
.pipe(gulp.dest("./build/"));
});
gulp.task("images", () => {
return gulp.src("./src/assets/**/*.(png|gif|jpg|jpeg|bmp|svg)")
.pipe(imagemin())
.pipe(gulp.dest("./build/assets/"));
});
gulp.task("assets", () => {
return gulp.src(["./src/assets/**/*", "!./src/assets/**/*.(png|gif|jpg|jpeg|bmp|svg)"])
.pipe(clone())
.pipe(gulp.dest("./build/assets/"));
});
gulp.task("maps", cb => {
fs.readdir("./src/maps", (err, files) => {
if (err) cb(err);
const width = 800;
const height = 200;
const canvas = new Canvas(width, height * (files.length || 1));
const ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
files.forEach((map, i) => {
const img = new Canvas.Image();
img.src = fs.readFileSync("./src/maps/" + map);
ctx.drawImage(img, 0, i * height);
});
if (!fs.existsSync("./build/assets/")) fs.mkdirSync("./build/assets/");
fs.writeFile("./build/assets/maps.png", canvas.toBuffer(), cb);
});
});
gulp.task("clean", () => {
del.sync("./build/**/*");
});
gulp.task("build", ["clean", "html", "images", "assets", "maps", "js", "sass"]);
gulp.task("watch", () => {
const express = require("express");
const app = express();
app.use(express.static("./build/"));
app.listen(8080, "0.0.0.0", () => console.log("Listening at localhost:8080"));
gulp.watch("./src/js/**/*.js", ["js"]);
gulp.watch("./src/sass/**/*.scss", ["sass"]);
gulp.watch("./src/**.html", ["html"]);
gulp.watch("./src/assets/**/*.(png|gif|jpg|jpeg|bmp|svg)", ["images"]);
gulp.watch(["./src/assets/**/*", "!./src/assets/**/*.(png|gif|jpg|jpeg|bmp|svg)"], ["assets"]);
});
gulp.task("default", ["build", "watch"]);