-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·78 lines (67 loc) · 2.16 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
var StringDecoder = require('string_decoder').StringDecoder
var minhtml = require('html-minifier').minify
var hyperstream = require('hyperstream')
var purify = require('purify-css')
var through = require('through2')
var mkdirp = require('mkdirp')
var path = require('path')
var pump = require('pump')
var fs = require('fs')
var app = require('./')
mkdirp.sync('dist/')
process.stdout.write('[build.js] Writing HTML to static file…')
var route = '/'
var tmpfile = path.join(process.cwd(), 'dist/_index.html')
var outfile = path.join(process.cwd(), 'dist/index.html')
var html = app.toString(route)
var hs = hyperstream({ body: { _html: html } })
var index = fs.createReadStream(path.join(process.cwd(), 'dist', 'index.html'))
var out = fs.createWriteStream(tmpfile)
var css = fs.readFileSync(path.join(process.cwd(), 'dist/bundle.css'), 'utf8')
var js = fs.readFileSync(path.join(process.cwd(), 'dist/bundle.js'), 'utf8')
var newCss = purify(js, css, { minify: true })
fs.writeFileSync(path.join(process.cwd(), 'dist/bundle.css'), newCss)
pump(index, hs, out, function (err) {
if (err) throw err
var minify = htmlMinifyStream()
var outstream = fs.createWriteStream(outfile)
pump(fs.createReadStream(tmpfile), minify, outstream, function (err) {
if (err) throw err
fs.unlinkSync(tmpfile)
process.stdout.write(' done!\n')
})
})
function htmlMinifyStream () {
var opts = {
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
conservativeCollapse: true,
decodeEntities: true,
quoteCharacter: '"',
removeComments: true,
removeEmptyAttributes: true,
removeEmptyElements: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
useShortDoctype: true
}
var decoder = new StringDecoder('utf8')
var src = ''
return through(write, flush)
function write (chunk, _, cb) {
src += decoder.write(chunk)
cb()
}
function flush (cb) {
src += decoder.end()
var res = minhtml(src, opts)
this.push(res)
cb()
}
}