eleventy-auto-cache-buster 0.7.0 → 0.8.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.
@@ -1,11 +1,11 @@
1
- const fs = require("fs");
2
- const path = require("path");
1
+ const fs = require("fs");
2
+ const path = require("path");
3
3
  const crypto = require("crypto");
4
- const glob = require("glob");
4
+ const glob = require("glob");
5
5
 
6
6
  let enableLogging = false;
7
- let algorithm = "md5";
8
- let hashTruncate = 12;
7
+ let algorithm = "md5";
8
+ let hashTruncate = 12;
9
9
  let hashFunction;
10
10
 
11
11
  function hash(content) {
@@ -41,13 +41,14 @@ function logRed(string) {
41
41
  }
42
42
 
43
43
  const defaultOptions = {
44
- globstring: "**/*",
45
- extensions: ["css", "js", "png", "jpg", "jpeg", "gif", "mp4", "ico"],
46
- hashTruncate: 12,
47
- runAsync: true,
44
+ globstring: "**/*",
45
+ globOptions: {nodir: true},
46
+ extensions: ["css", "js", "png", "jpg", "jpeg", "gif", "mp4", "ico"],
47
+ hashTruncate: 12,
48
+ runAsync: true,
48
49
  enableLogging: enableLogging,
49
50
  hashAlgorithm: algorithm,
50
- hashFunction: hash,
51
+ hashFunction: hash,
51
52
  }
52
53
 
53
54
  function collectLocalAssets(globResults=[], outputDir, extensions=defaultOptions.extensions) {
@@ -88,7 +89,7 @@ function writeAsync(outputPath, outputData) {
88
89
  }
89
90
 
90
91
  function replaceAssetsInFile(fileData, filePath, assetPathsAndHashes, writeFunc) {
91
- let outputString = fileData;
92
+ let outputString = fileData;
92
93
  let outputChanged = false; // Check if any hashes have been added
93
94
 
94
95
  // Check for every asset
@@ -113,20 +114,20 @@ function replaceAssetsInFile(fileData, filePath, assetPathsAndHashes, writeFunc)
113
114
  if (outputChanged) {
114
115
  writeFunc(filePath, outputString);
115
116
  }
116
-
117
117
  }
118
118
 
119
119
  module.exports = function(eleventyConfig, options=defaultOptions) {
120
120
  // Override default options with set options
121
- options = Object.assign(defaultOptions, options);
122
- const globstring = options.globstring;
123
- const extensions = options.extensions;
124
- const runAsync = options.runAsync;
121
+ options = Object.assign(defaultOptions, options, {
122
+ // Object.assign seems to overwrite nested objects. @emiliorcueto added the clever handling below
123
+ globOptions: Object.assign(defaultOptions.globOptions, options.globOptions) // -- ensure `nodir` is always set
124
+ });
125
+ const { globstring, globOptions, extensions, runAsync } = options;
125
126
  // Set options to globals
126
- enableLogging = options.enableLogging;
127
- hashTruncate = options.hashTruncate;
128
- hashFunction = options.hashFunction;
129
- algorithm = options.hashAlgorithm;
127
+ enableLogging = options.enableLogging;
128
+ hashTruncate = options.hashTruncate;
129
+ hashFunction = options.hashFunction;
130
+ algorithm = options.hashAlgorithm;
130
131
 
131
132
 
132
133
  // Setup
@@ -139,32 +140,35 @@ module.exports = function(eleventyConfig, options=defaultOptions) {
139
140
  if (runAsync) {
140
141
  eleventyConfig.on("eleventy.after", async ({ dir, results, runMode, outputMode }) => {
141
142
  logYellow(`[ACB] Collecting assets & calculating hashes using ${globstring}...`);
142
- const assetPathsAndHashes = collectLocalAssets(await glob.glob(dir.output + "/" + globstring, {nodir: true}), dir.output, extensions);
143
+ const assetPathsAndHashes = collectLocalAssets(await glob.glob(dir.output + "/" + globstring, globOptions), dir.output, extensions);
143
144
 
144
145
  logRegular(`[ACB] Replacing in output...`);
145
146
  results.forEach(({inputPath, outputPath, url, content}) => {
146
- fs.readFile(outputPath, encoding="UTF-8", (err, pageData) => {
147
- if (err) {
148
- logRed(err);
149
- throw err;
150
- }
151
- // Save the output data
152
- replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeAsync);
153
- });
147
+ if (!globOptions.ignore?.includes(outputPath)) { // -- Do not attempt to read explicitly ignored files as they may no longer exist!
148
+ fs.readFile(outputPath, encoding="UTF-8", (err, pageData) => {
149
+ if (err) {
150
+ logRed(err);
151
+ throw err;
152
+ }
153
+ // Save the output data
154
+ replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeAsync);
155
+ });
156
+ }
154
157
  });
155
158
  });
156
159
  } else {
157
160
  eleventyConfig.on("eleventy.after", ({ dir, results, runMode, outputMode }) => {
158
161
  logYellow(`[ACB] Collecting assets & calculating hashes using ${globstring}...`);
159
- const assetPathsAndHashes = collectLocalAssets(glob.globSync(dir.output + "/" + globstring, {nodir: true}), dir.output, extensions);
162
+ const assetPathsAndHashes = collectLocalAssets(glob.globSync(dir.output + "/" + globstring, globOptions), dir.output, extensions);
160
163
 
161
164
  logRegular(`[ACB] Replacing in output...`);
162
165
  results.forEach(({inputPath, outputPath, url, content}) => {
163
- const pageData = fs.readFileSync(outputPath, encoding="UTF-8");
164
- // Save the output data
165
- replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeSync);
166
+ if (!globOptions.ignore?.includes(outputPath)) { // -- Do not attempt to read explicitly ignored files as they may no longer exist!
167
+ const pageData = fs.readFileSync(outputPath, encoding="UTF-8");
168
+ // Save the output data
169
+ replaceAssetsInFile(pageData, outputPath, assetPathsAndHashes, writeSync);
170
+ }
166
171
  });
167
172
  });
168
173
  }
169
-
170
174
  }
package/README.md CHANGED
@@ -45,6 +45,7 @@ module.exports = function (eleventyConfig) {
45
45
  hashTruncate: 0, // Desired substring length of the hash. Set to 0 or lower to disable truncating
46
46
  runAsync: false, // Whether to run asynchronously. Default is true
47
47
  globstring: "subdir/**/*", // What glob string is used to locate assets.
48
+ globOptions: {nodir: true, ignore: ['node_modules/**', 'tmp/**']} // exclude multiple files or directories. (NOTE: `nodir` will automatically be set to true). Reference: https://github.com/isaacs/node-glob#readme
48
49
  extensions: ["css"], // What file extensions are accepted when locating assets.
49
50
  hashAlgorithm: "sha1", // What hash method to pass to the hash function. See Node.js' crypto.createHash documentation.
50
51
  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.7.0",
3
+ "version": "0.8.1",
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",