Skip to content

Commit

Permalink
Attempt to fix #122.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Aug 30, 2021
1 parent 764504f commit 2baf65a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
15 changes: 10 additions & 5 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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({
Expand Down
18 changes: 9 additions & 9 deletions test/test-markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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, `<img alt="Tiger" src="/img/W-k0C0bkvk-900.svg" width="900" height="900">`);
t.is(html, `<img alt="Tiger" src="/img/wGeeKEWkof-900.svg" width="900" height="900">`);
});

test("Filter out empty format arrays", async t => {
Expand Down

0 comments on commit 2baf65a

Please sign in to comment.