-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
144 lines (113 loc) · 3.73 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs')
const jsdom = require('jsdom')
const compressor = require('node-minify')
const handlebars = require('handlebars')
const minify = require('html-minifier').minify
const { v1: uuidv1 } = require('uuid')
const buildId = uuidv1()
const path = require('path')
const dir = path.join(__dirname, 'public')
console.log(`starting build in ${__dirname} => ${dir}`)
handlebars.registerPartial('include', (context) => {
const filePath = path.join(__dirname, '/views/inc/', `${context.path}.mustache`)
const html = fs.readFileSync(filePath).toString()
const include = handlebars.compile(html)
return include(context)
})
const readFilePath = path.join(__dirname, '/views/index.mustache')
fs.readFile(readFilePath, (err, content) => {
if (err) return console.log(err)
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json')))
if (typeof process.env.HOST !== 'undefined') {
config.host = process.env.HOST
}
if (typeof config.skills !== 'undefined') {
config.skills = config.skills.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()))
}
const indexHtml = String(content)
const index = handlebars.compile(indexHtml)
const html = index(config)
const document = new jsdom.JSDOM(html, {})
const window = document.window
require('jquery')(window)
const jQuery = window.$
const tasks = []
// minify js
const scriptsFile = `${dir}/app.min.js`
const scripts = []
jQuery('body script').each((index, script) => {
script = jQuery(script)
const src = script.attr('src')
const srcPath = `static/${src}`
if (fs.existsSync(srcPath)) {
scripts.push(srcPath)
script.remove()
}
})
console.log('JS FILES', scripts)
const jsMini = compressor.minify({
compressor: 'uglify-es',
input: scripts,
output: scriptsFile
}).then(() => {
console.log(`minified js to ${scriptsFile}`)
}).catch((err) => {
console.log('JS', err)
})
tasks.push(jsMini)
// minify css
const stylesFile = `${dir}/app.min.css`
const stylesheets = []
jQuery('head link[rel="stylesheet"]').each((index, style) => {
style = jQuery(style)
const href = style.attr('href')
const stylePath = `static/${href}`
if (fs.existsSync(stylePath)) {
stylesheets.push(stylePath)
style.remove()
}
})
console.log('CSS FILES', stylesheets)
const cssMini = compressor.minify({
compressor: 'clean-css',
input: stylesheets,
output: stylesFile
}).then(() => {
console.log(`minified css to ${stylesFile}`)
}).catch((err) => {
console.log('CSS', err)
})
tasks.push(cssMini)
// minify html
Promise.all(tasks).then(() => {
const scriptsContainer = jQuery('<script>')
scriptsContainer.attr('async', '')
scriptsContainer.appendTo(jQuery('body .scripts'))
scriptsContainer.attr('src', `/app.min.js?v=${buildId}`)
const styleContainer = jQuery('<link>')
styleContainer.attr('rel', 'stylesheet')
styleContainer.attr('href', `/app.min.css?v=${buildId}`)
jQuery('head').append(styleContainer)
jQuery('img').each((index, img) => {
img = jQuery(img)
const source = img.attr('src')
img.attr({
src: config.zero,
'data-src': source
})
img.addClass('lazyload')
})
const finalHtml = window.document.documentElement.outerHTML
const minifiedHtml = minify(finalHtml, {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true,
keepClosingSlash: true
})
const newHtml = '<!doctype html>' + minifiedHtml + '<!-- update 22/09/2020 -->'
fs.writeFileSync(`${dir}/index.min.html`, newHtml)
console.log(`minified html to ${dir}/index.min.html`)
}).catch((err) => {
console.log(err)
})
})