@hkdigital/lib-core 0.5.25 → 0.5.27
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.
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @param {object} [options]
|
|
6
6
|
* @param {number[]} [options.widths=DEFAULT_WIDTHS]
|
|
7
|
-
* @param {number
|
|
7
|
+
* @param {number} [options.thumbnailWidth=DEFAULT_THUMBNAIL_WIDTH]
|
|
8
|
+
* @param {number[]} [options.faviconSizes=FAVICON_SIZES]
|
|
9
|
+
* @param {number[]} [options.appleTouchSizes=APPLE_TOUCH_SIZES]
|
|
8
10
|
*
|
|
9
11
|
* @returns {(
|
|
10
12
|
* entries: [string, string[]][]
|
|
@@ -12,7 +14,9 @@
|
|
|
12
14
|
*/
|
|
13
15
|
export function generateResponseConfigs(options?: {
|
|
14
16
|
widths?: number[] | undefined;
|
|
15
|
-
thumbnailWidth?: number
|
|
17
|
+
thumbnailWidth?: number | undefined;
|
|
18
|
+
faviconSizes?: number[] | undefined;
|
|
19
|
+
appleTouchSizes?: number[] | undefined;
|
|
16
20
|
}): (entries: [string, string[]][]) => (Record<string, string | string[]>[]);
|
|
17
21
|
/**
|
|
18
22
|
* Configures and returns a function that can be used as
|
|
@@ -66,7 +66,9 @@ const DEFAULT_PRESETS = {
|
|
|
66
66
|
*
|
|
67
67
|
* @param {object} [options]
|
|
68
68
|
* @param {number[]} [options.widths=DEFAULT_WIDTHS]
|
|
69
|
-
* @param {number
|
|
69
|
+
* @param {number} [options.thumbnailWidth=DEFAULT_THUMBNAIL_WIDTH]
|
|
70
|
+
* @param {number[]} [options.faviconSizes=FAVICON_SIZES]
|
|
71
|
+
* @param {number[]} [options.appleTouchSizes=APPLE_TOUCH_SIZES]
|
|
70
72
|
*
|
|
71
73
|
* @returns {(
|
|
72
74
|
* entries: [string, string[]][]
|
|
@@ -91,12 +93,23 @@ export function generateResponseConfigs(options) {
|
|
|
91
93
|
configPairs[key] = value;
|
|
92
94
|
}
|
|
93
95
|
|
|
96
|
+
// console.log('entries', entries);
|
|
97
|
+
// e.g.
|
|
98
|
+
// entries [
|
|
99
|
+
// [ 'apple-touch-icons', [ '' ] ],
|
|
100
|
+
// [ 'as', [ 'metadata' ] ],
|
|
101
|
+
// [ 'format', [ 'avif' ] ],
|
|
102
|
+
// [ 'quality', [ '90' ] ]
|
|
103
|
+
// ]
|
|
104
|
+
|
|
94
105
|
// @ts-ignore
|
|
95
106
|
const responsiveConfig = entries.find(([key]) => key === 'responsive');
|
|
107
|
+
|
|
96
108
|
// @ts-ignore
|
|
97
|
-
const
|
|
109
|
+
const isFavicon = !!entries.find(([key]) => key === 'favicons');
|
|
110
|
+
|
|
98
111
|
// @ts-ignore
|
|
99
|
-
const
|
|
112
|
+
const isAppleTouchIcon = !!entries.find(([key]) => key === 'apple-touch-icons');
|
|
100
113
|
// console.log('responsiveConfig found:', !!responsiveConfig);
|
|
101
114
|
|
|
102
115
|
const widths = options?.widths ?? DEFAULT_WIDTHS;
|
|
@@ -110,7 +123,7 @@ export function generateResponseConfigs(options) {
|
|
|
110
123
|
};
|
|
111
124
|
|
|
112
125
|
// Handle favicons directive - generate all favicon sizes as PNG
|
|
113
|
-
if (
|
|
126
|
+
if (isFavicon) {
|
|
114
127
|
const faviconConfigs = faviconSizes.map((w) => {
|
|
115
128
|
return {
|
|
116
129
|
...configPairs,
|
|
@@ -124,7 +137,10 @@ export function generateResponseConfigs(options) {
|
|
|
124
137
|
}
|
|
125
138
|
|
|
126
139
|
// Handle apple-touch-icons directive - generate Apple touch icon sizes as PNG
|
|
127
|
-
|
|
140
|
+
|
|
141
|
+
// console.log('**** Check:isAppleTouchIcon', isAppleTouchIcon);
|
|
142
|
+
|
|
143
|
+
if (isAppleTouchIcon) {
|
|
128
144
|
const appleTouchConfigs = appleTouchSizes.map((w) => {
|
|
129
145
|
return {
|
|
130
146
|
...configPairs,
|
|
@@ -133,7 +149,7 @@ export function generateResponseConfigs(options) {
|
|
|
133
149
|
ensureAlpha: 'true'
|
|
134
150
|
};
|
|
135
151
|
});
|
|
136
|
-
// console.log('Returning apple-touch-icon configs:', appleTouchConfigs);
|
|
152
|
+
// console.log('**** Returning apple-touch-icon configs:', appleTouchConfigs);
|
|
137
153
|
return appleTouchConfigs;
|
|
138
154
|
}
|
|
139
155
|
|
|
@@ -178,6 +194,9 @@ export function generateDefaultDirectives(options) {
|
|
|
178
194
|
|
|
179
195
|
// > Return metadata if directive 'responsive' is set
|
|
180
196
|
|
|
197
|
+
// @see https://github.com/JonasKruckenberg/
|
|
198
|
+
// imagetools/blob/main/docs/directives.md#metadata
|
|
199
|
+
|
|
181
200
|
if (params.has('responsive')) {
|
|
182
201
|
params.set('as', 'metadata');
|
|
183
202
|
}
|
|
@@ -68,38 +68,42 @@ export async function generateViteConfig(options = {}) {
|
|
|
68
68
|
generateResponseConfigs
|
|
69
69
|
} = await import('./imagetools.js');
|
|
70
70
|
|
|
71
|
-
// Custom transform to ensure alpha channel based on directive
|
|
72
|
-
const ensureAlphaTransform = (config) => {
|
|
73
|
-
// Only apply if ensureAlpha directive is present
|
|
74
|
-
if (!config.ensureAlpha) return undefined;
|
|
75
|
-
|
|
76
|
-
return async (image) => {
|
|
77
|
-
// Force RGBA output by making one pixel slightly transparent
|
|
78
|
-
// This prevents Sharp from optimizing back to RGB palette
|
|
79
|
-
const metadata = await image.metadata();
|
|
80
|
-
|
|
81
|
-
return image
|
|
82
|
-
.ensureAlpha()
|
|
83
|
-
.composite([{
|
|
84
|
-
input: Buffer.from([0, 0, 0, 254]), // RGBA: black with 254 alpha (nearly opaque)
|
|
85
|
-
raw: { width: 1, height: 1, channels: 4 },
|
|
86
|
-
top: 0,
|
|
87
|
-
left: 0,
|
|
88
|
-
blend: 'over'
|
|
89
|
-
}]);
|
|
90
|
-
};
|
|
91
|
-
};
|
|
92
|
-
|
|
93
71
|
plugins.push(
|
|
94
72
|
imagetools({
|
|
95
73
|
defaultDirectives: generateDefaultDirectives(imagetoolsOptions),
|
|
96
74
|
resolveConfigs: generateResponseConfigs(imagetoolsOptions),
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
75
|
+
cache: {
|
|
76
|
+
// @note disable caching to test custom transforms
|
|
77
|
+
enabled: false,
|
|
78
|
+
// enabled: true,
|
|
79
|
+
dir: './node_modules/.cache/imagetools',
|
|
80
|
+
retention: 60 * 60 * 24 * 10 // 10 days
|
|
81
|
+
},
|
|
82
|
+
// @see https://www.npmjs.com/package/vite-imagetools?activeTab=readme
|
|
83
|
+
extendTransforms(builtins) {
|
|
84
|
+
const ensureAlphaTransform = (config, ctx) => {
|
|
85
|
+
// Check if 'ensureAlpha' directive is in the URL
|
|
86
|
+
if (!('ensureAlpha' in config)) {
|
|
87
|
+
return undefined // This transform doesn't apply
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Mark the parameter as used
|
|
91
|
+
ctx.useParam('ensureAlpha')
|
|
92
|
+
|
|
93
|
+
// Return the actual transformation function
|
|
94
|
+
return (image) => {
|
|
95
|
+
return image.ensureAlpha();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Return an ARRAY with builtins + your custom transform
|
|
100
|
+
return [...builtins, ensureAlphaTransform]
|
|
101
|
+
|
|
102
|
+
}
|
|
101
103
|
})
|
|
102
104
|
);
|
|
105
|
+
|
|
106
|
+
// eslint-disable-next-line no-unused-vars
|
|
103
107
|
} catch (error) {
|
|
104
108
|
const errorMessage = `
|
|
105
109
|
╭─────────────────────────────────────────────────────────────╮
|
|
@@ -126,6 +130,7 @@ export async function generateViteConfig(options = {}) {
|
|
|
126
130
|
};
|
|
127
131
|
|
|
128
132
|
if (enableVitest && !enableVitestWorkspace) {
|
|
133
|
+
// @ts-ignore
|
|
129
134
|
config.test = {
|
|
130
135
|
include: [
|
|
131
136
|
'src/**/*.{test,spec}.{js,ts}',
|
|
@@ -139,6 +144,7 @@ export async function generateViteConfig(options = {}) {
|
|
|
139
144
|
// Workspace mode: separate projects for jsdom and node tests
|
|
140
145
|
// jsdom tests: *.svelte.test.js (component tests with jsdom)
|
|
141
146
|
// node tests: *.test.js, *.spec.js (server-side tests)
|
|
147
|
+
// @ts-ignore
|
|
142
148
|
config.test = {
|
|
143
149
|
...vitestOptions,
|
|
144
150
|
projects: [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hkdigital/lib-core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.27",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "HKdigital",
|
|
6
6
|
"url": "https://hkdigital.nl"
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"check:run": "svelte-check --tsconfig ./jsconfig.json",
|
|
39
39
|
"check:watch-run": "svelte-check --tsconfig ./jsconfig.json --watch",
|
|
40
40
|
"git:push": "git push",
|
|
41
|
+
"imagetools:clean": "rm -rf node_modules/.cache/imagetools",
|
|
41
42
|
"lint:prettier": "prettier --check .",
|
|
42
43
|
"lint:eslint": "eslint .",
|
|
43
44
|
"test:unit": "vitest",
|