mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-21 19:34:19 +01:00
html: embed gziped version
This commit is contained in:
@@ -1,39 +1,36 @@
|
||||
const { src, dest, task } = require("gulp");
|
||||
const clean = require('gulp-clean');
|
||||
const gzip = require('gulp-gzip');
|
||||
const inlineSource = require('gulp-inline-source');
|
||||
const rename = require("gulp-rename");
|
||||
const through = require('through2');
|
||||
|
||||
const toCHeader = () => {
|
||||
return through.obj((file, enc, cb) => {
|
||||
const buf = file.contents;
|
||||
const len = buf.length;
|
||||
let idx = 0;
|
||||
let data = "unsigned char index_html[] = {\n ";
|
||||
const genHeader = (size, buf, len) => {
|
||||
let idx = 0;
|
||||
let data = "unsigned char index_html[] = {\n ";
|
||||
|
||||
for (const value of buf) {
|
||||
idx++;
|
||||
for (const value of buf) {
|
||||
idx++;
|
||||
|
||||
let current = value < 0 ? value + 256 : value;
|
||||
let current = value < 0 ? value + 256 : value;
|
||||
|
||||
data += "0x";
|
||||
data += (current >>> 4).toString(16);
|
||||
data += (current & 0xF).toString(16);
|
||||
data += "0x";
|
||||
data += (current >>> 4).toString(16);
|
||||
data += (current & 0xF).toString(16);
|
||||
|
||||
if (idx === len) {
|
||||
data += "\n";
|
||||
} else {
|
||||
data += idx % 12 === 0 ? ",\n " : ", ";
|
||||
}
|
||||
if (idx === len) {
|
||||
data += "\n";
|
||||
} else {
|
||||
data += idx % 12 === 0 ? ",\n " : ", ";
|
||||
}
|
||||
}
|
||||
|
||||
data += "};\n";
|
||||
data += `unsigned int index_html_len = ${len};\n`;
|
||||
file.contents = Buffer.from(data);
|
||||
|
||||
return cb(null, file);
|
||||
});
|
||||
data += "};\n";
|
||||
data += `unsigned int index_html_len = ${len};\n`;
|
||||
data += `unsigned int index_html_size = ${size};\n`;
|
||||
return data;
|
||||
};
|
||||
let fileSize = 0;
|
||||
|
||||
task('clean', () => {
|
||||
return src('dist', {read: false, allowEmpty: true})
|
||||
@@ -43,7 +40,16 @@ task('clean', () => {
|
||||
task('default', () => {
|
||||
return src('dist/index.html')
|
||||
.pipe(inlineSource())
|
||||
.pipe(toCHeader())
|
||||
.pipe(through.obj((file, enc, cb) => {
|
||||
fileSize = file.contents.length;
|
||||
return cb(null, file);
|
||||
}))
|
||||
.pipe(gzip())
|
||||
.pipe(through.obj((file, enc, cb) => {
|
||||
const buf = file.contents;
|
||||
file.contents = Buffer.from(genHeader(fileSize, buf, buf.length));
|
||||
return cb(null, file);
|
||||
}))
|
||||
.pipe(rename("html.h"))
|
||||
.pipe(dest('../src/'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user