Skip to content

Commit 50c7f68

Browse files
committed
Initial hacky build system for darkUI
1 parent ce09ca1 commit 50c7f68

File tree

2 files changed

+1353
-1204
lines changed

2 files changed

+1353
-1204
lines changed

gulpfile.js

+114-5
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ var buildVersion = null;
2424
gulp.task("default", ["browser-sync", "build-default", "watch"]);
2525
gulp.task("build-default", ["build-js", "build-sass"], browserSync.reload);
2626

27-
gulp.task("build", function(cb) {
28-
runSequence(["build-js-prod", "build-sass"], function() {
27+
gulp.task("build-deploy", function(cb) {
28+
runSequence(["build-js-prod", "build-sass-dark"], function() {
2929
browserSync.reload();
3030
cb();
3131
});
3232
});
3333

3434
gulp.task("deploy", function(cb) {
3535
buildVersion = getBuildVersion();
36-
runSequence("build", "clean-build", "copy-build", "inject-file-versions", "inject-build-version", "minify-html", cb);
36+
runSequence("build-deploy", "clean-build", "copy-build", "inject-file-versions", "inject-build-version", "minify-html", cb);
3737
});
3838

3939
gulp.task("browser-sync", function() {
@@ -74,7 +74,7 @@ gulp.task("build-js", function() {
7474
});
7575

7676
gulp.task("build-js-prod", function() {
77-
return rollup({
77+
return rollup({
7878
entry: "dev/src/app.js",
7979
moduleContext: {"dev/lib/codemirror.js":"window"},
8080
plugins: [babel({
@@ -142,7 +142,6 @@ gulp.task("inject-build-version", function(cb) {
142142
fs.writeFile("build/deploy/regexr.js", js, cb);
143143
});
144144

145-
146145
gulp.task("minify-html", function() {
147146
return gulp.src("build/index.html")
148147
.pipe(htmlmin({collapseWhitespace: true, conservativeCollapse: true, removeComments: true}))
@@ -202,4 +201,114 @@ function getDateString() {
202201
var now = new Date();
203202
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
204203
return months[now.getMonth()]+" "+now.getDate()+", "+now.getFullYear();
204+
}
205+
206+
// dark theme:
207+
// TODO: this whole approach can be cleaned up, ideally when moving to gulp v4
208+
var dark = "", root = "";
209+
gulp.task("build-sass-dark0", function(cb) {
210+
root = fs.readFileSync("dev/sass/regexr.scss", "utf-8");
211+
fs.writeFile("dev/sass/regexr.scss", root.replace('@import "colors";', '@import "colors_dark";'), cb);
212+
});
213+
gulp.task("build-sass-dark1", function(cb) {
214+
dark = fs.readFileSync("deploy/regexr.css", "utf-8");
215+
fs.writeFile("dev/sass/regexr.scss", root, cb);
216+
});
217+
gulp.task("build-sass-dark2", function(cb) {
218+
var def = fs.readFileSync("deploy/regexr.css", "utf-8");
219+
var diff = (new CSSDiff()).diff(def, dark, false);
220+
fs.writeFile("assets/themes/dark.css", diff, cb);
221+
dark = root = ""; // clean up.
222+
});
223+
224+
gulp.task("build-sass-dark", function(cb) {
225+
runSequence("build-sass-dark0", "build-sass", "build-sass-dark1", "build-sass", "build-sass-dark2", cb);
226+
});
227+
228+
229+
// CSSDiff:
230+
class CSSDiff {
231+
constructor() {}
232+
233+
diff(base, targ, pretty=false) {
234+
let diff = this.compare(this.parse(base), this.parse(targ));
235+
return this._writeDiff(diff, pretty);
236+
}
237+
238+
parse(s, o={}) {
239+
this._parse(s, /([^\n\r\{\}]+?)\s*\{\s*/g, /\}/g, o);
240+
for (let n in o) {
241+
if (n === " keys") { continue; }
242+
o[n] = this.parseBlock(o[n]);
243+
}
244+
return o;
245+
}
246+
247+
parseBlock(s, o={}) {
248+
return this._parse(s, /([^\s:]+)\s*:/g, /(?:;|$)/g, o);
249+
}
250+
251+
compare(o0, o1, o={}) {
252+
let keys = o1[" keys"], l=keys.length, arr=[];
253+
for (let i=0; i<l; i++) {
254+
let n = keys[i];
255+
if (!o0[n]) { o[n] = o1[n]; arr.push(n); continue; }
256+
let diff = this._compareBlock(o0[n], o1[n]);
257+
if (diff) { o[n] = diff; arr.push(n); }
258+
}
259+
o[" keys"] = arr;
260+
return o;
261+
}
262+
263+
_compareBlock(o0, o1) {
264+
let keys = o1[" keys"], l=keys.length, arr=[], o;
265+
for (let i=0; i<l; i++) {
266+
let n = keys[i];
267+
if (o0[n] === o1[n]) { continue; }
268+
if (!o) { o = {}; }
269+
o[n] = o1[n];
270+
arr.push(n);
271+
}
272+
if (o) { o[" keys"] = arr; }
273+
return o;
274+
}
275+
276+
_parse(s, keyRE, closeRE, o) {
277+
let i, match, arr=[];
278+
while (match = keyRE.exec(s)) {
279+
let key = match[1];
280+
i = closeRE.lastIndex = keyRE.lastIndex;
281+
if (!(match = closeRE.exec(s))) { console.log("couldn't find close", key); break; }
282+
o[key] = s.substring(i, closeRE.lastIndex-match[0].length).trim();
283+
i = keyRE.lastIndex = closeRE.lastIndex;
284+
arr.push(key);
285+
}
286+
o[" keys"] = arr;
287+
return o;
288+
}
289+
290+
_writeDiff(o, pretty=false) {
291+
let diff = "", ln="\n", s=" ";
292+
if (!pretty) { ln = s = ""; }
293+
let keys = o[" keys"], l=keys.length;
294+
for (let i=0; i<l; i++) {
295+
let n = keys[i];
296+
if (diff) { diff += ln + ln; }
297+
diff += n + s + "{" + ln;
298+
diff += this._writeBlock(o[n], pretty);
299+
diff += "}";
300+
}
301+
return diff;
302+
}
303+
304+
_writeBlock(o, pretty=false) {
305+
let diff = "", ln="\n", t="\t", s=" ";
306+
if (!pretty) { ln = t = s = ""; }
307+
let keys = o[" keys"], l=keys.length;
308+
for (let i=0; i<l; i++) {
309+
let n = keys[i];
310+
diff += t + n + ":" + s + o[n] + ";" + ln;
311+
}
312+
return diff;
313+
}
205314
}

0 commit comments

Comments
 (0)