eleventy-auto-cache-buster 0.5.0 → 0.6.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.
@@ -4,6 +4,8 @@ const glob = require("glob");
4
4
 
5
5
  let enableLogging = false;
6
6
  let algorithm = "md5";
7
+ let hashTruncate = 12;
8
+ let hashFunction;
7
9
 
8
10
  function hash(content) {
9
11
  const currentHash = crypto.createHash(algorithm);
@@ -40,83 +42,122 @@ function logRed(string) {
40
42
  const defaultOptions = {
41
43
  globstring: "**/*.{css,js,png,jpg,jpeg,gif,mp4,ico}",
42
44
  hashTruncate: 12,
45
+ runAsync: true,
43
46
  enableLogging: enableLogging,
44
47
  hashAlgorithm: algorithm,
45
48
  hashFunction: hash,
46
49
  }
47
50
 
48
- module.exports = function(eleventyConfig, options=defaultOptions) {
49
- eleventyConfig.on("eleventy.after", async ({ dir, results, runMode, outputMode }) => {
51
+ function collectLocalAssets(globResults=[], outputDir) {
52
+ const assetPaths = [];
53
+ globResults.forEach((assetPath) => {
54
+ assetPath = assetPath.replace(/\\/g, "/")
55
+ logGreen(`[ACB] ${assetPath} is an asset! Calculating hash...`);
56
+ const assetHash = hashFunction(fs.readFileSync(assetPath));
57
+ logGreen(`[ACB] ${assetPath} hash = ${assetHash}`);
58
+
59
+ assetPaths.push({
60
+ assetPath: assetPath.replace(outputDir + "/", ""),
61
+ assetHash: assetHash
62
+ });
63
+ });
64
+
65
+ logYellow(`[ACB] Collected all asset hashes!`);
66
+ return assetPaths;
67
+ }
68
+
69
+ function writeSync(outputPath, outputData) {
70
+ try {
71
+ fs.writeFileSync(outputPath, outputData);
72
+ logGreen(`[ACB] Added hashes to ${outputPath}`);
73
+ } catch (err) {
74
+ logRed(err);
75
+ }
76
+ }
77
+
78
+ function writeAsync(outputPath, outputData) {
79
+ fs.writeFile(outputPath, outputData, () => {
80
+ logGreen(`[ACB] Added hashes to ${outputPath}`);
81
+ });
82
+ }
50
83
 
51
- // Override default options with set options
52
- options = Object.assign(defaultOptions, options);
84
+ function replaceAssetsInFile(fileData, filePath, assetPathsAndHashes, writeFunc) {
85
+ let outputString = fileData;
86
+ let outputChanged = false; // Check if any hashes have been added
53
87
 
54
- const globstring = options.globstring;
55
- const hashTruncate = options.hashTruncate;
56
- const hashFunction = options.hashFunction;
57
- // Set options to globals
58
- enableLogging = options.enableLogging;
59
- algorithm = options.hashAlgorithm;
88
+ // Check for every asset
89
+ assetPathsAndHashes.forEach(({assetPath, assetHash}) => {
90
+ if (fileData.includes(assetPath)) {
91
+ logGreen(`[ACB] ${filePath} contains asset ${assetPath}`)
60
92
 
61
- if (hashTruncate > 0) {
62
- logRegular(`[ACB] Truncating hash to ${hashTruncate}`);
93
+ // Optionally truncate
94
+ if (hashTruncate > 0) {
95
+ assetHash = assetHash.substring(0, hashTruncate);
96
+ }
97
+
98
+ // Replace asset path with asset path with hash
99
+ outputString = outputString.replaceAll(assetPath, `${assetPath}?v=${assetHash}`);
100
+ // Write changes to file
101
+ outputChanged = true;
63
102
  } else {
64
- logRegular(`[ACB] hashTruncate smaller than or equal to 0, disabling truncation`);
103
+ logRegular(`[ACB] ${filePath} does NOT contain asset ${assetPath}. Skipping`)
65
104
  }
105
+ });
66
106
 
67
- const assetPaths = [];
68
- logYellow(`[ACB] Collecting assets & calculating hashes using ${globstring}...`);
69
- (await glob.glob(dir.output + "/" + globstring)).forEach((assetPath) => {
70
- assetPath = assetPath.replace(/\\/g, "/")
71
- logGreen(`[ACB] ${assetPath} is an asset! Calculating hash...`);
72
- const assetHash = hashFunction(fs.readFileSync(assetPath));
73
- logGreen(`[ACB] ${assetPath} hash = ${assetHash}`);
74
-
75
- assetPaths.push({
76
- assetPath: assetPath.replace(dir.output + "/", ""),
77
- assetHash: assetHash
78
- });
79
- });
107
+ if (outputChanged) {
108
+ writeFunc(filePath, outputString);
109
+ }
80
110
 
81
- logYellow(`[ACB] Collected all asset hashes!`);
82
- logRegular(`[ACB] Replacing in output...`);
111
+ }
83
112
 
84
- // For every page Eleventy outputs
85
- results.forEach(({inputPath, outputPath, url, content}) => {
86
- let outputData = ""; // Assigned later
87
- let outputChanged = false; // Check if any hashes have been added
113
+ module.exports = function(eleventyConfig, options=defaultOptions) {
114
+ // Override default options with set options
115
+ options = Object.assign(defaultOptions, options);
116
+ const globstring = options.globstring;
117
+ const runAsync = options.runAsync;
118
+ // Set options to globals
119
+ enableLogging = options.enableLogging;
120
+ hashTruncate = options.hashTruncate;
121
+ hashFunction = options.hashFunction;
122
+ algorithm = options.hashAlgorithm;
123
+
124
+
125
+ // Setup
126
+ if (hashTruncate > 0) {
127
+ logRegular(`[ACB] Truncating hash to ${hashTruncate}`);
128
+ } else {
129
+ logRegular(`[ACB] hashTruncate smaller than or equal to 0, disabling truncation`);
130
+ }
88
131
 
89
- // Read the output content
90
- fs.readFile(outputPath, encoding="UTF-8", (err, pageData) => {
91
- if (err) {
92
- logRed(err);
93
- throw err;
94
- }
95
- // Save the output data
96
- outputData = pageData;
97
-
98
- assetPaths.forEach(({assetPath, assetHash}) => {
99
- if (pageData.includes(assetPath)) {
100
- logGreen(`[ACB] ${outputPath} contains asset ${assetPath}`)
101
-
102
- if (hashTruncate > 0) {
103
- assetHash = assetHash.substring(0, hashTruncate);
104
- }
105
-
106
- outputData = outputData.replaceAll(assetPath, `${assetPath}?v=${assetHash}`);
107
- outputChanged = true;
108
-
109
- } else {
110
- logRegular(`[ACB] ${outputPath} does NOT contain asset ${assetPath}. Skipping`)
132
+ if (runAsync) {
133
+ eleventyConfig.on("eleventy.after", async ({ dir, results, runMode, outputMode }) => {
134
+ logYellow(`[ACB] Collecting assets & calculating hashes using ${globstring}...`);
135
+ const assetPathsAndHashes = collectLocalAssets(await glob.glob(dir.output + "/" + globstring), dir.output);
136
+
137
+ logRegular(`[ACB] Replacing in output...`);
138
+ results.forEach(({inputPath, outputPath, url, content}) => {
139
+ fs.readFile(outputPath, encoding="UTF-8", (err, pageData) => {
140
+ if (err) {
141
+ logRed(err);
142
+ throw err;
111
143
  }
144
+ // Save the output data
145
+ replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeAsync);
112
146
  });
113
-
114
- if (outputChanged) {
115
- fs.writeFile(outputPath, outputData, () => {
116
- logGreen(`[ACB] Added hashes to ${outputPath}`);
117
- });
118
- }
119
147
  });
120
148
  });
121
- });
149
+ } else {
150
+ eleventyConfig.on("eleventy.after", ({ dir, results, runMode, outputMode }) => {
151
+ logYellow(`[ACB] Collecting assets & calculating hashes using ${globstring}...`);
152
+ const assetPathsAndHashes = collectLocalAssets(glob.globSync(dir.output + "/" + globstring), dir.output);
153
+
154
+ logRegular(`[ACB] Replacing in output...`);
155
+ results.forEach(({inputPath, outputPath, url, content}) => {
156
+ const pageData = fs.readFileSync(outputPath, encoding="UTF-8");
157
+ // Save the output data
158
+ replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeSync);
159
+ });
160
+ });
161
+ }
162
+
122
163
  }
package/README.md CHANGED
@@ -43,6 +43,7 @@ module.exports = function (eleventyConfig) {
43
43
  eleventyConfig.addPlugin(eleventyAutoCacheBuster, {
44
44
  enableLogging: true, // Whether to enable eleventy-auto-cache-buster logging.
45
45
  hashTruncate: 0, // Desired substring length of the hash. Set to 0 or lower to disable truncating
46
+ runAsync: false, // Whether to run asynchronously. Default is true
46
47
  globstring: "**/*.ico", // What glob string is used to locate assets.
47
48
  hashAlgorithm: "sha1", // What hash method to pass to the hash function. See Node.js' crypto.createHash documentation.
48
49
  hashFunction: function (content) { return "example" } // What function to run to calculate hashes. Overrides hashAlgorithm.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eleventy-auto-cache-buster",
3
- "version": "0.5.0",
3
+ "version": "0.6.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",