-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
205 lines (172 loc) · 5.6 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
// gulpfile.js
const settings = require("./package.json").settings;
const gulp = require("gulp");
const del = require("del");
const ts = require("gulp-typescript");
const browserify = require("browserify");
const browserifyInc = require("browserify-incremental");
const source = require("vinyl-source-stream");
const buffer = require("vinyl-buffer");
const uglify = require("gulp-uglify");
const sourcemaps = require("gulp-sourcemaps");
const gutil = require("gulp-util");
const gulpif = require("gulp-if");
const changed = require("gulp-changed");
const rename = require("gulp-rename");
const browserSync = require("browser-sync");
const runSequence = require("run-sequence");
const tape = require("gulp-tape");
const through = require("through2");
const fs = require("fs");
const debug = settings.debug === true;
if (debug) { console.log("=== DEBUG Environment ===") }
else { console.log("=== RELEASE Environment ==="); }
// Clean destination directory
gulp.task("clean", () => {
let files = ["./.cache.json", "./*.log"];
if (debug) { files.push(settings.paths.debug + "*"); }
else { files.push(settings.paths.release + "*"); }
return del(files);
});
// Clean destination directory for all environnements
gulp.task("clean:all", () => {
let files = ["./.cache.json", "./*.log"];
files.push(settings.paths.debug + "*");
files.push(settings.paths.release + "*");
return del(files);
});
// Compile TypeScript files
gulp.task("compile", () => {
let config = "";
let dest = "";
if (debug) {
config = settings.tsconfig.debug;
dest = settings.paths.debug;
}
else {
config = settings.tsconfig.release;
dest = settings.paths.release;
}
let tsProject = ts.createProject(config);
return tsProject.src()
.pipe(gulpif(debug, sourcemaps.init()))
.pipe(tsProject()).js
.pipe(gulpif(debug, sourcemaps.write()))
.pipe(gulp.dest(dest))
.on("error", gutil.log);
});
// Bundle JavaScript files into a single file
gulp.task("bundle", ["compile"], () => {
const bundleFilename = settings.bundle;
const mainFilename = settings.main;
let dest = "";
if (debug) { dest = settings.paths.debug; }
else { dest = settings.paths.release; }
let b = browserifyInc({
"entries": dest + mainFilename,
"debug": true,
"cache": "./.cache.json"
})
return b
.bundle()
.pipe(source(bundleFilename))
.pipe(buffer())
.pipe(gulpif(debug, sourcemaps.init({ loadMaps: true })))
.pipe(uglify(/*{
"mangle": {
"properties": {
"keep_quoted": true
}
}
}*/))
.pipe(gulpif(debug, sourcemaps.write()))
.pipe(gulp.dest(dest))
.on("error", gutil.log)
.on("finish", () => {
if (!debug) {
del([dest + "*.js", "!" + dest + bundleFilename]);
}
});
});
// Copy all static assets
gulp.task("copy", () => {
let dest = "";
if (debug) { dest = settings.paths.debug; }
else { dest = settings.paths.release; }
if (debug) {
gulp.src(settings.paths.src + "*.debug.html")
.pipe(changed(dest))
.pipe(rename(path => {
path.basename = path.basename.split(".")[0];
}))
.pipe(gulp.dest(dest));
}
else {
gulp.src([settings.paths.src + "*.html", "!" + settings.paths.src + "*.debug.html"])
.pipe(changed(dest))
.pipe(gulp.dest(dest));
}
gulp.src(settings.paths.src + "*.json")
.pipe(changed(dest))
.pipe(gulp.dest(dest));
gulp.src(settings.paths.src + "*.glsl")
.pipe(changed(dest))
.pipe(gulp.dest(dest));
gulp.src(settings.paths.srcSounds + "**")
.pipe(changed(dest))
.pipe(gulp.dest(dest + settings.paths.tgtSounds));
gulp.src(settings.paths.srcImages + "**")
.pipe(changed(dest))
.pipe(gulp.dest(dest + settings.paths.tgtImages));
gulp.src(settings.paths.srcCss + "**")
.pipe(changed(dest))
.pipe(gulp.dest(dest + settings.paths.tgtCss));
gulp.src("./node_modules/dat.gui/build/dat.gui.min.js")
.pipe(changed(dest))
.pipe(gulp.dest(dest));
if (debug) {
gulp.src("./node_modules/dat.gui/build/dat.gui.js.map")
.pipe(changed(dest))
.pipe(gulp.dest(dest));
}
});
// Rebuild on change
gulp.task("watch", () => {
runSequence(["bundle", "copy"], "test");
gulp.watch(settings.paths.src + "**", () => {
runSequence(["bundle", "copy"], "test");
});
});
// Launch the HTTP server
gulp.task("serve", () => {
let dest = "";
if (debug) { dest = settings.paths.debug; }
else { dest = settings.paths.release; }
browserSync.init({
"port": settings.port,
"server": dest
});
});
// Rebuild on change and refresh the browser
gulp.task("watchRefresh", () => {
runSequence(["bundle", "copy"], ["serve", "test"]);
gulp.watch(settings.paths.src + "**", () => {
runSequence(["bundle", "copy"], "test", browserSync.reload);
});
});
// Default task
gulp.task("default", ["bundle", "copy"]);
// Unit tests
gulp.task("test", () => {
if (!debug) return;
process.stdout.write("\x1Bc");
const reporter = through.obj();
reporter
.pipe(process.stdout);
return gulp.src(settings.paths.tests + "*.js")
.pipe(tape({
"bail": false,
"outputStream": fs.createWriteStream(settings.paths.tests + "tape.log"),
"reporter": reporter
}));
});