eleventy-auto-cache-buster 0.8.0 → 0.8.2

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.
@@ -94,19 +94,35 @@ function replaceAssetsInFile(fileData, filePath, assetPathsAndHashes, writeFunc)
94
94
 
95
95
  // Check for every asset
96
96
  assetPathsAndHashes.forEach(({assetPath, assetHash}) => {
97
- if (fileData.includes(assetPath)) {
98
- logGreen(`[ACB] ${filePath} contains asset ${assetPath}`)
97
+ let found = false;
98
+ let indexPush = 0; // With values being replaced, the index values will need to be adjusted
99
+ const regex = new RegExp(assetPath.replaceAll("/", "\\/").replaceAll("?", "\\?") + '.*?(?=")', "g")
100
+ const matches = outputString.matchAll(regex);
101
+
102
+ while ((match = matches.next()).done != true) {
103
+ found = true;
104
+ const value = match.value
105
+ const path = value[0]
106
+ logGreen(`[ACB] ${filePath} contains asset ${assetPath} (matched on: ${path})`)
99
107
 
100
108
  // Optionally truncate
101
109
  if (hashTruncate > 0) {
102
110
  assetHash = assetHash.substring(0, hashTruncate);
103
111
  }
104
112
 
113
+ const seperator = path.includes("?") ? "&" : "?";
114
+ const newPath = `${path}${seperator}v=${assetHash}`;
105
115
  // Replace asset path with asset path with hash
106
- outputString = outputString.replaceAll(assetPath, `${assetPath}?v=${assetHash}`);
116
+ outputString = outputString.substring(0, value.index + indexPush)
117
+ + newPath
118
+ + outputString.substring(value.index + indexPush + path.length)
119
+
120
+ indexPush += newPath.length - path.length;
121
+
107
122
  // Write changes to file
108
123
  outputChanged = true;
109
- } else {
124
+ }
125
+ if (!found) {
110
126
  logRegular(`[ACB] ${filePath} does NOT contain asset ${assetPath}. Skipping`)
111
127
  }
112
128
  });
@@ -144,14 +160,16 @@ module.exports = function(eleventyConfig, options=defaultOptions) {
144
160
 
145
161
  logRegular(`[ACB] Replacing in output...`);
146
162
  results.forEach(({inputPath, outputPath, url, content}) => {
147
- fs.readFile(outputPath, encoding="UTF-8", (err, pageData) => {
148
- if (err) {
149
- logRed(err);
150
- throw err;
151
- }
152
- // Save the output data
153
- replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeAsync);
154
- });
163
+ if (!globOptions.ignore?.includes(outputPath)) { // -- Do not attempt to read explicitly ignored files as they may no longer exist!
164
+ fs.readFile(outputPath, encoding="UTF-8", (err, pageData) => {
165
+ if (err) {
166
+ logRed(err);
167
+ throw err;
168
+ }
169
+ // Save the output data
170
+ replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeAsync);
171
+ });
172
+ }
155
173
  });
156
174
  });
157
175
  } else {
@@ -161,9 +179,11 @@ module.exports = function(eleventyConfig, options=defaultOptions) {
161
179
 
162
180
  logRegular(`[ACB] Replacing in output...`);
163
181
  results.forEach(({inputPath, outputPath, url, content}) => {
164
- const pageData = fs.readFileSync(outputPath, encoding="UTF-8");
165
- // Save the output data
166
- replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeSync);
182
+ if (!globOptions.ignore?.includes(outputPath)) { // -- Do not attempt to read explicitly ignored files as they may no longer exist!
183
+ const pageData = fs.readFileSync(outputPath, encoding="UTF-8");
184
+ // Save the output data
185
+ replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeSync);
186
+ }
167
187
  });
168
188
  });
169
189
  }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "eleventy-auto-cache-buster",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
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",
7
7
  "author": "Denperidge",
8
8
  "license": "MIT",
9
9
  "scripts": {
10
+ "start": "pushd tests && RUNASYNC=0 USESERVE=1 HASHTRUNCATE=16 eleventy && popd",
10
11
  "test": "ava tests/test.mjs --timeout=30s"
11
12
  },
12
13
  "dependencies": {