Skip to content

Commit

Permalink
feat(*): add support for inserting a *.ico file as a shortcut icon
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Feb 4, 2020
1 parent dd296e5 commit 1ad2017
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Insert assets such as .js, .css into an HTML file.

* .js files inserted at end of <body>
* .css files inserted at end of <head>
* .ico file inserted at end of <head>

## Examples

Expand Down
10 changes: 10 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function main(params, read = fs.readFileSync, write = mkdirpWrite, timestamp = D

const jsFiles = assets.filter(s => /\.m?js$/i.test(s));
const cssFiles = assets.filter(s => /\.css$/.test(s));
const icoFile = assets.filter(s => /\.ico$/.test(s))[0];

log("in: %s", inputFile);
log("out: %s", outputFile);
Expand Down Expand Up @@ -243,6 +244,15 @@ function main(params, read = fs.readFileSync, write = mkdirpWrite, timestamp = D
treeAdapter.appendChild(head, stylesheet);
}

if (icoFile) {
const icoLink = treeAdapter.createElement('link', undefined, [
{name: 'rel', value: 'shortcut icon'},
{name: 'type', value: 'image/ico'},
{name: 'href', value: toUrl(icoFile)},
]);
treeAdapter.appendChild(head, icoLink);
}

const content = parse5.serialize(document, {treeAdapter});
write(outputFile, content, {encoding: 'utf-8'});
return 0;
Expand Down
14 changes: 14 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ describe('HTML inserter', () => {
expect(output).toBe(
'<html><head><link rel="stylesheet" href="./path/to/my.css?v=123"></head><body></body></html>');
});

it('should inject .ico files as shortcut/icon link tags', () => {
expect(main(["--out", "index.html", "--html", inFile, '--assets', 'path/to/my.ico'], read, write, stamper)).toBe(0);
expect(output).toBe(
'<html><head><link rel="shortcut icon" type="image/ico" href="./path/to/my.ico?v=123"></head><body></body></html>');
});

it('should strip the longest matching prefix for .ico files', () => {
expect(main(["--out", "index.html", "--html", inFile,
"--roots", 'path', 'path/to',
'--assets', 'path/to/my.ico'], read, write, stamper)).toBe(0);
expect(output).toBe(
'<html><head><link rel="shortcut icon" type="image/ico" href="/my.ico?v=123"></head><body></body></html>');
});
});

describe("modules", () => {
Expand Down

0 comments on commit 1ad2017

Please sign in to comment.