@@ -190,23 +190,18 @@ export function collectLocalAssets(globResults=[], outputDir, extensions=default
190190}
191191
192192/**
193- * Check if any of the assets are found in the passed file content/path
193+ * Check if any of the assets are found in the passed file content,
194194 * and cache bust them as necessary.
195- * If any assets are found, write the new contents to the passed file path
196- *
197- * Its peculiar design (.e.g. having to pass a write function & fileData)
198- * allows it to be used both for sync & async purposes
199- *
195+ * If any cache busting took place, return the cache busted string.
196+ * Else, return null
197+ * *
200198 * @see collectLocalAssets
201- * @see writeAsync
202- * @see writeSync
203199 *
204200 * @param {string } fileData original file contents (For example: "File\nContent")
205- * @param {string } filePath path to write to (For example "path/file.json")
206- * @param {Array<{assetPath: string, assetHash: string}> }} assetPathsAndHashes output from collectLocalAssets
207- * @param {function } writeFunc write function to use
201+ * @param {string } filePath file path that will be writen to (For example "path/file.json"). This is for logging purposes
202+ * @param {Array<{assetPath: string, assetHash: string}> } assetPathsAndHashes output from collectLocalAssets
208203 */
209- export function replaceAssetsInFile ( fileData , filePath , assetPathsAndHashes , writeFunc ) {
204+ export function replaceAssetsInString ( fileData , filePath , assetPathsAndHashes ) {
210205 let outputString = fileData ;
211206 let outputChanged = false ; // Check if any hashes have been added
212207 assetPathsAndHashes . forEach ( ( { assetPath, assetHash} ) => {
@@ -231,7 +226,9 @@ export function replaceAssetsInFile(fileData, filePath, assetPathsAndHashes, wri
231226 }
232227 } )
233228 if ( outputChanged ) {
234- writeFunc ( filePath , outputString ) ;
229+ return outputString ;
230+ } else {
231+ return null ;
235232 }
236233}
237234
@@ -270,8 +267,11 @@ export default function(eleventyConfig, options=defaultOptions) {
270267 logRed ( err ) ;
271268 throw err ;
272269 }
273- // Save the output data
274- replaceAssetsInFile ( pageData , outputPath , assetPathsAndHashes , writeAsync ) ;
270+ // If any cache busting took place, save the cache busted string to the file
271+ const cachebustedContents = replaceAssetsInString ( pageData , outputPath , assetPathsAndHashes , writeAsync ) ;
272+ if ( cachebustedContents ) {
273+ writeAsync ( outputPath , cachebustedContents ) ;
274+ }
275275 } ) ;
276276 }
277277 } ) ;
@@ -286,8 +286,11 @@ export default function(eleventyConfig, options=defaultOptions) {
286286 results . forEach ( ( { inputPath, outputPath, url, content} ) => {
287287 if ( ! globOptions . ignore ?. includes ( outputPath ) ) { // -- Do not attempt to read explicitly ignored files as they may no longer exist!
288288 const pageData = fs . readFileSync ( outputPath , { encoding : "UTF-8" } ) ;
289- // Save the output data
290- replaceAssetsInFile ( pageData , outputPath , assetPathsAndHashes , writeSync ) ;
289+ // If any cache busting took place, save the cache busted string to the file
290+ const cachebustedContents = replaceAssetsInString ( pageData , outputPath , assetPathsAndHashes ) ;
291+ if ( cachebustedContents ) {
292+ writeSync ( outputPath , cachebustedContents )
293+ }
291294 }
292295 } ) ;
293296 } ) ;
0 commit comments