Skip to content

Commit 5b338e4

Browse files
committed
Minify everything when building
1 parent 3b59d66 commit 5b338e4

File tree

7 files changed

+2189
-39
lines changed

7 files changed

+2189
-39
lines changed

.eleventy.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
1+
const fs = require('fs');
2+
const inclusiveLangPlugin = require('@11ty/eleventy-plugin-inclusive-language');
3+
const htmlmin = require('html-minifier');
4+
const { minify } = require('terser');
15
const siteSettings = require('./src/globals/site.json');
26

3-
module.exports = function (config) {
4-
config.addPassthroughCopy('./src/css/tailwind.include.css');
5-
config.addPassthroughCopy({ public: './' });
7+
module.exports = function (eleventyConfig) {
8+
eleventyConfig.addPassthroughCopy('./src/css/tailwind.include.css');
9+
eleventyConfig.addPassthroughCopy({ public: './' });
10+
11+
eleventyConfig.addPlugin(inclusiveLangPlugin);
12+
13+
eleventyConfig.addNunjucksAsyncFilter('jsmin', async function (
14+
code,
15+
callback
16+
) {
17+
if (process.env.ELEVENTY_ENV === 'production') {
18+
try {
19+
const minified = await minify(code);
20+
callback(null, minified.code);
21+
} catch (err) {
22+
console.error('Terser error: ', err);
23+
// Fail gracefully.
24+
callback(null, code);
25+
}
26+
} else {
27+
callback(null, code);
28+
}
29+
});
30+
31+
eleventyConfig.setBrowserSyncConfig({
32+
callbacks: {
33+
ready: function (err, bs) {
34+
bs.addMiddleware('*', (req, res) => {
35+
const content_404 = fs.readFileSync('dist/404.html');
36+
// Provides the 404 content without redirect.
37+
res.write(content_404);
38+
// Add 404 http status code in request header.
39+
res.writeHead(404, { 'Content-Type': 'text/html' });
40+
res.end();
41+
});
42+
},
43+
},
44+
});
45+
46+
eleventyConfig.addTransform('htmlmin', function (content, outputPath) {
47+
if (
48+
process.env.ELEVENTY_ENV === 'production' &&
49+
outputPath &&
50+
outputPath.endsWith('.html')
51+
) {
52+
return htmlmin.minify(content, {
53+
useShortDoctype: true,
54+
removeComments: true,
55+
collapseWhitespace: true,
56+
});
57+
}
58+
59+
return content;
60+
});
661

762
return {
863
pathPrefix: siteSettings.baseUrl,

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
/dist
12
*.include.css
3+
package.json
4+
package-lock.json

0 commit comments

Comments
 (0)