@fullstackunicorn/websites 1.0.0 → 1.0.1

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 CHANGED
@@ -1,2 +1,9 @@
1
+ // NODE BUILT IN
2
+ export { default as path } from 'path'
3
+
4
+ // FULLSTACKUNICORN
5
+ export { fsa } from '@fullstackunicorn/filesystem'
6
+
1
7
  // TOOLS
8
+ export * as blurhash from "blurhash"
2
9
  export { default as sharp } from 'sharp'
package/index.js CHANGED
@@ -1,4 +1,11 @@
1
1
  import '@fullstackunicorn/initialize'
2
- F3.service = '@fullstackunicorn/icons'
2
+ F3.service = '@fullstackunicorn/websites'
3
+
4
+ import { icons } from '#@/modules/icons.js'
5
+ import { images } from '#@/modules/images.js'
6
+
7
+ export const website = {
8
+ icons,
9
+ images
10
+ }
3
11
 
4
- export { icons } from '#@/modules/icons.js'
package/modules/icons.js CHANGED
@@ -1,16 +1,14 @@
1
- import { tools } from '#@/modules/icons.tools.js'
2
-
3
1
  class Icons {
4
2
 
5
- generate = async (root, config, files = {}) => {
3
+ generate = async (files = {}) => {
6
4
 
7
- const extFaviconExpected = tools.getExtension(config.favicon)
5
+ const result = {}
8
6
 
9
- const extFavicon = tools.getExtension(files.favicon.name)
7
+ const extFavicon = this.getExtension(files.favicon.name)
10
8
  const isFaviconSvg = extFavicon === 'svg'
11
9
  const isFaviconPng = extFavicon === 'png'
12
10
 
13
- const extIcon = tools.getExtension(files.icon.name)
11
+ const extIcon = this.getExtension(files.icon.name)
14
12
  const isIconSvg = extIcon === 'svg'
15
13
  const isIconPng = extIcon === 'png'
16
14
 
@@ -19,68 +17,165 @@ class Icons {
19
17
  let pngIcoBlob
20
18
 
21
19
  // 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)
20
+ if (isFaviconSvg) pngIcoBlob = await this.generateFaviconsFromSvg(result.icons, files.favicon.data)
21
+ else if (isFaviconPng) pngIcoBlob = await this.generateFaviconsFromPng(result.icons, files.favicon.data)
22
+ if (pngIcoBlob) await this.generateIco(result.favicon, pngIcoBlob)
25
23
 
26
24
  // 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)
25
+ if (isIconSvg) await this.generateIconsFromSvg(result.icons, files.icon.data)
26
+ else if (isIconPng) await this.generateIconsFromPng(result.icons, files.icon.data)
29
27
 
30
- return { favicon: extFavicon }
28
+ return result
31
29
 
32
30
  }
33
31
 
34
- generateIco = async (zip, pngBlob) => {
32
+ generate = async ({ favicon, icon }) => {
33
+
34
+ const result = {}
35
+ result.ico = {}
36
+ result.icons = {}
37
+
38
+ const extFavicon = this.getExtension(favicon.name)
39
+ const isFaviconSvg = extFavicon === 'svg'
40
+ const isFaviconPng = extFavicon === 'png'
41
+
42
+ const extIcon = this.getExtension(icon.name)
43
+ const isIconSvg = extIcon === 'svg'
44
+ const isIconPng = extIcon === 'png'
45
+
46
+ let pngIcoBlob = null
47
+
48
+ if (isFaviconSvg) pngIcoBlob = await this.generateFaviconsFromSvg(result.icons, favicon.buffer)
49
+ else if (isFaviconPng) pngIcoBlob = await this.generateFaviconsFromPng(result.icons, favicon.buffer)
50
+ else F3.throw('favicon-extensions-not-matching')
51
+
52
+ if (pngIcoBlob) await this.generateIco(result.ico, pngIcoBlob)
53
+
54
+ if (isIconSvg) await this.generateIconsFromSvg(result.icons, icon.buffer)
55
+ else if (isIconPng) await this.generateIconsFromPng(result.icons, icon.buffer)
56
+ else F3.throw('icon-extensions-not-matching')
57
+
58
+ return result
59
+ }
60
+
61
+ generateIco = async (favicon, pngBlob) => {
35
62
  const icoBlob = await this.pngToIcoBlob(pngBlob)
36
63
  const icoArrayBuffer = await icoBlob.arrayBuffer()
37
- zip.file(`favicon.ico`, icoArrayBuffer)
64
+ this.appendFile(favicon, `favicon.ico`, icoArrayBuffer)
38
65
  }
39
66
 
40
67
  generateIconsFromSvg = async (icons, file) => {
41
68
  const svgText = Buffer.isBuffer(file) ? file.toString() : await file.text();
42
69
  const svgBlob = new Blob([svgText], { type: 'image/svg+xml' });
43
70
  for (const size of this.sizesIcon) {
44
- const pngBlob = await tools.svgToPngBlob(svgBlob, size, size);
71
+ const pngBlob = await this.svgToPngBlob(svgBlob, size, size);
45
72
  const arrayBuffer = await pngBlob.arrayBuffer();
46
- tools.appendFile(icons, `icon-${size}.png`, arrayBuffer);
73
+ this.appendFile(icons, `icon-${size}.png`, arrayBuffer);
47
74
  }
48
75
  return
49
76
  }
50
77
 
51
78
  generateIconsFromPng = async (icons, file) => {
52
79
  for (const size of this.sizesIcon) {
53
- const pngBlob = await tools.resizePngBlob(file, size, size)
80
+ const pngBlob = await this.resizePngBlob(file, size, size)
54
81
  const arrayBuffer = await pngBlob.arrayBuffer()
55
- tools.appendFile(icons, `icon-${size}.png`, arrayBuffer)
82
+ this.appendFile(icons, `icon-${size}.png`, arrayBuffer)
56
83
  }
57
84
  return
58
85
  }
59
86
 
60
87
  generateFaviconsFromSvg = async (icons, file) => {
61
-
62
88
  const svgText = Buffer.isBuffer(file) ? file.toString() : await file.text();
63
89
  const svgBlob = new Blob([svgText], { type: 'image/svg+xml' });
64
-
65
- tools.appendFile(icons, `favicon.svg`, Buffer.isBuffer(file) ? file : await file.arrayBuffer());
66
90
  const size = this.sizeFavicon
67
- const pngBlob = await tools.svgToPngBlob(svgBlob, size, size);
91
+ const pngBlob = await this.svgToPngBlob(svgBlob, size, size);
68
92
  const arrayBuffer = await pngBlob.arrayBuffer();
69
-
70
- tools.appendFile(icons, `favicon.png`, arrayBuffer);
93
+ this.appendFile(icons, `favicon.png`, arrayBuffer);
71
94
  return pngBlob
72
95
  }
73
96
 
74
97
  generateFaviconsFromPng = async (favicon, file) => {
75
98
  const size = this.sizeFavicon
76
- const pngBlob = await tools.resizePngBlob(file, size, size)
99
+ const pngBlob = await this.resizePngBlob(file, size, size)
77
100
  const arrayBuffer = await pngBlob.arrayBuffer()
78
- tools.appendFile(favicon, `favicon.png`, arrayBuffer)
101
+ this.appendFile(favicon, `favicon.png`, arrayBuffer)
79
102
  return pngBlob
80
103
  }
81
104
 
105
+ // helpers
106
+
107
+ getExtension = name => name.split('.').pop().toLowerCase()
108
+
109
+ appendFile = (icons, name, data) => {
110
+ if (data instanceof ArrayBuffer) icons[name] = new Uint8Array(data)
111
+ else if (ArrayBuffer.isView(data)) icons[name] = new Uint8Array(data.buffer)
112
+ else icons[name] = data
113
+ }
114
+
115
+ async svgToPngBlob(svgBlob, width, height) {
116
+ const svgText = await svgBlob.text();
117
+ const buffer = await sharp(Buffer.from(svgText))
118
+ .resize(width, height, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
119
+ .png()
120
+ .toBuffer();
121
+ return new Blob([buffer], { type: 'image/png' });
122
+ }
82
123
 
124
+ async resizePngBlob(pngBlob, width, height) {
125
+ const buffer = Buffer.isBuffer(pngBlob) ? pngBlob : Buffer.from(await pngBlob.arrayBuffer());
126
+ const resizedBuffer = await sharp(buffer)
127
+ .resize(width, height, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
128
+ .png()
129
+ .toBuffer();
130
+ return new Blob([resizedBuffer], { type: 'image/png' });
131
+ }
83
132
 
133
+ pngToIcoBlob = async (pngBlob) => {
134
+
135
+ // ICO Header (ICONDIR): 6 bytes
136
+ const iconDir = new Uint8Array(6);
137
+ iconDir[0] = 0; // Reserved
138
+ iconDir[1] = 0;
139
+ iconDir[2] = 1; // Type: 1 for ICO
140
+ iconDir[3] = 0;
141
+ iconDir[4] = 1; // Number of images
142
+ iconDir[5] = 0;
143
+
144
+ // Read PNG blob
145
+ const pngBuffer = await pngBlob.arrayBuffer();
146
+
147
+ // ICONDIRENTRY: 16 bytes for one image
148
+ const iconEntry = new Uint8Array(16);
149
+ iconEntry[0] = 32; // Width (32px)
150
+ iconEntry[1] = 32; // Height (32px)
151
+ iconEntry[2] = 0; // Color count (0 for >8bpp)
152
+ iconEntry[3] = 0; // Reserved
153
+ iconEntry[4] = 1; // Color planes
154
+ iconEntry[5] = 0;
155
+ iconEntry[6] = 32; // Bits per pixel (32-bit RGBA)
156
+ iconEntry[7] = 0;
157
+ // Size of image data (PNG size)
158
+ const pngSize = pngBuffer.byteLength;
159
+ iconEntry[8] = pngSize & 0xff;
160
+ iconEntry[9] = (pngSize >> 8) & 0xff;
161
+ iconEntry[10] = (pngSize >> 16) & 0xff;
162
+ iconEntry[11] = (pngSize >> 24) & 0xff;
163
+ // Offset of image data (header + entry = 6 + 16 = 22)
164
+ iconEntry[12] = 22;
165
+ iconEntry[13] = 0;
166
+ iconEntry[14] = 0;
167
+ iconEntry[15] = 0;
168
+
169
+ // Combine header, entry, and PNG data
170
+ const totalSize = 6 + 16 + pngBuffer.byteLength;
171
+ const icoBuffer = new Uint8Array(totalSize);
172
+ icoBuffer.set(iconDir, 0);
173
+ icoBuffer.set(iconEntry, 6);
174
+ icoBuffer.set(new Uint8Array(pngBuffer), 22);
175
+
176
+ // Create Blob for ICO
177
+ return new Blob([icoBuffer], { type: 'image/x-icon' });
178
+ }
84
179
  }
85
180
 
86
181
  export const icons = new Icons()
@@ -0,0 +1,139 @@
1
+ import { path, sharp, blurhash } from '#@/index.dep.js'
2
+
3
+ class Images {
4
+ extensions = ['.jpg', '.png']
5
+ widths = {
6
+ TS: 64,
7
+ TM: 256,
8
+ TL: 512,
9
+ SD: 960,
10
+ MD: 1440,
11
+ HD: 1920
12
+ }
13
+
14
+ appendFile = (obj, name, data) => {
15
+ if (data instanceof ArrayBuffer) obj[name] = new Uint8Array(data)
16
+ else if (ArrayBuffer.isView(data)) obj[name] = new Uint8Array(data.buffer)
17
+ else obj[name] = data
18
+ }
19
+
20
+ getExtension = pathParsed => {
21
+ let ext = pathParsed.ext
22
+ if (ext === '.jpeg') ext = '.jpg'
23
+ if (!this.extensions.includes(ext)) F3.throw('non-supported-extension', { pathParsed })
24
+ return ext
25
+ }
26
+
27
+ getPicture = async ({ file, asset }) => {
28
+ const picture = {}
29
+ const pathParsed = path.parse(file.path)
30
+ if (!pathParsed.dir) F3.throw('no-picture-dir', file.path)
31
+
32
+ picture.dir = pathParsed.dir || ''
33
+ picture.original = pathParsed.base
34
+ picture.ext = this.getExtension(pathParsed)
35
+ picture.base = sharp(file.buffer)
36
+ picture.name = pathParsed.name
37
+
38
+ picture.src = `${asset}/${picture.dir}/TS/${picture.name}${picture.ext}`
39
+
40
+ picture.pathJson = `${picture.dir}/${picture.name}.json`.replace(/^\/+/, '')
41
+ picture.pathImg = `${picture.dir}/${picture.name}${picture.ext}`.replace(/^\/+/, '')
42
+
43
+ return picture
44
+ }
45
+
46
+ getPathThumbnail = ({ picture, entry }) => {
47
+ const relPath = `${picture.dir}/${entry[0]}/${picture.name}${picture.ext}`.replace(/^\/+/, '')
48
+ return relPath
49
+ }
50
+
51
+ getPictureData = async ({ picture }) => {
52
+ const size = await picture.base.clone().metadata()
53
+
54
+ if (!size.width || isNaN(size.width)) F3.throw('no-width-height', picture.name)
55
+ if (!size.height || isNaN(size.height)) F3.throw('no-height-height', picture.name)
56
+
57
+ const pictureData = {}
58
+ pictureData.original = picture.original
59
+ pictureData.name = `${picture.name}${picture.ext}`
60
+ pictureData.x = size.width
61
+ pictureData.y = size.height
62
+ pictureData.placeholder = await this.getBlurhash({ picture })
63
+ pictureData.src = picture.src
64
+ pictureData.ratio = size.width / size.height
65
+ pictureData.max = this.getMaxSize({ size })
66
+
67
+ return pictureData
68
+ }
69
+
70
+ getBlurhash = async ({ picture }) => {
71
+ const { data, info } = await picture.base
72
+ .clone()
73
+ .ensureAlpha()
74
+ .resize(32, 32, { fit: 'inside' })
75
+ .raw()
76
+ .toBuffer({ resolveWithObject: true })
77
+ const pixels = new Uint8ClampedArray(data)
78
+ return blurhash.encode(pixels, info.width, info.height, 4, 4)
79
+ }
80
+
81
+ getMaxSize = ({ size }) => {
82
+ const sortedSizes = Object.entries(this.widths).sort(([, a], [, b]) => a - b)
83
+ let maxSize = sortedSizes[0][0]
84
+ for (const [key, value] of sortedSizes) {
85
+ if (size.width >= value) maxSize = key
86
+ else break
87
+ }
88
+ return maxSize
89
+ }
90
+
91
+ getBuffer = async ({ picture, entry }) => {
92
+ const image = picture.base.clone().resize({ width: entry[1] })
93
+ if (picture.ext === '.png') image.png({ compressionLevel: 9, progressive: true })
94
+ else image.jpeg({ quality: 75, progressive: true })
95
+ const buffer = await image.toBuffer()
96
+ return buffer
97
+ }
98
+
99
+ generate = async ({ file, asset }) => {
100
+ const picture = await this.getPicture({ file, asset })
101
+ const pictureData = await this.getPictureData({ picture })
102
+
103
+ const result = {}
104
+
105
+ // json
106
+ this.appendFile(result, 'json', {
107
+ path: picture.pathJson,
108
+ file: pictureData
109
+ })
110
+
111
+ // original
112
+ const originalBuffer = await picture.base.clone().toBuffer()
113
+ this.appendFile(result, 'original', {
114
+ path: picture.pathImg,
115
+ file: originalBuffer
116
+ })
117
+
118
+ const created = []
119
+ for (const entry of Object.entries(this.widths)) {
120
+ if (pictureData.x <= entry[1]) continue
121
+
122
+ const buffer = await this.getBuffer({ picture, entry })
123
+ const thumbPath = this.getPathThumbnail({ picture, entry })
124
+
125
+ // thumbnail
126
+ this.appendFile(result, entry[0], {
127
+ path: thumbPath,
128
+ file: buffer
129
+ })
130
+ created.push(entry[0])
131
+ }
132
+
133
+ F3.logs('✅ Created: ', created.join(' '))
134
+
135
+ return result
136
+ }
137
+ }
138
+
139
+ export const images = new Images()
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0",
2
+ "version": "1.0.1",
3
3
  "name": "@fullstackunicorn/websites",
4
4
  "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
5
  "description": "A development utility for generating icons, favicons, thumbnails and all what is needed for websites",
@@ -29,14 +29,17 @@
29
29
  "#@/*": "./*"
30
30
  },
31
31
  "scripts": {
32
- "test": "exit 1"
32
+ "test": "exit 1",
33
+ "run": "chmod +x ./config.run.sh && ./config.run.sh"
33
34
  },
34
35
  "dependencies": {
35
- "@fullstackunicorn/initialize": "^1.0.2",
36
+ "@fullstackunicorn/filesystem": "^1.0.4",
37
+ "@fullstackunicorn/initialize": "^1.0.7",
38
+ "blurhash": "^2.0.5",
36
39
  "sharp": "^0.34.5"
37
40
  },
38
41
  "devDependencies": {
39
- "@fullstackunicorn/cli": "^1.0.0",
40
- "@fullstackunicorn/reduce": "^1.0.18"
42
+ "@fullstackunicorn/cli": "^1.0.3",
43
+ "@fullstackunicorn/reduce": "^1.0.19"
41
44
  }
42
45
  }
@@ -1,80 +0,0 @@
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()