Skip to content

Commit 3d1a0f7

Browse files
committed
Consolidate into single minify transform
1 parent a6e4607 commit 3d1a0f7

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

.eleventy.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module.exports = (eleventyConfig) => {
2-
eleventyConfig.addTransform("jsmin", require("./transforms/jsmin"));
3-
eleventyConfig.addTransform("htmlmin", require("./transforms/htmlmin"));
2+
eleventyConfig.addTransform("minify", require("./transforms/minify"));
43

54
return {
65
dir: {

transforms/htmlmin.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
module.exports = async (content, outputPath) => {
2-
if (!outputPath.endsWith(".html")) {
3-
return content;
4-
}
1+
const minify = require("html-minifier").minify;
2+
const prettier = require("prettier");
53

6-
const $content = require("prettier").format(content, { parser: "html" });
4+
module.exports = async (content, outputPath) => {
5+
const $content = prettier.format(content, { parser: "html" });
76

87
// If this is NOT a production build, return the prettified HTML for easier debugging.
98
if (process.env.NODE_ENV !== "production") {
109
return $content;
1110
}
1211

1312
// Else, let's "aggressively" minify the HTML (and any inlined CSS/JavaScript).
14-
return require("html-minifier").minify($content, {
13+
return minify($content, {
1514
collapseWhitespace: true,
1615
minifyCSS: true,
1716
minifyJS: true,

transforms/jsmin.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
module.exports = async (content, outputPath) => {
2-
if (!outputPath.endsWith(".min.js")) {
3-
return content;
4-
}
1+
const minify = require("terser").minify;
52

6-
const minified = await require("terser").minify(content, {});
3+
module.exports = async (content, outputPath) => {
4+
const minified = await minify(content, {});
75
return minified.code;
86
};

transforms/minify.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const extname = require("path").extname;
2+
3+
const htmlmin = require("./htmlmin");
4+
const jsmin = require("./jsmin");
5+
6+
module.exports = async (content, outputPath) => {
7+
const ext = extname(outputPath);
8+
switch (ext) {
9+
case ".html":
10+
return htmlmin(content, outputPath);
11+
case ".js":
12+
return jsmin(content, outputPath);
13+
default:
14+
return content;
15+
}
16+
};

0 commit comments

Comments
 (0)