eleventy-auto-cache-buster 0.1.1 → 0.3.0
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/11tyAutoCacheBuster.js +28 -5
- package/LICENSE +21 -0
- package/README.md +49 -0
- package/package.json +1 -1
package/11tyAutoCacheBuster.js
CHANGED
|
@@ -2,7 +2,10 @@ const fs = require("fs");
|
|
|
2
2
|
const crypto = require("crypto");
|
|
3
3
|
const glob = require("glob");
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
let enableLogging = false;
|
|
6
|
+
let algorithm = "md5";
|
|
7
|
+
|
|
8
|
+
function hash(content) {
|
|
6
9
|
const currentHash = crypto.createHash(algorithm);
|
|
7
10
|
currentHash.setEncoding("hex");
|
|
8
11
|
currentHash.write(content);
|
|
@@ -11,11 +14,15 @@ function hash(content, algorithm="md5") {
|
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
function logRegular(string) {
|
|
14
|
-
|
|
17
|
+
if (enableLogging) {
|
|
18
|
+
console.log(string);
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
function _logColour(string, colourCode) {
|
|
18
|
-
|
|
23
|
+
if (enableLogging) {
|
|
24
|
+
console.log(`\x1b[${colourCode}m${string} \x1b[0m`);
|
|
25
|
+
}
|
|
19
26
|
}
|
|
20
27
|
|
|
21
28
|
function logGreen(string) {
|
|
@@ -30,15 +37,31 @@ function logRed(string) {
|
|
|
30
37
|
_logColour(string, "31");
|
|
31
38
|
}
|
|
32
39
|
|
|
40
|
+
const defaultOptions = {
|
|
41
|
+
globstring: "**/*.{css,js,png,jpg,jpeg,gif,mp4,ico}",
|
|
42
|
+
enableLogging: enableLogging,
|
|
43
|
+
hashAlgorithm: algorithm,
|
|
44
|
+
hashFunction: hash,
|
|
45
|
+
}
|
|
33
46
|
|
|
34
|
-
module.exports = function(eleventyConfig,
|
|
47
|
+
module.exports = function(eleventyConfig, options=defaultOptions) {
|
|
35
48
|
eleventyConfig.on("eleventy.after", async ({ dir, results, runMode, outputMode }) => {
|
|
49
|
+
|
|
50
|
+
// Override default options with set options
|
|
51
|
+
options = Object.assign(defaultOptions, options);
|
|
52
|
+
|
|
53
|
+
// Set options to globals
|
|
54
|
+
enableLogging = options.enableLogging;
|
|
55
|
+
algorithm = options.hashAlgorithm;
|
|
56
|
+
const globstring = options.globstring;
|
|
57
|
+
const hashFunction = options.hashFunction;
|
|
58
|
+
|
|
36
59
|
const assetPaths = [];
|
|
37
60
|
logYellow(`[ACB] Collecting assets & calculating hashes using ${globstring}...`);
|
|
38
61
|
(await glob.glob(dir.output + "/" + globstring)).forEach((assetPath) => {
|
|
39
62
|
assetPath = assetPath.replace(/\\/g, "/")
|
|
40
63
|
logGreen(`[ACB] ${assetPath} is an asset! Calculating hash...`);
|
|
41
|
-
const assetHash =
|
|
64
|
+
const assetHash = hashFunction(fs.readFileSync(assetPath));
|
|
42
65
|
logGreen(`[ACB] ${assetPath} hash = ${assetHash}`);
|
|
43
66
|
|
|
44
67
|
assetPaths.push({
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Denperidge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Eleventy-Auto-Cache-Buster
|
|
2
|
+
An Eleventy content-hash based cache buster, not requiring any filters or code changes aside from adding the plugin in `.eleventy.js`!
|
|
3
|
+
|
|
4
|
+
Turn...
|
|
5
|
+
```html
|
|
6
|
+
<link rel="icon" type="image/x-icon" href="/favicon.ico">
|
|
7
|
+
<link rel="stylesheet" href="/stylesheet.css">
|
|
8
|
+
```
|
|
9
|
+
Into...
|
|
10
|
+
```html
|
|
11
|
+
<link rel="icon" type="image/x-icon" href="/favicon.ico?v=542610a0404a5b8a2f5459c0fc5b9691">
|
|
12
|
+
<link rel="stylesheet" href="/stylesheet.css?v=c9ca3e51cf7edc4c86c5fef68361957c">
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## How-to
|
|
16
|
+
### Getting started
|
|
17
|
+
1. Install the plugin
|
|
18
|
+
- Yarn: `yarn add -D eleventy-auto-cache-buster`
|
|
19
|
+
- Npm: `npm --save-dev eleventy-auto-cache-buster`
|
|
20
|
+
2. Enable the plugin
|
|
21
|
+
```js
|
|
22
|
+
// .eleventy.js
|
|
23
|
+
const eleventyAutoCacheBuster = require("eleventy-auto-cache-buster");
|
|
24
|
+
|
|
25
|
+
module.exports = function (eleventyConfig) {
|
|
26
|
+
eleventyConfig.addPlugin(eleventyAutoCacheBuster);
|
|
27
|
+
// ...
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
3. That's it! No further changes are required.
|
|
31
|
+
|
|
32
|
+
### Changing options
|
|
33
|
+
```js
|
|
34
|
+
// .eleventy.js
|
|
35
|
+
const eleventyAutoCacheBuster = require("eleventy-auto-cache-buster");
|
|
36
|
+
|
|
37
|
+
module.exports = function (eleventyConfig) {
|
|
38
|
+
eleventyConfig.addPlugin(eleventyAutoCacheBuster, {
|
|
39
|
+
enableLogging: true, // Whether to enable eleventy-auto-cache-buster logging.
|
|
40
|
+
globstring: "**/*.ico", // What glob string is used to locate assets.
|
|
41
|
+
hashAlgorithm: "sha1", // What hash method to pass to the hash function. See Node.js' crypto.createHash documentation.
|
|
42
|
+
hashFunction: function (content) { return "example" } // What function to run to calculate hashes. Overrides hashAlgorithm.
|
|
43
|
+
});
|
|
44
|
+
// ...
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
This project is licensed under the [MIT License](LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eleventy-auto-cache-buster",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Busts cache using ?v= with minimal configuration!",
|
|
5
5
|
"main": "11tyAutoCacheBuster.js",
|
|
6
6
|
"repository": "https://github.com/Denperidge/eleventy-auto-cache-buster.git",
|