1
1
import * as child_process from 'child_process' ;
2
2
import * as path from 'path' ;
3
3
import * as sander from 'sander' ;
4
+ import * as clorox from 'clorox' ;
4
5
import cheerio from 'cheerio' ;
5
6
import URL from 'url-parse' ;
6
7
import fetch from 'node-fetch' ;
7
8
import * as ports from 'port-authority' ;
9
+ import prettyBytes from 'pretty-bytes' ;
8
10
import { minify_html } from './utils/minify_html' ;
9
11
import { locations } from '../config' ;
10
12
@@ -41,18 +43,22 @@ export async function exporter(export_dir: string) {
41
43
proc . on ( 'message' , message => {
42
44
if ( ! message . __sapper__ ) return ;
43
45
44
- const url = new URL ( message . url , origin ) ;
46
+ let file = new URL ( message . url , origin ) . pathname . slice ( 1 ) ;
47
+ let { body } = message ;
45
48
46
- if ( saved . has ( url . pathname ) ) return ;
47
- saved . add ( url . pathname ) ;
49
+ if ( saved . has ( file ) ) return ;
50
+ saved . add ( file ) ;
48
51
49
- if ( message . type === 'text/html' ) {
50
- const file = `${ export_dir } /${ url . pathname } /index.html` ;
51
- sander . writeFileSync ( file , minify_html ( message . body ) ) ;
52
- } else {
53
- const file = `${ export_dir } /${ url . pathname } ` ;
54
- sander . writeFileSync ( file , message . body ) ;
52
+ const is_html = message . type === 'text/html' ;
53
+
54
+ if ( is_html ) {
55
+ file = file === '' ? 'index.html' : `${ file } /index.html` ;
56
+ body = minify_html ( body ) ;
55
57
}
58
+
59
+ console . log ( `${ clorox . bold . cyan ( file ) } ${ clorox . gray ( `(${ prettyBytes ( body . length ) } )` ) } ` ) ;
60
+
61
+ sander . writeFileSync ( `${ export_dir } /${ file } ` , body ) ;
56
62
} ) ;
57
63
58
64
function handle ( url : URL ) {
@@ -79,7 +85,7 @@ export async function exporter(export_dir: string) {
79
85
}
80
86
} )
81
87
. catch ( ( err : Error ) => {
82
- console . error ( ` Error rendering ${ url . pathname } : ${ err . message } `) ;
88
+ console . log ( clorox . red ( `> Error rendering ${ url . pathname } : ${ err . message } `) ) ;
83
89
} ) ;
84
90
}
85
91
0 commit comments