11const { src, dest, task } = require ( "gulp" ) ;
22const clean = require ( 'gulp-clean' ) ;
3+ const gzip = require ( 'gulp-gzip' ) ;
34const inlineSource = require ( 'gulp-inline-source' ) ;
45const rename = require ( "gulp-rename" ) ;
56const through = require ( 'through2' ) ;
67
7- const toCHeader = ( ) => {
8- return through . obj ( ( file , enc , cb ) => {
9- const buf = file . contents ;
10- const len = buf . length ;
11- let idx = 0 ;
12- let data = "unsigned char index_html[] = {\n " ;
8+ const genHeader = ( size , buf , len ) => {
9+ let idx = 0 ;
10+ let data = "unsigned char index_html[] = {\n " ;
1311
14- for ( const value of buf ) {
15- idx ++ ;
12+ for ( const value of buf ) {
13+ idx ++ ;
1614
17- let current = value < 0 ? value + 256 : value ;
15+ let current = value < 0 ? value + 256 : value ;
1816
19- data += "0x" ;
20- data += ( current >>> 4 ) . toString ( 16 ) ;
21- data += ( current & 0xF ) . toString ( 16 ) ;
17+ data += "0x" ;
18+ data += ( current >>> 4 ) . toString ( 16 ) ;
19+ data += ( current & 0xF ) . toString ( 16 ) ;
2220
23- if ( idx === len ) {
24- data += "\n" ;
25- } else {
26- data += idx % 12 === 0 ? ",\n " : ", " ;
27- }
21+ if ( idx === len ) {
22+ data += "\n" ;
23+ } else {
24+ data += idx % 12 === 0 ? ",\n " : ", " ;
2825 }
26+ }
2927
30- data += "};\n" ;
31- data += `unsigned int index_html_len = ${ len } ;\n` ;
32- file . contents = Buffer . from ( data ) ;
33-
34- return cb ( null , file ) ;
35- } ) ;
28+ data += "};\n" ;
29+ data += `unsigned int index_html_len = ${ len } ;\n` ;
30+ data += `unsigned int index_html_size = ${ size } ;\n` ;
31+ return data ;
3632} ;
33+ let fileSize = 0 ;
3734
3835task ( 'clean' , ( ) => {
3936 return src ( 'dist' , { read : false , allowEmpty : true } )
@@ -43,7 +40,16 @@ task('clean', () => {
4340task ( 'default' , ( ) => {
4441 return src ( 'dist/index.html' )
4542 . pipe ( inlineSource ( ) )
46- . pipe ( toCHeader ( ) )
43+ . pipe ( through . obj ( ( file , enc , cb ) => {
44+ fileSize = file . contents . length ;
45+ return cb ( null , file ) ;
46+ } ) )
47+ . pipe ( gzip ( ) )
48+ . pipe ( through . obj ( ( file , enc , cb ) => {
49+ const buf = file . contents ;
50+ file . contents = Buffer . from ( genHeader ( fileSize , buf , buf . length ) ) ;
51+ return cb ( null , file ) ;
52+ } ) )
4753 . pipe ( rename ( "html.h" ) )
4854 . pipe ( dest ( '../src/' ) ) ;
4955} ) ;
0 commit comments