Skip to content

Commit b424fa2

Browse files
committed
shaveoff: smaller builds
1 parent b9d3bcc commit b424fa2

File tree

8 files changed

+262
-14
lines changed

8 files changed

+262
-14
lines changed

_includes/base-simple-header.njk

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>tinytown.studio</title>
7-
<style>
8-
{% include "../node_modules/shed-css/dist/index.css" %}
9-
</style>
7+
<link rel="stylesheet" href="/shed-css/index.css" />
108
<link rel="stylesheet" href="/main.css" />
119
<link rel="preconnect" href="https://fonts.googleapis.com" />
1210
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
@@ -17,7 +15,7 @@
1715
</head>
1816
<body>
1917
<header>
20-
<h1 class="m:0 p:0 m-t:2">tinyTown</h1>
18+
<h1 class="m:0 p:0 m-t:2">tiny<span class="t-t:u">Town</span></h1>
2119
<p class="m:0 p:0 sub-heading subtle">tiny things for the tiny ones</p>
2220
</header>
2321
<div class="m-t:10">

_includes/base.njk

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>tinytown.studio</title>
7-
<style>
8-
{% include "../node_modules/shed-css/dist/index.css" %}
9-
</style>
10-
<link rel="stylesheet" href="/main.css" />
7+
<link rel="stylesheet" href="/node_modules/shed-css/dist/index.css" />
8+
<link rel="stylesheet" href="/main.css" /
119
<link rel="preconnect" href="https://fonts.googleapis.com" />
1210
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
1311
<link

_lib/purge.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { purgeCSSPlugin } from '@fullhuman/postcss-purgecss';
2+
import fs from 'fs';
3+
import postCSS from 'postcss';
4+
import glob from 'tiny-glob';
5+
import MinifyCSS from 'clean-css';
6+
7+
import { extname, join } from 'node:path';
8+
9+
export async function purge(directories) {
10+
const inputDir = directories.output;
11+
12+
const requiredFiles = await glob('**/*.{html,css}', {
13+
cwd: join(process.cwd(), inputDir),
14+
absolute: true,
15+
});
16+
17+
const cssFiles = [];
18+
const htmlFiles = [];
19+
20+
for (let file of requiredFiles) {
21+
if (extname(file) === '.css') {
22+
cssFiles.push(file);
23+
}
24+
if (extname(file) === '.html') {
25+
htmlFiles.push(file);
26+
}
27+
}
28+
29+
for (let file of cssFiles) {
30+
const sourceCSS = await fs.promises.readFile(file, 'utf8');
31+
const allCSS = await postCSS([
32+
purgeCSSPlugin({
33+
content: htmlFiles,
34+
variables: true,
35+
keyframes: true,
36+
defaultExtractor: (content) => content.match(/[\w-:./]+(?<!:)/g) || [],
37+
}),
38+
])
39+
.process(sourceCSS, {
40+
from: file,
41+
to: file,
42+
})
43+
.catch((error) => {
44+
console.log(error);
45+
});
46+
47+
const minifiedCSS = new MinifyCSS().minify(allCSS.css).styles;
48+
await fs.promises.writeFile(file, minifiedCSS, 'utf8');
49+
}
50+
}

eleventy.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { EleventyHtmlBasePlugin } from '@11ty/eleventy';
2+
import { purge } from './_lib/purge.js';
23

34
export default function (eleventyConfig) {
45
eleventyConfig.addPassthroughCopy({
56
'./public/': '/',
67
});
8+
eleventyConfig.addPassthroughCopy({
9+
'./node_modules/shed-css/dist/index.css':
10+
'/node_modules/shed-css/dist/index.css',
11+
});
712
eleventyConfig.addPlugin(EleventyHtmlBasePlugin);
13+
14+
eleventyConfig.on(
15+
'eleventy.after',
16+
async ({ directories, results, runMode, outputMode }) => {
17+
await purge(directories);
18+
}
19+
);
820
}

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"type": "module",
1414
"dependencies": {
1515
"@11ty/eleventy": "^3.0.0",
16+
"clean-css": "^5.3.3",
1617
"shed-css": "^1.4.0"
18+
},
19+
"devDependencies": {
20+
"@fullhuman/postcss-purgecss": "^7.0.2",
21+
"eleventy-plugin-purgecss": "^0.5.0",
22+
"postcss": "^8.5.1",
23+
"tiny-glob": "^0.2.9"
1724
}
1825
}

0 commit comments

Comments
 (0)