html: embed gziped version

This commit is contained in:
Shuanglei Tao
2020-02-05 16:42:43 +08:00
parent 0e728e61ce
commit 437e63a39c
8 changed files with 8541 additions and 34036 deletions

View File

@@ -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/'));
});