@saiballo/eleventy-plugin-favicon-factory 1.0.4 → 1.1.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/package.json +2 -2
- package/src/favicon.js +82 -61
- package/src/utils.js +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saiballo/eleventy-plugin-favicon-factory",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A plugin for 11ty that creates favicons and a manifest",
|
|
5
5
|
"main": "main.js",
|
|
6
6
|
"keywords": [
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"refresh": "rm -rf node_modules && rm package-lock.json && npm install"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"sharp": "^0.34.
|
|
36
|
+
"sharp": "^0.34.5",
|
|
37
37
|
"sharp-ico": "^0.1.5"
|
|
38
38
|
}
|
|
39
39
|
}
|
package/src/favicon.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Created: 15/04/2024 (18:41:03)
|
|
6
6
|
* Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
7
7
|
*
|
|
8
|
-
* Last update:
|
|
8
|
+
* Last update: 21/02/2026 (18:42:55)
|
|
9
9
|
* Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
10
10
|
*
|
|
11
11
|
* Copyleft: 2024 - Tutti i diritti riservati
|
|
@@ -34,7 +34,7 @@ const createManifest = async (eleventyconfig, pluginconfig, sizelist) => {
|
|
|
34
34
|
const filePath = util.getFilePath(pluginconfig.imgPathHref, pluginconfig.outputFolder);
|
|
35
35
|
|
|
36
36
|
// if missing, create the output folder
|
|
37
|
-
await util.
|
|
37
|
+
await util.checkFolder(path.join(eleventyconfig.dir.output, pluginconfig.manifestOutputFolder));
|
|
38
38
|
|
|
39
39
|
// create the icon list filtering only 3 sizes
|
|
40
40
|
const iconList = sizelist.filter((size) => [192, 256, 512].includes(size)).map((size) => ({
|
|
@@ -75,36 +75,37 @@ const createManifest = async (eleventyconfig, pluginconfig, sizelist) => {
|
|
|
75
75
|
const getHtmlCode = (eleventyconfig, pluginconfig) => {
|
|
76
76
|
|
|
77
77
|
const filePath = util.getFilePath(pluginconfig.imgPathHref, pluginconfig.outputFolder);
|
|
78
|
+
const sizeList = Array.isArray(pluginconfig.sizeList) ? pluginconfig.sizeList : [];
|
|
78
79
|
|
|
79
80
|
const htmlCode = [
|
|
80
81
|
// universal fallback
|
|
81
|
-
`<link rel="shortcut icon" href="${filePath}/${pluginconfig.prefixName}.ico" type="image/x-icon"
|
|
82
|
+
`<link rel="shortcut icon" href="${filePath}/${pluginconfig.prefixName}.ico" type="image/x-icon">`,
|
|
82
83
|
// modern browsers
|
|
83
84
|
`<link rel="icon" href="${filePath}/${pluginconfig.prefixName}.svg" type="image/svg+xml">`
|
|
84
85
|
];
|
|
85
86
|
|
|
86
87
|
// fallback some desktop browsers and modern mobile browsers: Chrome/Edge mobile
|
|
87
|
-
if (
|
|
88
|
+
if (sizeList.includes(32)) {
|
|
88
89
|
|
|
89
90
|
htmlCode.push(`<link rel="icon" type="image/png" sizes="32x32" href="${filePath}/${pluginconfig.prefixName}-32x32.png">`);
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
if (
|
|
93
|
+
if (sizeList.includes(16)) {
|
|
93
94
|
|
|
94
95
|
htmlCode.push(`<link rel="icon" type="image/png" sizes="16x16" href="${filePath}/${pluginconfig.prefixName}-16x16.png">`);
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
if (
|
|
98
|
+
if (sizeList.includes(512)) {
|
|
98
99
|
|
|
99
|
-
htmlCode.push(`<link rel="apple-touch-icon" href="${filePath}
|
|
100
|
+
htmlCode.push(`<link rel="apple-touch-icon" href="${filePath}/${pluginconfig.prefixName}-512x512.png">`);
|
|
100
101
|
}
|
|
101
102
|
|
|
102
103
|
// apple icon
|
|
103
|
-
|
|
104
|
+
sizeList.forEach((size) => {
|
|
104
105
|
|
|
105
106
|
if ([57, 72, 76, 114, 120, 144, 152, 180].includes(size)) {
|
|
106
107
|
|
|
107
|
-
htmlCode.push(`<link rel="apple-touch-icon" sizes="${size}x${size}" href="${filePath}
|
|
108
|
+
htmlCode.push(`<link rel="apple-touch-icon" sizes="${size}x${size}" href="${filePath}/${pluginconfig.prefixName}-${size}x${size}.png">`);
|
|
108
109
|
}
|
|
109
110
|
});
|
|
110
111
|
|
|
@@ -133,83 +134,103 @@ const getHtmlCode = (eleventyconfig, pluginconfig) => {
|
|
|
133
134
|
*/
|
|
134
135
|
const favicon = (eleventyconfig, pluginconfig) => {
|
|
135
136
|
|
|
136
|
-
|
|
137
|
+
let compilePromise = null;
|
|
138
|
+
let devOnlyMsgShown = false;
|
|
137
139
|
|
|
138
|
-
|
|
140
|
+
const skipProdCompile = () => {
|
|
139
141
|
|
|
140
|
-
|
|
142
|
+
if (pluginconfig.runOnlyDevMode === true && process.env.NODE_ENV === "production") return true;
|
|
141
143
|
|
|
142
|
-
|
|
144
|
+
return false;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const compileOnce = async (sourceimg) => {
|
|
148
|
+
|
|
149
|
+
const isValidSource = await util.isFile(sourceimg);
|
|
150
|
+
|
|
151
|
+
if (isValidSource === false) {
|
|
152
|
+
|
|
153
|
+
toLog("source image not found. can't process favicon list", "error");
|
|
154
|
+
return;
|
|
143
155
|
}
|
|
144
156
|
|
|
145
|
-
|
|
157
|
+
const outputDir = path.join(eleventyconfig.dir.output, pluginconfig.outputFolder);
|
|
158
|
+
const isOutputFolder = await util.checkFolder(outputDir);
|
|
146
159
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
// if it doesn't exist, i can't proceed
|
|
150
|
-
if (!isValidSource) {
|
|
160
|
+
// stop if output folder exists
|
|
161
|
+
if (isOutputFolder === true) {
|
|
151
162
|
|
|
152
|
-
|
|
163
|
+
toLog("favicon shortcode detected but output folder already exists. favicon generation skipped", "info");
|
|
153
164
|
|
|
154
|
-
|
|
155
|
-
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
toLog("starting favicons creation...", "info");
|
|
169
|
+
|
|
170
|
+
util.copyFile(sourceimg, `${outputDir}/${pluginconfig.prefixName}.svg`);
|
|
156
171
|
|
|
157
|
-
|
|
158
|
-
|
|
172
|
+
const isValidSizeList = Array.isArray(pluginconfig.sizeList) && pluginconfig.sizeList.length > 0;
|
|
173
|
+
const sizeList = [
|
|
174
|
+
...isValidSizeList ? pluginconfig.sizeList : [],
|
|
175
|
+
...[512]
|
|
176
|
+
];
|
|
177
|
+
const uniqueSizeList = [...new Set(sizeList)];
|
|
159
178
|
|
|
160
|
-
|
|
161
|
-
if (!isOutputFolder || process.env.NODE_ENV === "production") {
|
|
179
|
+
await Promise.all(uniqueSizeList.map((size) => {
|
|
162
180
|
|
|
163
|
-
|
|
181
|
+
return sharp(sourceimg).resize(size).toFile(`${outputDir}/${pluginconfig.prefixName}-${size}x${size}.png`);
|
|
182
|
+
}));
|
|
164
183
|
|
|
165
|
-
|
|
184
|
+
// check if the 512x512 image exists
|
|
185
|
+
const png512Path = `${outputDir}/${pluginconfig.prefixName}-512x512.png`;
|
|
186
|
+
const isValid512 = await util.isFile(png512Path);
|
|
187
|
+
|
|
188
|
+
if (isValid512 === false) return;
|
|
189
|
+
|
|
190
|
+
// create the icon file format
|
|
191
|
+
await sharpIco.sharpsToIco(
|
|
192
|
+
[
|
|
193
|
+
sharp(png512Path)
|
|
194
|
+
],
|
|
195
|
+
`${outputDir}/${pluginconfig.prefixName}.ico`,
|
|
196
|
+
{
|
|
197
|
+
"sizes": [64, 32, 24],
|
|
198
|
+
"resizeOptions": {
|
|
199
|
+
"fit": "cover"
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
);
|
|
166
203
|
|
|
167
|
-
|
|
168
|
-
const sizeList = [
|
|
169
|
-
...isValidSizeList ? pluginconfig.sizeList : [],
|
|
170
|
-
...[512]
|
|
171
|
-
];
|
|
172
|
-
const uniqueSizeList = [...new Set(sizeList)];
|
|
204
|
+
if (pluginconfig.manifestGenerate === true) {
|
|
173
205
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
206
|
+
await createManifest(eleventyconfig, pluginconfig, uniqueSizeList);
|
|
207
|
+
}
|
|
208
|
+
};
|
|
177
209
|
|
|
178
|
-
|
|
179
|
-
const png512Path = `${outputDir}/${pluginconfig.prefixName}-512x512.png`;
|
|
180
|
-
const isValid512 = await util.isFile(png512Path);
|
|
210
|
+
eleventyconfig.addShortcode("favicon", async (sourceimg) => {
|
|
181
211
|
|
|
182
|
-
|
|
212
|
+
// log once in production
|
|
213
|
+
if (skipProdCompile() === true) {
|
|
183
214
|
|
|
184
|
-
|
|
185
|
-
await sharpIco.sharpsToIco(
|
|
186
|
-
[
|
|
187
|
-
sharp(`${outputDir}/${pluginconfig.prefixName}-512x512.png`)
|
|
188
|
-
],
|
|
189
|
-
`${outputDir}/${pluginconfig.prefixName}.ico`,
|
|
190
|
-
{
|
|
191
|
-
"sizes": [64, 32, 24],
|
|
192
|
-
"resizeOptions": {
|
|
193
|
-
"fit": "cover"
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
);
|
|
215
|
+
if (devOnlyMsgShown === false) {
|
|
197
216
|
|
|
198
|
-
|
|
199
|
-
if (pluginconfig.manifestGenerate) {
|
|
217
|
+
toLog("favicon compilation is required only in dev mode", "info");
|
|
200
218
|
|
|
201
|
-
|
|
202
|
-
}
|
|
219
|
+
devOnlyMsgShown = true;
|
|
203
220
|
}
|
|
204
221
|
|
|
205
222
|
return getHtmlCode(eleventyconfig, pluginconfig);
|
|
223
|
+
}
|
|
206
224
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
toLog(`${err["message"]}`, "error");
|
|
225
|
+
// memoization: only once compile action
|
|
226
|
+
if (compilePromise === null) {
|
|
210
227
|
|
|
211
|
-
|
|
228
|
+
compilePromise = compileOnce(sourceimg);
|
|
212
229
|
}
|
|
230
|
+
|
|
231
|
+
await compilePromise;
|
|
232
|
+
|
|
233
|
+
return getHtmlCode(eleventyconfig, pluginconfig);
|
|
213
234
|
});
|
|
214
235
|
};
|
|
215
236
|
|
package/src/utils.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Created: 15/04/2024 (18:41:03)
|
|
6
6
|
* Created by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
7
7
|
*
|
|
8
|
-
* Last update:
|
|
8
|
+
* Last update: 21/02/2026 (18:43:41)
|
|
9
9
|
* Updated by: Lorenzo Saibal Forti <lorenzo.forti@gmail.com>
|
|
10
10
|
*
|
|
11
11
|
* Copyleft: 2024 - Tutti i diritti riservati
|
|
@@ -57,12 +57,12 @@ const copyFile = async (source, destination) => {
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
|
-
* The function `
|
|
60
|
+
* The function `checkFolder` checks if a folder exists, creates it if it doesn't, and logs any errors encountered.
|
|
61
61
|
* @param folder - it represents the path to a directory that you want to check if it exists. If the directory exists, the function will return `true`.
|
|
62
62
|
* If the directory does not exist, the function will create it using `fsp.mkdir` with the `recursive
|
|
63
63
|
* @returns If the folder exists, `true` is being returned. If the folder does not exist and is successfully created, `false` is being returned.
|
|
64
64
|
*/
|
|
65
|
-
const
|
|
65
|
+
const checkFolder = async (folder) => {
|
|
66
66
|
|
|
67
67
|
try {
|
|
68
68
|
|
|
@@ -73,7 +73,7 @@ const isFolder = async (folder) => {
|
|
|
73
73
|
"recursive": true
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
-
toLog("output folder created.
|
|
76
|
+
toLog("output folder created. processing favicons...", "info");
|
|
77
77
|
|
|
78
78
|
return false;
|
|
79
79
|
|
|
@@ -117,5 +117,5 @@ module["exports"] = {
|
|
|
117
117
|
getFilePath,
|
|
118
118
|
copyFile,
|
|
119
119
|
isFile,
|
|
120
|
-
|
|
120
|
+
checkFolder
|
|
121
121
|
};
|