Skip to content

Commit 04e2aa8

Browse files
committed
tests passing
1 parent 439ebd8 commit 04e2aa8

10 files changed

+60
-51
lines changed

src/index.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import type {
88
Options,
99
ParsedOptions,
1010
LoaderContext,
11-
AdapterClass,
11+
AdapterImplementation,
1212
MimeType,
13-
AdapterResizeResults,
13+
AdapterResizeResponse,
1414
} from "./types"
1515

1616
import schema from "./schema.json"
@@ -29,7 +29,7 @@ const DEFAULTS = {
2929
* **Responsive Loader**
3030
*
3131
* Creates multiple images from one source image, and returns a srcset
32-
* [Responsive Images Loader](https://github.com/dazuaz/responsive-loader)
32+
* [Responsive Loader](https://github.com/dazuaz/responsive-loader)
3333
*
3434
* @method loader
3535
*
@@ -46,7 +46,9 @@ export default function loader(this: LoaderContext, content: Buffer): void {
4646
// @ts-ignore
4747
validate(schema, options, { name: "Responsive Loader" })
4848

49-
// parses options and set defaults options
49+
/**
50+
* Parses options and set defaults options
51+
*/
5052
const {
5153
outputContext,
5254
outputPlaceholder,
@@ -61,14 +63,12 @@ export default function loader(this: LoaderContext, content: Buffer): void {
6163
sizes,
6264
}: ParsedOptions = parseOptions(this, options)
6365

64-
// Not undefined
6566
if (typeof loaderCallback == "undefined") {
6667
new Error("Responsive loader callback error")
6768
return
6869
}
6970

7071
if (!sizes.length) {
71-
// pass
7272
loaderCallback(null, content)
7373
return
7474
}
@@ -78,7 +78,7 @@ export default function loader(this: LoaderContext, content: Buffer): void {
7878
return
7979
}
8080

81-
const createFile = ({ data, width, height }: AdapterResizeResults) => {
81+
const createFile = ({ data, width, height }: AdapterResizeResponse) => {
8282
const fileName = interpolateName(this, name, {
8383
context: outputContext,
8484
content: data,
@@ -103,24 +103,28 @@ export default function loader(this: LoaderContext, content: Buffer): void {
103103
}
104104
}
105105

106-
// Disable processing of images by this loader (useful in development)
106+
/**
107+
* Disable processing of images by this loader (useful in development)
108+
*/
107109
if (options.disable) {
108110
const { path } = createFile({ data: content, width: 100, height: 100 })
109111
loaderCallback(
110112
null,
111113
`${options.esModule ? "export default" : "module.exports ="} {
112-
srcSet:${path},
113-
images:[{path:${path},width:100,height:100}],
114+
srcSet: ${path},
115+
images: [{path:${path},width:100,height:100}],
114116
src: ${path},
115-
toString:function(){return ${path}}
117+
toString: function(){return ${path}}
116118
};`
117119
)
118120
return
119121
}
120122

121123
const adapter: Adapter = options.adapter || require("./adapters/jimp")
122124

123-
// The full config is passed to the adapter, later sources' properties overwrite earlier ones.
125+
/**
126+
* The full config is passed to the adapter, later sources' properties overwrite earlier ones.
127+
*/
124128
const adapterOptions = Object.assign({}, options, {
125129
quality,
126130
background,
@@ -149,9 +153,9 @@ export default function loader(this: LoaderContext, content: Buffer): void {
149153
null,
150154
`${options.esModule ? "export default" : "module.exports ="} {
151155
srcSet: ${srcset},
152-
images:[ ${images}],
156+
images: [${images}],
153157
src: ${firstImage.path},
154-
toString:function(){return ${firstImage.path}},
158+
toString: function(){return ${firstImage.path}},
155159
${placeholder ? "placeholder: " + placeholder + "," : ""}
156160
width: ${firstImage.width},
157161
height: ${firstImage.height}
@@ -171,13 +175,13 @@ export default function loader(this: LoaderContext, content: Buffer): void {
171175
*/
172176

173177
async function transformations(
174-
img: AdapterClass,
178+
img: AdapterImplementation,
175179
sizes: number[],
176180
mime: MimeType,
177181
outputPlaceholder: boolean,
178182
placeholderSize: number,
179183
adapterOptions: Options
180-
): Promise<AdapterResizeResults[]> {
184+
): Promise<AdapterResizeResponse[]> {
181185
const metadata = await img.metadata()
182186
const promises = []
183187
const widthsToGenerate = new Set()

src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ export type ParsedOptions = {
4242
sizes: number[]
4343
}
4444

45-
export type Adapter = (imagePath: string) => AdapterClass
45+
export type Adapter = (imagePath: string) => AdapterImplementation
4646

47-
export interface AdapterClass {
47+
export interface AdapterImplementation {
4848
metadata: () => Promise<{ width: number; height: number }>
49-
resize: (config: { width: number; mime: string; options: Options }) => Promise<AdapterResizeResults>
49+
resize: (config: { width: number; mime: string; options: Options }) => Promise<AdapterResizeResponse>
5050
}
51-
export type AdapterResizeResults = { data: Buffer; width: number; height: number }
51+
export type AdapterResizeResponse = { data: Buffer; width: number; height: number }
5252
export type CreateFile = {
5353
loaderContext: LoaderContext
5454
data: Buffer
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/sharp/build/__snapshots__/test.js.snap

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,6 @@ Object {
3636
}
3737
`;
3838

39-
exports[`jpg to avif 1`] = `
40-
Object {
41-
"default": Object {
42-
"height": 699,
43-
"images": Array [
44-
Object {
45-
"height": 699,
46-
"path": "foobar/efa5d854dddd643ff86e78ea919a3c5e-777.avif",
47-
"width": 777,
48-
},
49-
],
50-
"src": "foobar/efa5d854dddd643ff86e78ea919a3c5e-777.avif",
51-
"srcSet": "foobar/efa5d854dddd643ff86e78ea919a3c5e-777.avif 777w",
52-
"toString": [Function],
53-
"width": 777,
54-
},
55-
}
56-
`;
57-
5839
exports[`jpg to webp 1`] = `
5940
Object {
6041
"default": Object {
@@ -284,21 +265,45 @@ Object {
284265
exports[`png to avif 1`] = `
285266
Object {
286267
"default": Object {
287-
"height": 580,
268+
"height": 595,
288269
"images": Array [
289-
Object {
290-
"height": 580,
291-
"path": "foobar/426873cddcfae03f71fa5ff4de43cc25-500.avif",
292-
"width": 500,
293-
},
294270
Object {
295271
"height": 595,
296272
"path": "foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif",
297273
"width": 513,
298274
},
299275
],
300-
"src": "foobar/426873cddcfae03f71fa5ff4de43cc25-500.avif",
301-
"srcSet": "foobar/426873cddcfae03f71fa5ff4de43cc25-500.avif 500w,foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif 513w",
276+
"src": "foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif",
277+
"srcSet": "foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif 513w",
278+
"toString": [Function],
279+
"width": 513,
280+
},
281+
}
282+
`;
283+
284+
exports[`png to avif 2`] = `
285+
Object {
286+
"default": Object {
287+
"height": 450,
288+
"images": Array [
289+
Object {
290+
"height": 450,
291+
"path": "foobar/38a6d99027dbc677773a00929d989048-500.avif",
292+
"width": 500,
293+
},
294+
Object {
295+
"height": 675,
296+
"path": "foobar/5c529246a1a1bf2de0a188d3c36697c6-750.avif",
297+
"width": 750,
298+
},
299+
Object {
300+
"height": 900,
301+
"path": "foobar/0dd6b3f08687c424d0c4706f386f62b7-1000.avif",
302+
"width": 1000,
303+
},
304+
],
305+
"src": "foobar/38a6d99027dbc677773a00929d989048-500.avif",
306+
"srcSet": "foobar/38a6d99027dbc677773a00929d989048-500.avif 500w,foobar/5c529246a1a1bf2de0a188d3c36697c6-750.avif 750w,foobar/0dd6b3f08687c424d0c4706f386f62b7-1000.avif 1000w",
302307
"toString": [Function],
303308
"width": 500,
304309
},
Binary file not shown.

test/sharp/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
test("jpg to avif", () => {
2-
const output = require("../cat-1000.jpg?format=avif&size=777")
1+
test("png to avif", () => {
2+
const output = require("../cat-transparent.png?format=avif&size=777")
33
expect(output).toMatchSnapshot()
44
})
55
test("png to avif", () => {
6-
const output = require("../cat-transparent.png?format=avif")
6+
const output = require("../cat-1000.jpg?format=avif")
77
expect(output).toMatchSnapshot()
88
})
99
test("preserves rotation", () => {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* Strict Type-Checking Options */
1010
"strict": true /* Enable all strict type-checking options. */,
1111
"moduleResolution": "node",
12-
"sourceMap": true,
12+
// "sourceMap": true,
1313
/* Additional Checks */
1414
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
1515
"resolveJsonModule": true,

0 commit comments

Comments
 (0)