Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuaz committed Dec 25, 2020
1 parent 439ebd8 commit 04e2aa8
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 51 deletions.
36 changes: 20 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
Options,
ParsedOptions,
LoaderContext,
AdapterClass,
AdapterImplementation,
MimeType,
AdapterResizeResults,
AdapterResizeResponse,
} from "./types"

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

// parses options and set defaults options
/**
* Parses options and set defaults options
*/
const {
outputContext,
outputPlaceholder,
Expand All @@ -61,14 +63,12 @@ export default function loader(this: LoaderContext, content: Buffer): void {
sizes,
}: ParsedOptions = parseOptions(this, options)

// Not undefined
if (typeof loaderCallback == "undefined") {
new Error("Responsive loader callback error")
return
}

if (!sizes.length) {
// pass
loaderCallback(null, content)
return
}
Expand All @@ -78,7 +78,7 @@ export default function loader(this: LoaderContext, content: Buffer): void {
return
}

const createFile = ({ data, width, height }: AdapterResizeResults) => {
const createFile = ({ data, width, height }: AdapterResizeResponse) => {
const fileName = interpolateName(this, name, {
context: outputContext,
content: data,
Expand All @@ -103,24 +103,28 @@ export default function loader(this: LoaderContext, content: Buffer): void {
}
}

// Disable processing of images by this loader (useful in development)
/**
* Disable processing of images by this loader (useful in development)
*/
if (options.disable) {
const { path } = createFile({ data: content, width: 100, height: 100 })
loaderCallback(
null,
`${options.esModule ? "export default" : "module.exports ="} {
srcSet:${path},
images:[{path:${path},width:100,height:100}],
srcSet: ${path},
images: [{path:${path},width:100,height:100}],
src: ${path},
toString:function(){return ${path}}
toString: function(){return ${path}}
};`
)
return
}

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

// The full config is passed to the adapter, later sources' properties overwrite earlier ones.
/**
* The full config is passed to the adapter, later sources' properties overwrite earlier ones.
*/
const adapterOptions = Object.assign({}, options, {
quality,
background,
Expand Down Expand Up @@ -149,9 +153,9 @@ export default function loader(this: LoaderContext, content: Buffer): void {
null,
`${options.esModule ? "export default" : "module.exports ="} {
srcSet: ${srcset},
images:[ ${images}],
images: [${images}],
src: ${firstImage.path},
toString:function(){return ${firstImage.path}},
toString: function(){return ${firstImage.path}},
${placeholder ? "placeholder: " + placeholder + "," : ""}
width: ${firstImage.width},
height: ${firstImage.height}
Expand All @@ -171,13 +175,13 @@ export default function loader(this: LoaderContext, content: Buffer): void {
*/

async function transformations(
img: AdapterClass,
img: AdapterImplementation,
sizes: number[],
mime: MimeType,
outputPlaceholder: boolean,
placeholderSize: number,
adapterOptions: Options
): Promise<AdapterResizeResults[]> {
): Promise<AdapterResizeResponse[]> {
const metadata = await img.metadata()
const promises = []
const widthsToGenerate = new Set()
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export type ParsedOptions = {
sizes: number[]
}

export type Adapter = (imagePath: string) => AdapterClass
export type Adapter = (imagePath: string) => AdapterImplementation

export interface AdapterClass {
export interface AdapterImplementation {
metadata: () => Promise<{ width: number; height: number }>
resize: (config: { width: number; mime: string; options: Options }) => Promise<AdapterResizeResults>
resize: (config: { width: number; mime: string; options: Options }) => Promise<AdapterResizeResponse>
}
export type AdapterResizeResults = { data: Buffer; width: number; height: number }
export type AdapterResizeResponse = { data: Buffer; width: number; height: number }
export type CreateFile = {
loaderContext: LoaderContext
data: Buffer
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
59 changes: 32 additions & 27 deletions test/sharp/build/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,6 @@ Object {
}
`;

exports[`jpg to avif 1`] = `
Object {
"default": Object {
"height": 699,
"images": Array [
Object {
"height": 699,
"path": "foobar/efa5d854dddd643ff86e78ea919a3c5e-777.avif",
"width": 777,
},
],
"src": "foobar/efa5d854dddd643ff86e78ea919a3c5e-777.avif",
"srcSet": "foobar/efa5d854dddd643ff86e78ea919a3c5e-777.avif 777w",
"toString": [Function],
"width": 777,
},
}
`;

exports[`jpg to webp 1`] = `
Object {
"default": Object {
Expand Down Expand Up @@ -284,21 +265,45 @@ Object {
exports[`png to avif 1`] = `
Object {
"default": Object {
"height": 580,
"height": 595,
"images": Array [
Object {
"height": 580,
"path": "foobar/426873cddcfae03f71fa5ff4de43cc25-500.avif",
"width": 500,
},
Object {
"height": 595,
"path": "foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif",
"width": 513,
},
],
"src": "foobar/426873cddcfae03f71fa5ff4de43cc25-500.avif",
"srcSet": "foobar/426873cddcfae03f71fa5ff4de43cc25-500.avif 500w,foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif 513w",
"src": "foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif",
"srcSet": "foobar/2dd56762ba549a1d513c236b0a3435a6-513.avif 513w",
"toString": [Function],
"width": 513,
},
}
`;

exports[`png to avif 2`] = `
Object {
"default": Object {
"height": 450,
"images": Array [
Object {
"height": 450,
"path": "foobar/38a6d99027dbc677773a00929d989048-500.avif",
"width": 500,
},
Object {
"height": 675,
"path": "foobar/5c529246a1a1bf2de0a188d3c36697c6-750.avif",
"width": 750,
},
Object {
"height": 900,
"path": "foobar/0dd6b3f08687c424d0c4706f386f62b7-1000.avif",
"width": 1000,
},
],
"src": "foobar/38a6d99027dbc677773a00929d989048-500.avif",
"srcSet": "foobar/38a6d99027dbc677773a00929d989048-500.avif 500w,foobar/5c529246a1a1bf2de0a188d3c36697c6-750.avif 750w,foobar/0dd6b3f08687c424d0c4706f386f62b7-1000.avif 1000w",
"toString": [Function],
"width": 500,
},
Expand Down
Binary file not shown.
6 changes: 3 additions & 3 deletions test/sharp/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
test("jpg to avif", () => {
const output = require("../cat-1000.jpg?format=avif&size=777")
test("png to avif", () => {
const output = require("../cat-transparent.png?format=avif&size=777")
expect(output).toMatchSnapshot()
})
test("png to avif", () => {
const output = require("../cat-transparent.png?format=avif")
const output = require("../cat-1000.jpg?format=avif")
expect(output).toMatchSnapshot()
})
test("preserves rotation", () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"moduleResolution": "node",
"sourceMap": true,
// "sourceMap": true,
/* Additional Checks */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"resolveJsonModule": true,
Expand Down

0 comments on commit 04e2aa8

Please sign in to comment.