Skip to content

Commit

Permalink
feat: allow adding a hash in generated font file name (@brunoroux)
Browse files Browse the repository at this point in the history
Merge branch 'add_hash_generated_filename' of https://github.com/brunoroux/webfont into brunoroux-add_hash_generated_filename
  • Loading branch information
Jimmy Andrade committed Sep 29, 2020
2 parents 9206e72 + c7f6882 commit 1913c9a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/standalone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,16 @@ describe("standalone", () => {
expect(Array.isArray(result.glyphsData)).toBe(true);
expect(result.glyphsData.length > 0).toBe(true);
});

it("should export `hash` in `result`", () => {
expect.assertions(1);

return standalone({
files: `${fixturesGlob}/svg-icons/**/*`
}).then(result => {
expect(result.hash).toBe("ead2b6f69fc603bf1cbd00bf9f8a8a33");

return result;
});
});
});
14 changes: 14 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ const cli = meow(
--metadata
Content of the metadata tag.
--add-hash-in-font-url
Generated font url will be : [webfont].[ext]?v=[hash]
`,
{
autoHelp: false,
Expand Down Expand Up @@ -215,6 +219,10 @@ const cli = meow(
alias: "p",
type: "string"
},
"add-hash-in-font-url": {
default: false,
type: "boolean"
},
verbose: {
default: false,
type: "boolean"
Expand Down Expand Up @@ -333,6 +341,10 @@ if (cli.flags.sort === false) {
optionsBase.sort = cli.flags.sort;
}

if (cli.flags.addHashInFontUrl) {
optionsBase.addHashInFontUrl = cli.flags.addHashInFontUrl;
}

if (cli.flags.help || cli.flags.h) {
cli.showHelp();
}
Expand Down Expand Up @@ -387,6 +399,8 @@ Promise.resolve()
path.basename(result.config.template).replace(".njk", "")
);
}

delete result.hash;
}

return Promise.resolve()
Expand Down
10 changes: 9 additions & 1 deletion src/standalone.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Readable } from "stream";
import fs from "fs";
import path from "path";
import crypto from "crypto";
import SVGIcons2SVGFontStream from "svgicons2svgfont";
import cosmiconfig from "cosmiconfig";
import pLimit from "p-limit";
Expand Down Expand Up @@ -244,6 +245,11 @@ export default async function(initialOptions) {
: {}
);

result.hash = crypto
.createHash("md5")
.update(result.svg)
.digest("hex");

if (options.formats.includes("eot")) {
result.eot = toEot(result.ttf);
}
Expand Down Expand Up @@ -283,6 +289,7 @@ export default async function(initialOptions) {
templateFilePath = path.resolve(resolvedTemplateFilePath);
}

const hashOption = options.addHashInFontUrl ? { hash: result.hash } : {};
const nunjucksOptions = deepmerge.all([
{
glyphs: result.glyphsData.map(glyphData => {
Expand All @@ -298,7 +305,8 @@ export default async function(initialOptions) {
className: options.templateClassName || options.fontName,
fontName: options.templateFontName || options.fontName,
fontPath: options.templateFontPath.replace(/\/?$/, "/")
}
},
hashOption
]);

result.template = nunjucks.render(templateFilePath, nunjucksOptions);
Expand Down
12 changes: 6 additions & 6 deletions templates/template.css.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
font-weight: 400;
font-display: auto;
{% if formats.indexOf('eot')>-1 -%}
src: url("{{ fontPath }}{{ fontName }}.eot");
src: url("{{ fontPath }}{{ fontName }}.eot{% if hash %}?v={{ hash }}{% endif %}");
{%- endif %}
{%- set eotIndex = formats.indexOf('eot') -%}
{%- set woff2Index = formats.indexOf('woff2') -%}
{%- set woffIndex = formats.indexOf('woff') -%}
{%- set ttfIndex = formats.indexOf('ttf') -%}
{%- set svgIndex = formats.indexOf('svg') %}
src: {% if eotIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.eot?#iefix") format("embedded-opentype")
url("{{ fontPath }}{{ fontName }}.eot?{% if hash %}v={{ hash }}{% endif %}#iefix") format("embedded-opentype")
{%- set nothing = formats.splice(eotIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if woff2Index != -1 -%}
url("{{ fontPath }}{{ fontName }}.woff2") format("woff2")
url("{{ fontPath }}{{ fontName }}.woff2{% if hash %}?v={{ hash }}{% endif %}") format("woff2")
{%- set nothing = formats.splice(woff2Index, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if woffIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.woff") format("woff")
url("{{ fontPath }}{{ fontName }}.woff{% if hash %}?v={{ hash }}{% endif %}") format("woff")
{%- set nothing = formats.splice(woffIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if ttfIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.ttf") format("truetype")
url("{{ fontPath }}{{ fontName }}.ttf{% if hash %}?v={{ hash }}{% endif %}") format("truetype")
{%- set nothing = formats.splice(ttfIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if svgIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.svg#{{ fontName }}") format("svg");
url("{{ fontPath }}{{ fontName }}.svg{% if hash %}?v={{ hash }}{% endif %}#{{ fontName }}") format("svg");
{%- endif %}
}

Expand Down
12 changes: 6 additions & 6 deletions templates/template.html.njk
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,35 @@
font-weight: 400;
font-display: auto;
{% if formats.indexOf('eot')>-1 -%}
src: url("{{ fontPath }}{{ fontName }}.eot");
src: url("{{ fontPath }}{{ fontName }}.eot{% if hash %}?v={{ hash }}{% endif %}");
{%- endif %}
{%- set eotIndex = formats.indexOf('eot') -%}
{%- set woff2Index = formats.indexOf('woff2') -%}
{%- set woffIndex = formats.indexOf('woff') -%}
{%- set ttfIndex = formats.indexOf('ttf') -%}
{%- set svgIndex = formats.indexOf('svg') %}
src: {% if eotIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.eot?#iefix") format("embedded-opentype")
url("{{ fontPath }}{{ fontName }}.eot?{% if hash %}v={{ hash }}{% endif %}#iefix") format("embedded-opentype")
{%- set nothing = formats.splice(eotIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if woff2Index != -1 -%}
url("{{ fontPath }}{{ fontName }}.woff2") format("woff2")
url("{{ fontPath }}{{ fontName }}.woff2{% if hash %}?v={{ hash }}{% endif %}") format("woff2")
{%- set nothing = formats.splice(woff2Index, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if woffIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.woff") format("woff")
url("{{ fontPath }}{{ fontName }}.woff{% if hash %}?v={{ hash }}{% endif %}") format("woff")
{%- set nothing = formats.splice(woffIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if ttfIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.ttf") format("truetype")
url("{{ fontPath }}{{ fontName }}.ttf{% if hash %}?v={{ hash }}{% endif %}") format("truetype")
{%- set nothing = formats.splice(ttfIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if svgIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.svg#{{ fontName }}") format("svg");
url("{{ fontPath }}{{ fontName }}.svg{% if hash %}?v={{ hash }}{% endif %}#{{ fontName }}") format("svg");
{%- endif %}
}
Expand Down
12 changes: 6 additions & 6 deletions templates/template.scss.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@
font-weight: 400;
font-display: auto;
{% if formats.indexOf('eot')>-1 -%}
src: url("{{ fontPath }}{{ fontName }}.eot");
src: url("{{ fontPath }}{{ fontName }}.eot{% if hash %}?v={{ hash }}{% endif %}");
{%- endif -%}
{%- set eotIndex = formats.indexOf('eot') -%}
{%- set woff2Index = formats.indexOf('woff2') -%}
{%- set woffIndex = formats.indexOf('woff') -%}
{%- set ttfIndex = formats.indexOf('ttf') -%}
{%- set svgIndex = formats.indexOf('svg') %}
src: {% if eotIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.eot?#iefix") format("embedded-opentype")
url("{{ fontPath }}{{ fontName }}.eot?{% if hash %}v={{ hash }}{% endif %}#iefix") format("embedded-opentype")
{%- set nothing = formats.splice(eotIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if woff2Index != -1 -%}
url("{{ fontPath }}{{ fontName }}.woff2") format("woff2")
url("{{ fontPath }}{{ fontName }}.woff2{% if hash %}?v={{ hash }}{% endif %}") format("woff2")
{%- set nothing = formats.splice(woff2Index, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if woffIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.woff") format("woff")
url("{{ fontPath }}{{ fontName }}.woff{% if hash %}?v={{ hash }}{% endif %}") format("woff")
{%- set nothing = formats.splice(woffIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if ttfIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.ttf") format("truetype")
url("{{ fontPath }}{{ fontName }}.ttf{% if hash %}?v={{ hash }}{% endif %}") format("truetype")
{%- set nothing = formats.splice(ttfIndex, 1) -%}
{%- if formats.length != 0 -%}, {% else -%}; {% endif -%}
{%- endif -%}
{%- if svgIndex != -1 -%}
url("{{ fontPath }}{{ fontName }}.svg#{{ fontName }}") format("svg");
url("{{ fontPath }}{{ fontName }}.svg{% if hash %}?v={{ hash }}{% endif %}#{{ fontName }}") format("svg");
{%- endif %}
}

Expand Down

0 comments on commit 1913c9a

Please sign in to comment.