-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.js
47 lines (40 loc) · 1.27 KB
/
transforms.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
const process = require('process')
const htmlmin = require('html-minifier')
const critical = require('critical')
const buildDir = '_site'
const shouldTransformHTML = (outputPath) => outputPath && outputPath.endsWith('.html') && process.env.ELEVENTY_ENV === 'production'
const isHomePage = (outputPath) => outputPath === `${buildDir}/index.html`
process.setMaxListeners(Number.POSITIVE_INFINITY)
module.exports = {
htmlmin(content, outputPath) {
// Minify all html when in production mode
if (shouldTransformHTML(outputPath)) {
return htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
})
}
return content
},
async critical(content, outputPath) {
if (shouldTransformHTML(outputPath) && isHomePage(outputPath)) {
// Do the transform for critical CSS on all html pages when in production mode.
// if (shouldTransformHTML(outputPath)) {
try {
const config = {
base: `${buildDir}/`,
html: content,
inline: true,
width: 1280,
height: 800,
}
const { html } = await critical.generate(config)
return html
} catch (error) {
console.error(error)
}
}
return content
},
}