eleventy-auto-cache-buster 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish.yml +41 -0
- package/11tyAutoCacheBuster.js +87 -0
- package/package.json +12 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: Publish npm package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
github-release:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
permissions:
|
|
12
|
+
contents: write
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
|
|
16
|
+
- uses: actions/setup-node@v3
|
|
17
|
+
with:
|
|
18
|
+
node-version: 20
|
|
19
|
+
|
|
20
|
+
- run: yarn install --immutable
|
|
21
|
+
|
|
22
|
+
- uses: ncipollo/release-action@v1.12.0
|
|
23
|
+
with:
|
|
24
|
+
artifacts: "11tyAutoCacheBuster.js"
|
|
25
|
+
|
|
26
|
+
npm-publish:
|
|
27
|
+
runs-on: ubuntu-latest
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v3
|
|
30
|
+
|
|
31
|
+
- uses: actions/setup-node@v3
|
|
32
|
+
with:
|
|
33
|
+
node-version: 20
|
|
34
|
+
registry-url: https://registry.npmjs.org/
|
|
35
|
+
|
|
36
|
+
- run: yarn install --immutable
|
|
37
|
+
|
|
38
|
+
- run: yarn publish
|
|
39
|
+
env:
|
|
40
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
41
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const crypto = require("crypto");
|
|
3
|
+
const glob = require("glob");
|
|
4
|
+
|
|
5
|
+
function hash(content, algorithm="md5") {
|
|
6
|
+
const currentHash = crypto.createHash(algorithm);
|
|
7
|
+
currentHash.setEncoding("hex");
|
|
8
|
+
currentHash.write(content);
|
|
9
|
+
currentHash.end();
|
|
10
|
+
return currentHash.read();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function logRegular(string) {
|
|
14
|
+
console.log(string);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function _logColour(string, colourCode) {
|
|
18
|
+
console.log(`\x1b[${colourCode}m${string} \x1b[0m`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function logGreen(string) {
|
|
22
|
+
_logColour(string, "32");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function logYellow(string) {
|
|
26
|
+
_logColour(string, "33");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function logRed(string) {
|
|
30
|
+
_logColour(string, "31");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
module.exports = function(eleventyConfig, globstring="**/*.{css,js,png,jpg,jpeg,gif,mp4,ico}") {
|
|
35
|
+
eleventyConfig.on("eleventy.after", async ({ dir, results, runMode, outputMode }) => {
|
|
36
|
+
const assetPaths = [];
|
|
37
|
+
logYellow(`[ACB] Collecting assets & calculating hashes using ${globstring}...`);
|
|
38
|
+
(await glob.glob(dir.output + "/" + globstring)).forEach((assetPath) => {
|
|
39
|
+
assetPath = assetPath.replace(/\\/g, "/")
|
|
40
|
+
logGreen(`[ACB] ${assetPath} is an asset! Calculating hash...`);
|
|
41
|
+
const assetHash = hash(fs.readFileSync(assetPath));
|
|
42
|
+
logGreen(`[ACB] ${assetPath} hash = ${assetHash}`);
|
|
43
|
+
|
|
44
|
+
assetPaths.push({
|
|
45
|
+
assetPath: assetPath.replace(dir.output, ""),
|
|
46
|
+
assetHash: assetHash
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
logYellow(`[ACB] Collected all asset hashes!`);
|
|
51
|
+
logRegular(`[ACB] Replacing in output...`);
|
|
52
|
+
|
|
53
|
+
// For every file Eleventy outputs
|
|
54
|
+
results.forEach(({inputPath, outputPath, url, content}) => {
|
|
55
|
+
let outputData = ""; // Assigned later
|
|
56
|
+
let outputChanged = false; // Check if any hashes have been added
|
|
57
|
+
|
|
58
|
+
// Read the output content
|
|
59
|
+
fs.readFile(outputPath, encoding="UTF-8", (err, data) => {
|
|
60
|
+
if (err) {
|
|
61
|
+
logRed(err);
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
// Save the output data
|
|
65
|
+
outputData = data;
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
assetPaths.forEach(({assetPath, assetHash}) => {
|
|
69
|
+
if (data.includes(assetPath)) {
|
|
70
|
+
logGreen(`[ACB] ${outputPath} contains asset ${assetPath}`)
|
|
71
|
+
outputData = outputData.replace(assetPath, assetPath + "?v=" + assetHash);
|
|
72
|
+
outputChanged = true;
|
|
73
|
+
|
|
74
|
+
} else {
|
|
75
|
+
logRegular(`[ACB] ${outputPath} does NOT contain asset ${assetPath}. Skipping`)
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (outputChanged) {
|
|
80
|
+
fs.writeFile(outputPath, outputData, () => {
|
|
81
|
+
logGreen(`[ACB] Added hashes to ${outputPath}`);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eleventy-auto-cache-buster",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Busts cache using ?v= with minimal configuration!",
|
|
5
|
+
"main": "11tyAutoCacheBuster.js",
|
|
6
|
+
"repository": "https://github.com/Denperidge/eleventy-auto-cache-buster.git",
|
|
7
|
+
"author": "Denperidge",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"glob": "^10.3.10"
|
|
11
|
+
}
|
|
12
|
+
}
|