-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
74 lines (62 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const render = require('favicon-emoji/lib/render');
const toIco = require('to-ico');
const emojiUnicode = require('emoji-unicode');
const emojiNameMap = require('emoji-name-map');
const fs = require('pn/fs');
const {convert} = require('convert-svg-to-png');
const isShortcode = /^:?[a-z0-9_]+:?$/;
async function generatePngs(options) {
let emoji = typeof options === 'string' ? options : options.emoji;
if (isShortcode.test(emoji)) {
emoji = emojiNameMap.get(emoji);
}
const sizes = options.sizes || [16, 32, 48];
if (options.useSystem) {
return render(emoji, sizes);
}
const unicode = emojiUnicode(emoji).replace(/\s+/g, '-');
const svg = await fs.readFile(
require.resolve(`twemoji/2/svg/${unicode}.svg`)
);
return Promise.all(
sizes.map(size =>
convert(svg, {
width: size,
height: size
})
)
);
}
class EmojiFaviconPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.hooks.make.tapAsync(
'EmojiFaviconPlugin',
async (compilation, callback) => {
const pngs = await generatePngs(this.options);
const ico = await toIco(pngs);
compilation.assets['favicon.ico'] = {
source: () => ico,
size: () => ico.length
};
if (compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing) {
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync(
'EmojiFaviconPlugin',
(htmlPluginData, callback) => {
const publicPath = compilation.outputOptions.publicPath || '';
htmlPluginData.html = htmlPluginData.html.replace(
/(<\/head>)/i,
`<link rel="shortcut icon" href="${publicPath}favicon.ico">$&`
);
callback(null, htmlPluginData);
}
);
}
callback();
}
);
}
}
module.exports = EmojiFaviconPlugin;