@hkdigital/lib-core 0.5.26 → 0.5.28

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[]} [options.thumbnailWidth=DEFAULT_THUMBNAIL_WIDTH]
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[] | undefined;
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[]} [options.thumbnailWidth=DEFAULT_THUMBNAIL_WIDTH]
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 faviconsConfig = entries.find(([key]) => key === 'favicons');
109
+ const isFavicon = !!entries.find(([key]) => key === 'favicons');
110
+
98
111
  // @ts-ignore
99
- const appleTouchIconsConfig = entries.find(([key]) => key === 'apple-touch-icons');
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 (faviconsConfig) {
126
+ if (isFavicon) {
114
127
  const faviconConfigs = faviconSizes.map((w) => {
115
128
  return {
116
129
  ...configPairs,
@@ -124,16 +137,20 @@ export function generateResponseConfigs(options) {
124
137
  }
125
138
 
126
139
  // Handle apple-touch-icons directive - generate Apple touch icon sizes as PNG
127
- if (appleTouchIconsConfig) {
140
+
141
+ // console.log('**** Check:isAppleTouchIcon', isAppleTouchIcon);
142
+
143
+ if (isAppleTouchIcon) {
128
144
  const appleTouchConfigs = appleTouchSizes.map((w) => {
129
145
  return {
130
146
  ...configPairs,
131
147
  w: String(w),
132
148
  format: 'png',
133
- ensureAlpha: 'true'
149
+ ensureAlpha: 'true',
150
+ density: '300'
134
151
  };
135
152
  });
136
- // console.log('Returning apple-touch-icon configs:', appleTouchConfigs);
153
+ // console.log('**** Returning apple-touch-icon configs:', appleTouchConfigs);
137
154
  return appleTouchConfigs;
138
155
  }
139
156
 
@@ -178,6 +195,9 @@ export function generateDefaultDirectives(options) {
178
195
 
179
196
  // > Return metadata if directive 'responsive' is set
180
197
 
198
+ // @see https://github.com/JonasKruckenberg/
199
+ // imagetools/blob/main/docs/directives.md#metadata
200
+
181
201
  if (params.has('responsive')) {
182
202
  params.set('as', 'metadata');
183
203
  }
@@ -63,31 +63,57 @@ export async function generateViteConfig(options = {}) {
63
63
  if (enableImagetools) {
64
64
  try {
65
65
  const { imagetools } = await import('vite-imagetools');
66
- const {
67
- generateDefaultDirectives,
68
- generateResponseConfigs
69
- } = await import('./imagetools.js');
66
+ const { generateDefaultDirectives, generateResponseConfigs } =
67
+ await import('./imagetools.js');
70
68
 
71
- // Custom transform to ensure alpha channel for all PNGs
72
- const ensureAlphaTransform = (config) => {
73
- // Apply to all PNG images (harmless no-op if alpha exists)
74
- if (config.format !== 'png') return undefined;
75
-
76
- return (image) => {
77
- return image.ensureAlpha();
78
- };
79
- };
69
+ const sharp = (await import('sharp')).default;
80
70
 
81
71
  plugins.push(
82
72
  imagetools({
83
73
  defaultDirectives: generateDefaultDirectives(imagetoolsOptions),
84
74
  resolveConfigs: generateResponseConfigs(imagetoolsOptions),
85
- extendTransforms: (builtins) => [
86
- ...builtins,
87
- ensureAlphaTransform
88
- ]
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
+ // extendOutputFormats(builtins) {
84
+ // return {
85
+ // ...builtins,
86
+ // png: (config) => ({
87
+ // format: 'png',
88
+ // transform: (image) => image.withMetadata({ density: 300 })
89
+ // })
90
+ // };
91
+ // },
92
+ extendTransforms(builtins) {
93
+ const ensureAlphaTransform = (config, ctx) => {
94
+ // Check if 'ensureAlpha' directive is in the URL
95
+ if (!('ensureAlpha' in config)) {
96
+ return undefined; // This transform doesn't apply
97
+ }
98
+
99
+ // Mark the parameter as used
100
+ ctx.useParam('ensureAlpha');
101
+
102
+ // Return the actual transformation function
103
+ return (image) => {
104
+ return image
105
+ .ensureAlpha()
106
+ .withMetadata({ xmp: '' });
107
+ };
108
+ };
109
+
110
+ // Return an ARRAY with builtins + your custom transform
111
+ return [...builtins, ensureAlphaTransform];
112
+ }
89
113
  })
90
114
  );
115
+
116
+ // eslint-disable-next-line no-unused-vars
91
117
  } catch (error) {
92
118
  const errorMessage = `
93
119
  ╭─────────────────────────────────────────────────────────────╮
@@ -98,7 +124,9 @@ export async function generateViteConfig(options = {}) {
98
124
  │ Or set enableImagetools: false │
99
125
  ╰─────────────────────────────────────────────────────────────╯`;
100
126
  console.error(errorMessage);
101
- throw new Error('vite-imagetools is required when enableImagetools: true');
127
+ throw new Error(
128
+ 'vite-imagetools is required when enableImagetools: true'
129
+ );
102
130
  }
103
131
  }
104
132
 
@@ -110,10 +138,11 @@ export async function generateViteConfig(options = {}) {
110
138
  new Date().toISOString()
111
139
  ),
112
140
  ...customDefines
113
- },
141
+ }
114
142
  };
115
143
 
116
144
  if (enableVitest && !enableVitestWorkspace) {
145
+ // @ts-ignore
117
146
  config.test = {
118
147
  include: [
119
148
  'src/**/*.{test,spec}.{js,ts}',
@@ -127,6 +156,7 @@ export async function generateViteConfig(options = {}) {
127
156
  // Workspace mode: separate projects for jsdom and node tests
128
157
  // jsdom tests: *.svelte.test.js (component tests with jsdom)
129
158
  // node tests: *.test.js, *.spec.js (server-side tests)
159
+ // @ts-ignore
130
160
  config.test = {
131
161
  ...vitestOptions,
132
162
  projects: [
@@ -169,10 +199,7 @@ export async function generateViteConfig(options = {}) {
169
199
  * @returns {object} Define configuration object
170
200
  */
171
201
  export function generateViteDefines(options = {}) {
172
- const {
173
- packageJsonPath = './package.json',
174
- customDefines = {}
175
- } = options;
202
+ const { packageJsonPath = './package.json', customDefines = {} } = options;
176
203
 
177
204
  const packageJson = JSON.parse(
178
205
  readFileSync(resolve(packageJsonPath), 'utf-8')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.5.26",
3
+ "version": "0.5.28",
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",