-
Notifications
You must be signed in to change notification settings - Fork 284
/
optimize-html.js
124 lines (113 loc) · 3.6 KB
/
optimize-html.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
/**
* Copyright (c) 2020 Google Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const minify = require("html-minifier").minify;
const AmpOptimizer = require("@ampproject/toolbox-optimizer");
const ampOptimizer = AmpOptimizer.create({
blurredPlaceholders: true,
imageBasePath: "./_site/",
//verbose: true,
});
const PurgeCSS = require("purgecss").PurgeCSS;
const csso = require("csso");
/**
* Inlines the CSS.
* Makes font display display-optional
* Minifies and optimizes the JS
* Optimizes HTML
* Optimizes AMP
*/
const purifyCss = async (rawContent, outputPath) => {
let content = rawContent;
if (
outputPath &&
outputPath.endsWith(".html") &&
!isAmp(content) &&
!/data-style-override/.test(content)
) {
let before = require("fs").readFileSync("css/main.css", {
encoding: "utf-8",
});
before = before.replace(
/@font-face {/g,
"@font-face {font-display:optional;"
);
const purged = await new PurgeCSS().purge({
content: [
{
raw: rawContent,
extension: "html",
},
],
css: [
{
raw: before,
},
],
/*extractors: [
{
extractor: require("purge-from-html").extract,
extensions: ["html"],
},
],*/
fontFace: true,
variables: true,
});
const after = csso.minify(purged[0].css).css;
//console.log("CSS reduction", before.length - after.length);
content = content.replace("</head>", `<style>${after}</style></head>`);
}
return content;
};
const minifyHtml = (rawContent, outputPath) => {
let content = rawContent;
if (outputPath && outputPath.endsWith(".html") && !isAmp(content)) {
content = minify(content, {
removeAttributeQuotes: true,
collapseBooleanAttributes: true,
collapseWhitespace: true,
removeComments: true,
sortClassName: true,
sortAttributes: true,
html5: true,
decodeEntities: true,
removeOptionalTags: true,
});
}
return content;
};
const optimizeAmp = async (rawContent, outputPath) => {
let content = rawContent;
if (outputPath && outputPath.endsWith(".html") && isAmp(content)) {
content = await ampOptimizer.transformHtml(content);
}
return content;
};
module.exports = {
initArguments: {},
configFunction: async (eleventyConfig, pluginOptions = {}) => {
eleventyConfig.addTransform("purifyCss", purifyCss);
eleventyConfig.addTransform("minifyHtml", minifyHtml);
eleventyConfig.addTransform("optimizeAmp", optimizeAmp);
},
};
function isAmp(content) {
return /\<html amp/i.test(content);
}