favgen 0.0.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +9 -12
- package/bin/index.js +3 -5
- package/lib/generator.d.ts +1 -1
- package/lib/generator.js +34 -42
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
package/README.md
CHANGED
|
@@ -5,21 +5,20 @@ This is a simple CLI tool to generate an optimized set of favicons from a single
|
|
|
5
5
|
Use it like this: `npx favgen /path/to/input -o /path/to/output`.
|
|
6
6
|
|
|
7
7
|
You can tweak the following settings by giving additional commands:
|
|
8
|
-
- output directory by providing `-o` option with a path (`
|
|
9
|
-
-
|
|
10
|
-
- colors palette size by providing `--colors` with a number between 2 and 256
|
|
11
|
-
- producing 16x16 .ico file by setting `--include16` flag
|
|
8
|
+
- output directory by providing `-o` option with a path (`__favicons__` by default)
|
|
9
|
+
- colors palette size by providing `--colors` with a number between 2 and 256 (64 by default)
|
|
12
10
|
|
|
13
11
|
Input file can be in any of the following formats: JPEG, PNG, WebP, GIF, AVIF, TIFF or SVG (anything [sharp library](https://sharp.pixelplumbing.com/) accepts).
|
|
14
12
|
|
|
15
13
|
By default, the following set of favicons is produced:
|
|
16
|
-
- `
|
|
14
|
+
- `icon.svg` (only if input file is SVG)
|
|
17
15
|
- `favicon.ico` 32x32
|
|
18
|
-
- `
|
|
19
|
-
- `
|
|
16
|
+
- `icon-192.png` 192x192 (for Android devices)
|
|
17
|
+
- `icon-512.png` 512x512 (for Android devices)
|
|
18
|
+
- `icon-mask.png` 512x512 with safe padding (for Android maskable icon support)
|
|
20
19
|
- `apple-touch-icon.png` 180x180 (original image is resized to 140x140 and 20px transparent padding is added on each side; rationale for this is given in the article)
|
|
21
20
|
|
|
22
|
-
Additionally, a sample `manifest.webmanifest` file is produced which
|
|
21
|
+
Additionally, a sample `manifest.webmanifest` file is produced which includes regular and maskable Android icons.
|
|
23
22
|
|
|
24
23
|
Besides that, PNG output is optimized by `sharp` (which uses `pngquant`) and SVG output is optimized by [SVGO](https://github.com/svg/svgo).
|
|
25
24
|
Also, color palette is reduced to 64 colors by default in order to reduce assets’ size.
|
|
@@ -27,10 +26,8 @@ Also, color palette is reduced to 64 colors by default in order to reduce assets
|
|
|
27
26
|
The tool can be also used as API:
|
|
28
27
|
```js
|
|
29
28
|
const { produceIcons } = require("favgen")
|
|
30
|
-
const inputFilePath = "
|
|
29
|
+
const inputFilePath = "icon.svg"
|
|
31
30
|
const outputDirPath = "__favicons__"
|
|
32
|
-
const prefix = "favicon" // default value
|
|
33
31
|
const paletteSize = 64 // default value
|
|
34
|
-
|
|
35
|
-
produceIcons(inputFilePath, outputDirPath, prefix, paletteSize, include16)
|
|
32
|
+
produceIcons(inputFilePath, outputDirPath, paletteSize)
|
|
36
33
|
```
|
package/bin/index.js
CHANGED
|
@@ -15,10 +15,8 @@ program
|
|
|
15
15
|
.description("Produce a set of favicons from a single input file.")
|
|
16
16
|
.argument("<inputPath>", "Input icon path")
|
|
17
17
|
.option("-o, --output <path>", "Output directory path", "__favicons__")
|
|
18
|
-
.option("--prefix <name>", "Icon prefix", "favicon")
|
|
19
18
|
.option("--colors <number>", "Color paleete size, between 2 and 256", 64)
|
|
20
|
-
.
|
|
21
|
-
.action((filepath, { output: outputDir, prefix, colors, include16 }) => {
|
|
19
|
+
.action(async (filepath, { output: outputDir, colors }) => {
|
|
22
20
|
const colorsPaletteSize = parseInt(colors, 10);
|
|
23
21
|
const isValidPaletteSize =
|
|
24
22
|
Number.isNaN(colorsPaletteSize) ||
|
|
@@ -30,11 +28,11 @@ program
|
|
|
30
28
|
);
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
const inputPath = path.join(CWD, filepath);
|
|
31
|
+
const inputPath = path.isAbsolute(filepath) ? filepath : path.join(CWD, filepath);
|
|
34
32
|
const outputPath = path.isAbsolute(outputDir)
|
|
35
33
|
? outputDir
|
|
36
34
|
: path.join(CWD, outputDir);
|
|
37
|
-
produceIcons(inputPath, outputPath,
|
|
35
|
+
await produceIcons(inputPath, outputPath, colorsPaletteSize);
|
|
38
36
|
});
|
|
39
37
|
|
|
40
38
|
program.parse();
|
package/lib/generator.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare function produceIcons(inputFilePath: string, outputDirPath: string,
|
|
1
|
+
declare function produceIcons(inputFilePath: string, outputDirPath: string, paletteSize?: number): Promise<void>;
|
|
2
2
|
export default produceIcons;
|
package/lib/generator.js
CHANGED
|
@@ -11,7 +11,7 @@ const svgo_1 = require("svgo");
|
|
|
11
11
|
const is_svg_1 = __importDefault(require("is-svg"));
|
|
12
12
|
const baseIconConfigs = [
|
|
13
13
|
{
|
|
14
|
-
name: "
|
|
14
|
+
name: "icon.svg",
|
|
15
15
|
},
|
|
16
16
|
{
|
|
17
17
|
// no colors palette size otherwise its color profile is off
|
|
@@ -20,15 +20,22 @@ const baseIconConfigs = [
|
|
|
20
20
|
pxSize: 32,
|
|
21
21
|
},
|
|
22
22
|
{
|
|
23
|
-
name: "
|
|
23
|
+
name: "icon-192.png",
|
|
24
24
|
pxSize: 192,
|
|
25
25
|
colorsPaletteSize: 64,
|
|
26
26
|
},
|
|
27
27
|
{
|
|
28
|
-
name: "
|
|
28
|
+
name: "icon-512.png",
|
|
29
29
|
pxSize: 512,
|
|
30
30
|
colorsPaletteSize: 64,
|
|
31
31
|
},
|
|
32
|
+
{
|
|
33
|
+
name: "icon-mask.png",
|
|
34
|
+
pxSize: 512,
|
|
35
|
+
colorsPaletteSize: 64,
|
|
36
|
+
// 512 - (52 * 2) = 408 which stays in the recommended 409px safe zone
|
|
37
|
+
padding: 52,
|
|
38
|
+
},
|
|
32
39
|
{
|
|
33
40
|
name: "apple-touch-icon.png",
|
|
34
41
|
pxSize: 180,
|
|
@@ -37,8 +44,15 @@ const baseIconConfigs = [
|
|
|
37
44
|
},
|
|
38
45
|
];
|
|
39
46
|
function buildPng(rawBuffer, { pxSize, colorsPaletteSize, padding }) {
|
|
47
|
+
if (pxSize === undefined) {
|
|
48
|
+
throw Error("PNG output size is not defined.");
|
|
49
|
+
}
|
|
50
|
+
const resizePxSize = padding ? pxSize - padding * 2 : pxSize;
|
|
51
|
+
if (resizePxSize <= 0) {
|
|
52
|
+
throw Error("Padding is too large for the output size.");
|
|
53
|
+
}
|
|
40
54
|
const outputIcon = (0, sharp_1.default)(rawBuffer)
|
|
41
|
-
.resize(
|
|
55
|
+
.resize(resizePxSize, resizePxSize)
|
|
42
56
|
.png({ compressionLevel: 9, colors: colorsPaletteSize });
|
|
43
57
|
return padding
|
|
44
58
|
? outputIcon.extend({
|
|
@@ -81,60 +95,38 @@ function getIconBuffer(rawBuffer, cfg) {
|
|
|
81
95
|
throw Error(`Extension ${iconExtension} is not recognized.`);
|
|
82
96
|
}
|
|
83
97
|
}
|
|
84
|
-
async function produceIcons(inputFilePath, outputDirPath,
|
|
98
|
+
async function produceIcons(inputFilePath, outputDirPath, paletteSize = 64) {
|
|
85
99
|
await promises_1.default.access(inputFilePath);
|
|
86
|
-
|
|
87
|
-
await promises_1.default.access(outputDirPath);
|
|
88
|
-
}
|
|
89
|
-
catch (e) {
|
|
90
|
-
if (e instanceof Error && e.message.includes("ENOENT")) {
|
|
91
|
-
promises_1.default.mkdir(outputDirPath);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
throw e;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
100
|
+
await promises_1.default.mkdir(outputDirPath, { recursive: true });
|
|
97
101
|
const rawIconBuf = await promises_1.default.readFile(inputFilePath);
|
|
98
102
|
const isSvgBuf = (0, is_svg_1.default)(rawIconBuf);
|
|
99
|
-
|
|
103
|
+
const iconConfigs = baseIconConfigs
|
|
104
|
+
.filter((cfg) => isSvgBuf || cfg.name !== "icon.svg")
|
|
105
|
+
.map((cfg) => {
|
|
100
106
|
const mappedCfg = { ...cfg };
|
|
101
|
-
if (!isSvgBuf && mappedCfg.name.endsWith(".svg")) {
|
|
102
|
-
mappedCfg.name = mappedCfg.name.replace(".svg", ".png");
|
|
103
|
-
mappedCfg.pxSize = 32;
|
|
104
|
-
}
|
|
105
|
-
mappedCfg.name = mappedCfg.name.replace("favicon", prefix);
|
|
106
107
|
if (mappedCfg.name.endsWith(".png")) {
|
|
107
108
|
mappedCfg.colorsPaletteSize = paletteSize;
|
|
108
109
|
}
|
|
109
110
|
return mappedCfg;
|
|
110
111
|
});
|
|
111
|
-
if (include16) {
|
|
112
|
-
const ico = iconConfigs.find((cfg) => cfg.name.endsWith(".ico"));
|
|
113
|
-
const icoNameWithoutExt = ico.name.slice(0, -4);
|
|
114
|
-
iconConfigs.push({ ...ico, name: `${icoNameWithoutExt}-32.ico` });
|
|
115
|
-
iconConfigs.push({
|
|
116
|
-
...ico,
|
|
117
|
-
name: `${icoNameWithoutExt}-16.ico`,
|
|
118
|
-
pxSize: 16,
|
|
119
|
-
});
|
|
120
|
-
iconConfigs = iconConfigs.filter((cfg) => cfg !== ico);
|
|
121
|
-
}
|
|
122
112
|
const iconsGenerationSeries = iconConfigs.map(async (cfg) => {
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
? { ...cfg, colorsPaletteSize: paletteSize, name: iconName }
|
|
126
|
-
: { ...cfg, name: iconName };
|
|
127
|
-
const outputBuffer = await getIconBuffer(rawIconBuf, iconCfg);
|
|
128
|
-
return promises_1.default.writeFile(path_1.default.join(outputDirPath, iconCfg.name), outputBuffer);
|
|
113
|
+
const outputBuffer = await getIconBuffer(rawIconBuf, cfg);
|
|
114
|
+
return promises_1.default.writeFile(path_1.default.join(outputDirPath, cfg.name), outputBuffer);
|
|
129
115
|
});
|
|
130
116
|
await Promise.all(iconsGenerationSeries);
|
|
131
117
|
const manifestFile = {
|
|
132
118
|
icons: [
|
|
133
|
-
{ src:
|
|
134
|
-
{
|
|
119
|
+
{ src: "/icon-192.png", type: "image/png", sizes: "192x192" },
|
|
120
|
+
{
|
|
121
|
+
src: "/icon-mask.png",
|
|
122
|
+
type: "image/png",
|
|
123
|
+
sizes: "512x512",
|
|
124
|
+
purpose: "maskable",
|
|
125
|
+
},
|
|
126
|
+
{ src: "/icon-512.png", type: "image/png", sizes: "512x512" },
|
|
135
127
|
],
|
|
136
128
|
};
|
|
137
129
|
const manifestText = JSON.stringify(manifestFile, null, 2);
|
|
138
|
-
promises_1.default.writeFile(path_1.default.join(outputDirPath, "manifest.webmanifest"), manifestText);
|
|
130
|
+
await promises_1.default.writeFile(path_1.default.join(outputDirPath, "manifest.webmanifest"), manifestText);
|
|
139
131
|
}
|
|
140
132
|
exports.default = produceIcons;
|