@fullstackunicorn/websites 1.0.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/index.dep.js ADDED
@@ -0,0 +1,2 @@
1
+ // TOOLS
2
+ export { default as sharp } from 'sharp'
package/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import '@fullstackunicorn/initialize'
2
+ F3.service = '@fullstackunicorn/icons'
3
+
4
+ export { icons } from '#@/modules/icons.js'
@@ -0,0 +1,86 @@
1
+ import { tools } from '#@/modules/icons.tools.js'
2
+
3
+ class Icons {
4
+
5
+ generate = async (root, config, files = {}) => {
6
+
7
+ const extFaviconExpected = tools.getExtension(config.favicon)
8
+
9
+ const extFavicon = tools.getExtension(files.favicon.name)
10
+ const isFaviconSvg = extFavicon === 'svg'
11
+ const isFaviconPng = extFavicon === 'png'
12
+
13
+ const extIcon = tools.getExtension(files.icon.name)
14
+ const isIconSvg = extIcon === 'svg'
15
+ const isIconPng = extIcon === 'png'
16
+
17
+ if (extFaviconExpected !== extFavicon) F3.throw('favicon-extensions-not-matching')
18
+
19
+ let pngIcoBlob
20
+
21
+ // generate the favicons
22
+ if (isFaviconSvg) pngIcoBlob = await this.generateFaviconsFromSvg(root.icons, files.favicon.data)
23
+ else if (isFaviconPng) pngIcoBlob = await this.generateFaviconsFromPng(root.icons, files.favicon.data)
24
+ if (pngIcoBlob) await this.generateIco(root.favicon, pngIcoBlob)
25
+
26
+ // generate the icons
27
+ if (isIconSvg) await this.generateIconsFromSvg(root.icons, files.icon.data)
28
+ else if (isIconPng) await this.generateIconsFromPng(root.icons, files.icon.data)
29
+
30
+ return { favicon: extFavicon }
31
+
32
+ }
33
+
34
+ generateIco = async (zip, pngBlob) => {
35
+ const icoBlob = await this.pngToIcoBlob(pngBlob)
36
+ const icoArrayBuffer = await icoBlob.arrayBuffer()
37
+ zip.file(`favicon.ico`, icoArrayBuffer)
38
+ }
39
+
40
+ generateIconsFromSvg = async (icons, file) => {
41
+ const svgText = Buffer.isBuffer(file) ? file.toString() : await file.text();
42
+ const svgBlob = new Blob([svgText], { type: 'image/svg+xml' });
43
+ for (const size of this.sizesIcon) {
44
+ const pngBlob = await tools.svgToPngBlob(svgBlob, size, size);
45
+ const arrayBuffer = await pngBlob.arrayBuffer();
46
+ tools.appendFile(icons, `icon-${size}.png`, arrayBuffer);
47
+ }
48
+ return
49
+ }
50
+
51
+ generateIconsFromPng = async (icons, file) => {
52
+ for (const size of this.sizesIcon) {
53
+ const pngBlob = await tools.resizePngBlob(file, size, size)
54
+ const arrayBuffer = await pngBlob.arrayBuffer()
55
+ tools.appendFile(icons, `icon-${size}.png`, arrayBuffer)
56
+ }
57
+ return
58
+ }
59
+
60
+ generateFaviconsFromSvg = async (icons, file) => {
61
+
62
+ const svgText = Buffer.isBuffer(file) ? file.toString() : await file.text();
63
+ const svgBlob = new Blob([svgText], { type: 'image/svg+xml' });
64
+
65
+ tools.appendFile(icons, `favicon.svg`, Buffer.isBuffer(file) ? file : await file.arrayBuffer());
66
+ const size = this.sizeFavicon
67
+ const pngBlob = await tools.svgToPngBlob(svgBlob, size, size);
68
+ const arrayBuffer = await pngBlob.arrayBuffer();
69
+
70
+ tools.appendFile(icons, `favicon.png`, arrayBuffer);
71
+ return pngBlob
72
+ }
73
+
74
+ generateFaviconsFromPng = async (favicon, file) => {
75
+ const size = this.sizeFavicon
76
+ const pngBlob = await tools.resizePngBlob(file, size, size)
77
+ const arrayBuffer = await pngBlob.arrayBuffer()
78
+ tools.appendFile(favicon, `favicon.png`, arrayBuffer)
79
+ return pngBlob
80
+ }
81
+
82
+
83
+
84
+ }
85
+
86
+ export const icons = new Icons()
@@ -0,0 +1,80 @@
1
+ import { sharp } from '#@/index.dep.js'
2
+
3
+ class Tools {
4
+
5
+ getExtension = name => name.split('.').pop().toLowerCase()
6
+
7
+ appendFile = (icons, name, data) => {
8
+ if (data instanceof ArrayBuffer) icons[name] = new Uint8Array(data)
9
+ else if (ArrayBuffer.isView(data)) icons[name] = new Uint8Array(data.buffer)
10
+ else icons[name] = data
11
+ }
12
+
13
+ async svgToPngBlob(svgBlob, width, height) {
14
+ const svgText = await svgBlob.text();
15
+ const buffer = await sharp(Buffer.from(svgText))
16
+ .resize(width, height, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
17
+ .png()
18
+ .toBuffer();
19
+ return new Blob([buffer], { type: 'image/png' });
20
+ }
21
+
22
+ async resizePngBlob(pngBlob, width, height) {
23
+ const buffer = Buffer.isBuffer(pngBlob) ? pngBlob : Buffer.from(await pngBlob.arrayBuffer());
24
+ const resizedBuffer = await sharp(buffer)
25
+ .resize(width, height, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
26
+ .png()
27
+ .toBuffer();
28
+ return new Blob([resizedBuffer], { type: 'image/png' });
29
+ }
30
+
31
+ pngToIcoBlob = async (pngBlob) => {
32
+
33
+ // ICO Header (ICONDIR): 6 bytes
34
+ const iconDir = new Uint8Array(6);
35
+ iconDir[0] = 0; // Reserved
36
+ iconDir[1] = 0;
37
+ iconDir[2] = 1; // Type: 1 for ICO
38
+ iconDir[3] = 0;
39
+ iconDir[4] = 1; // Number of images
40
+ iconDir[5] = 0;
41
+
42
+ // Read PNG blob
43
+ const pngBuffer = await pngBlob.arrayBuffer();
44
+
45
+ // ICONDIRENTRY: 16 bytes for one image
46
+ const iconEntry = new Uint8Array(16);
47
+ iconEntry[0] = 32; // Width (32px)
48
+ iconEntry[1] = 32; // Height (32px)
49
+ iconEntry[2] = 0; // Color count (0 for >8bpp)
50
+ iconEntry[3] = 0; // Reserved
51
+ iconEntry[4] = 1; // Color planes
52
+ iconEntry[5] = 0;
53
+ iconEntry[6] = 32; // Bits per pixel (32-bit RGBA)
54
+ iconEntry[7] = 0;
55
+ // Size of image data (PNG size)
56
+ const pngSize = pngBuffer.byteLength;
57
+ iconEntry[8] = pngSize & 0xff;
58
+ iconEntry[9] = (pngSize >> 8) & 0xff;
59
+ iconEntry[10] = (pngSize >> 16) & 0xff;
60
+ iconEntry[11] = (pngSize >> 24) & 0xff;
61
+ // Offset of image data (header + entry = 6 + 16 = 22)
62
+ iconEntry[12] = 22;
63
+ iconEntry[13] = 0;
64
+ iconEntry[14] = 0;
65
+ iconEntry[15] = 0;
66
+
67
+ // Combine header, entry, and PNG data
68
+ const totalSize = 6 + 16 + pngBuffer.byteLength;
69
+ const icoBuffer = new Uint8Array(totalSize);
70
+ icoBuffer.set(iconDir, 0);
71
+ icoBuffer.set(iconEntry, 6);
72
+ icoBuffer.set(new Uint8Array(pngBuffer), 22);
73
+
74
+ // Create Blob for ICO
75
+ return new Blob([icoBuffer], { type: 'image/x-icon' });
76
+ }
77
+
78
+ }
79
+
80
+ export const tools = new Tools()
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "version": "1.0.0",
3
+ "name": "@fullstackunicorn/websites",
4
+ "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
+ "description": "A development utility for generating icons, favicons, thumbnails and all what is needed for websites",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "private": false,
9
+ "readme": "package.readme.md",
10
+ "main": "index.js",
11
+ "files": [
12
+ "index.js",
13
+ "index.dep.js",
14
+ "modules/"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://gitlab.com/fullstackunicorn/websites.git"
19
+ },
20
+ "keywords": [
21
+ "icon-generator",
22
+ "favicon-generator",
23
+ "react-native",
24
+ "react",
25
+ "fullstackunicorn"
26
+ ],
27
+ "imports": {
28
+ "#@": "./",
29
+ "#@/*": "./*"
30
+ },
31
+ "scripts": {
32
+ "test": "exit 1"
33
+ },
34
+ "dependencies": {
35
+ "@fullstackunicorn/initialize": "^1.0.2",
36
+ "sharp": "^0.34.5"
37
+ },
38
+ "devDependencies": {
39
+ "@fullstackunicorn/cli": "^1.0.0",
40
+ "@fullstackunicorn/reduce": "^1.0.18"
41
+ }
42
+ }