Skip to content

Commit ed46535

Browse files
committed
add more debug logs around image service
1 parent 5148c5f commit ed46535

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

packages/astro-image-service-ng/src/index.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import type { Config } from './service/config'
33

44
function integration(config: Config): AstroIntegration {
55
return {
6-
name: '@jcayzac/astro-image-service-ng/service',
6+
name: '@jcayzac/astro-image-service-ng',
77
hooks: {
8-
'astro:config:setup': (options) => {
9-
const { updateConfig } = options
10-
8+
'astro:config:setup': ({ updateConfig }) => {
119
updateConfig({
1210
image: {
1311
service: {
@@ -22,11 +20,8 @@ function integration(config: Config): AstroIntegration {
2220
rollupOptions: {
2321
output: {
2422
manualChunks: (id: string) => {
25-
if (!/\/@jcayzac\/astro-image-service-ng\b/.test(id)) {
26-
return
27-
}
28-
29-
return 'image-service'
23+
const separate = /\/@jcayzac\/astro-image-service-ng\b/.test(id)
24+
return separate ? 'image-service' : undefined
3025
},
3126
},
3227
},

packages/astro-image-service-ng/src/service/index.ts

+68-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inspect } from 'node:util'
12
import sharp, { type FormatEnum, type ResizeOptions } from 'sharp'
23
import type { LocalImageService } from 'astro'
34
import { baseService } from 'astro/assets'
@@ -19,22 +20,75 @@ export interface Transform {
1920
}
2021

2122
const service: LocalImageService<Config> = {
22-
propertiesToHash: baseService.propertiesToHash ?? [],
23-
validateOptions: baseService.validateOptions,
24-
getURL: baseService.getURL,
25-
parseURL: baseService.parseURL,
26-
getHTMLAttributes: baseService.getHTMLAttributes,
27-
getSrcSet: baseService.getSrcSet,
28-
async transform(inputBuffer, transformOptions, config) {
23+
propertiesToHash: ['src', 'width', 'height', 'format', 'quality'],
24+
25+
validateOptions: (options, config) => {
26+
if (config.service.config._debug)
27+
globalThis.console.log(`in: validateOptions(${inspect({ options, config })})`)
28+
29+
const result = baseService.validateOptions?.(options, config) ?? options
30+
31+
if (config.service.config._debug)
32+
globalThis.console.log(`out: validateOptions(…) = ${inspect(result)}`)
33+
},
34+
35+
getURL: (options, config) => {
36+
if (config.service.config._debug)
37+
globalThis.console.log(`in: getURL(${inspect({ options, config })})`)
38+
39+
const result = baseService.getURL(options, config)
40+
41+
if (config.service.config._debug)
42+
globalThis.console.log(`out: getURL(…) = ${inspect(result)}`)
43+
},
44+
45+
parseURL: (url, config) => {
46+
if (config.service.config._debug)
47+
globalThis.console.log(`in: parseURL(${inspect({ url, config })})`)
48+
49+
const result = baseService.parseURL(url, config)
50+
51+
if (config.service.config._debug)
52+
globalThis.console.log(`out: parseURL(…) = ${inspect(result)}`)
53+
},
54+
55+
getHTMLAttributes: (options, config) => {
2956
if (config.service.config._debug)
30-
globalThis.console.log('Made it to the transform function', { transformOptions, config })
57+
globalThis.console.log(`in: getHTMLAttributes(${inspect({ options, config })})`)
3158

32-
const transform: Transform = transformOptions as Transform
59+
const result = baseService.getHTMLAttributes
60+
61+
if (config.service.config._debug)
62+
globalThis.console.log(`out: getHTMLAttributes(…) = ${inspect(result)}`)
63+
},
64+
65+
getSrcSet: (options, config) => {
66+
if (config.service.config._debug)
67+
globalThis.console.log(`in: getSrcSet(${inspect({ options, config })})`)
68+
69+
const result = baseService.getSrcSet?.(options, config)
70+
71+
if (config.service.config._debug)
72+
globalThis.console.log(`out: getSrcSet(…) = ${inspect(result)}`)
73+
74+
return result
75+
},
76+
77+
async transform(inputBuffer, options, config) {
78+
if (config.service.config._debug)
79+
globalThis.console.log(`in: transform(${inspect({ options, config })})`)
80+
81+
const transform: Transform = options as Transform
3382

3483
if (transform.format === 'svg') {
3584
// FIXME: Returning the input buffer here assumes it's SVG, but it could be anything.
3685
// TODO: Add support for SVG image tracing.
37-
return { data: inputBuffer, format: 'svg' }
86+
const result = { data: inputBuffer, format: 'svg' }
87+
88+
if (config.service.config._debug)
89+
globalThis.console.log(`out: transform(…) = ${inspect(result)}`)
90+
91+
return result
3892
}
3993

4094
const result = sharp(inputBuffer, {
@@ -65,6 +119,10 @@ const service: LocalImageService<Config> = {
65119
}
66120

67121
const { data, info: { format } } = await result.toBuffer({ resolveWithObject: true })
122+
123+
if (config.service.config._debug)
124+
globalThis.console.log(`out: transform(…) = ${inspect({ data, format })}`)
125+
68126
return {
69127
data,
70128
format,

0 commit comments

Comments
 (0)