From 2baf65a6ca561b7dd4c6269eaa3a342998e01824 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Sun, 29 Aug 2021 21:26:27 -0500 Subject: [PATCH] Attempt to fix #122. --- img.js | 15 ++++++++++----- test/test-markup.js | 18 +++++++++--------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/img.js b/img.js index a9d0926..28be182 100644 --- a/img.js +++ b/img.js @@ -125,6 +125,7 @@ class Image { opts.sourceUrl = this.src.toString(); opts.__originalSize = this.src.length; // used for hashing } else { + // TODO @zachleat (multiread): another read opts.__originalSize = fs.statSync(this.src).size; // used for hashing } @@ -180,12 +181,16 @@ class Image { } // format is only required for local files - static getHash(src, options = {}, length = 10) { + static getHash(src, format, options = {}, length = 10) { let hash = createHash("sha256"); if(fs.existsSync(src)) { - // TODO probably need a cache here + // TODO @zachleat (multiread): probably need a cache here let fileContent = fs.readFileSync(src); + if(format === "svg") { + // remove all newlines for hashing for better cross-OS hash compatibility (#122) + fileContent = fileContent.toString().replace(/\r|\n/g, ''); + } hash.update(fileContent); } else { // probably a remote URL @@ -262,7 +267,7 @@ class Image { return this.assetCache.fetch(this.cacheOptions); } - // TODO read local file contents here and always return a buffer + // TODO @zachleat (multiread): read local file contents here and always return a buffer return this.src; } @@ -335,7 +340,7 @@ class Image { if(this.options.useCache && fs.existsSync(stat.outputPath)){ stat.size = fs.statSync(stat.outputPath).size; if(this.options.dryRun) { - // TODO get rid of this duplicate read when input is always a buffer (see TODO in getInput) + // TODO @zachleat (multiread): get rid of this duplicate read when input is always a buffer (see TODO in getInput) stat.buffer = fs.readFileSync(this.src); } @@ -466,7 +471,7 @@ class ImageStat { let outputFilename; let outputExtension = options.extensions[format] || format; - let id = Image.getHash(src, options); + let id = Image.getHash(src, format, options); if(options.urlFormat && typeof options.urlFormat === "function") { url = options.urlFormat({ diff --git a/test/test-markup.js b/test/test-markup.js index d3584f8..6d22b91 100644 --- a/test/test-markup.js +++ b/test/test-markup.js @@ -167,13 +167,13 @@ test("Image markup (defaults, inlined)", async t => { }); test("svgShortCircuit and generateHTML: Issue #48", async t => { - let img = new eleventyImage.Image("./test/Ghostscript_Tiger.svg", { - formats: ["webp", "png", "svg"], - svgShortCircuit: true, - dryRun: true, - }); - let svgStats = eleventyImage.ImageStat.getStat("./test/Ghostscript_Tiger.svg", "svg", "/img/", 900, 900, img.options); - console.log( svgStats ); + // let img = new eleventyImage.Image("./test/Ghostscript_Tiger.svg", { + // formats: ["webp", "png", "svg"], + // svgShortCircuit: true, + // dryRun: true, + // }); + // let svgStats = eleventyImage.ImageStat.getStat("./test/Ghostscript_Tiger.svg", "svg", "/img/", 900, 900, img.options); + // console.log( svgStats ); let stats = await eleventyImage("./test/Ghostscript_Tiger.svg", { formats: ["webp", "png", "svg"], @@ -183,12 +183,12 @@ test("svgShortCircuit and generateHTML: Issue #48", async t => { t.is(stats.svg.length, 1); t.is(stats.webp.length, 0); t.is(stats.png.length, 0); - t.is(stats.svg[0].url, "/img/W-k0C0bkvk-900.svg"); + t.is(stats.svg[0].url, "/img/wGeeKEWkof-900.svg"); let html = eleventyImage.generateHTML(stats, { alt: "Tiger", }); - t.is(html, `Tiger`); + t.is(html, `Tiger`); }); test("Filter out empty format arrays", async t => {