Skip to content

Commit

Permalink
feat: rename customOptionsFactory to optionsGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin-LL committed Jan 11, 2021
1 parent 4a81bee commit 678af94
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ import webpSrcSet from "./some_pic.jpg?srcset&format=webp";
// webpSrcSet will be "00[...]5.webp 480w, 40[...]3.webp 1024w, 76[...]b.webp 1920w, a4[...]c.webp 2560w, b1[...]c.webp"
```

| Name | Type | Default | Description |
| --------------------------------------------------- | ------------ | --------- | -------------------------------------------------------------------------------------------- |
| **[`sizes`](#sizes)** | `(string)[]` | undefined | Sizes in the output srcset. |
| **[`scaleUp`](#scaleup)** | `boolean` | `false` | Whether or not to scale up the image when the desired width is greater than the image width. |
| **[`customOptionsFactory`](#customoptionsfactory)** | `function` | undefined | A function that returns the option to be passed on to the next loader. |
| **[`esModule`](#esmodule)** | `boolean` | `true` | Whether the export is in ES modules syntax or CommonJS modules syntax |
| Name | Type | Default | Description |
| ------------------------------------------- | ------------ | --------- | -------------------------------------------------------------------------------------------- |
| **[`sizes`](#sizes)** | `(string)[]` | undefined | Sizes in the output srcset. |
| **[`scaleUp`](#scaleup)** | `boolean` | `false` | Whether or not to scale up the image when the desired width is greater than the image width. |
| **[`optionsGenerator`](#optionsgenerator)** | `function` | undefined | A function that returns the option to be passed on to the next loader. |
| **[`esModule`](#esmodule)** | `boolean` | `true` | Whether the export is in ES modules syntax or CommonJS modules syntax |

### `sizes`

Expand All @@ -108,7 +108,7 @@ When true, if the desired width is greater than the image width, the size will n

Note: this option has no effect on `"[number]x"` or `"original"`

### `customOptionsFactory`
### `optionsGenerator`

If you wish to use a resize loader other than [webpack-image-resize-loader](https://github.com/Calvin-LL/webpack-image-resize-loader). You may customize how the width and scale is passed down to that loader`s options.

Expand All @@ -122,12 +122,12 @@ If you wish to use a resize loader other than [webpack-image-resize-loader](http
}
```

For example, if `sizes` is `["10w", "1x", "2x", "original"]`, `customOptionsFactory` will be called with
For example, if `sizes` is `["10w", "1x", "2x", "original"]`, `optionsGenerator` will be called with

- `customOptionsFactory(10, undefined, existingOptions)` for `10w`
- `customOptionsFactory(undefined, 1, existingOptions)` for `1x`
- `customOptionsFactory(undefined, 2, existingOptions)` for `2x`
- `customOptionsFactory(undefined, undefined, existingOptions)` for `"original"`
- `optionsGenerator(10, undefined, existingOptions)` for `10w`
- `optionsGenerator(undefined, 1, existingOptions)` for `1x`
- `optionsGenerator(undefined, 2, existingOptions)` for `2x`
- `optionsGenerator(undefined, undefined, existingOptions)` for `"original"`

### `esModule`

Expand Down
10 changes: 3 additions & 7 deletions src/helpers/getRequireStringWithModifiedResizeLoaderOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function getRequireStringWithModifiedResizeLoaderOptions(
loaderIndex: number,
options: { width?: number; scale?: number },
resizeLoaderName: string,
customOptionsFactory: FullOptions["customOptionsFactory"]
optionsGenerator: FullOptions["optionsGenerator"]
): string {
const resizeLoaderResolvedPath = require.resolve(resizeLoaderName);
const resizeLoader = loaders.find(
Expand All @@ -31,18 +31,14 @@ export default function getRequireStringWithModifiedResizeLoaderOptions(
const resizeLoaderRequest = resizeLoader.request;
const resizeLoaderPath = resizeLoader.path;

if (customOptionsFactory)
if (optionsGenerator)
return remainingRequest.replace(
resizeLoaderRequest,
resizeLoaderPath +
"?" +
escapeJsonStringForLoader(
JSON.stringify(
customOptionsFactory(
options.width,
options.scale,
resizeLoaderOptions
)
optionsGenerator(options.width, options.scale, resizeLoaderOptions)
)
)
);
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface Options {
readonly sizes?: (`${number}w` | `${number}x` | "original")[];
readonly scaleUp?: boolean;
readonly resizeLoader?: string;
readonly customOptionsFactory?: (
readonly optionsGenerator?: (
width: number | undefined,
scale: number | undefined,
existingOptions: Record<string, any> | undefined
Expand Down Expand Up @@ -104,7 +104,7 @@ async function generateSrcSetString(
loaderIndex,
{},
options.resizeLoader,
options.customOptionsFactory
options.optionsGenerator
)}${requireEnd}${separator}`;

continue;
Expand All @@ -127,7 +127,7 @@ async function generateSrcSetString(
loaderIndex,
resizeLoaderOption,
options.resizeLoader,
options.customOptionsFactory
options.optionsGenerator
)}${requireEnd} ${size}${separator}`;
}

Expand Down
2 changes: 1 addition & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"description": "Name of resize loader. Default to \"webpack-image-resize-loader\".",
"type": "string"
},
"customOptionsFactory": {
"optionsGenerator": {
"description": "A function that returns the option to be passed on to webpack-image-resize-loader.",
"instanceof": "Function"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import WISLWebpackTestCompiler from "./helpers/WISLWebpackTestCompiler";

describe.each([4, 5] as const)(
'v%d "customOptionsFactory" option',
'v%d "optionsGenerator" option',
(webpackVersion) => {
it("should work with a function", async () => {
const mockCustomOptionsFactory: (
const mockOptionsGenerator: (
width: number | undefined,
scale: number | undefined,
existingOptions: Record<string, any>
Expand All @@ -18,18 +18,18 @@ describe.each([4, 5] as const)(
const bundle = await compiler.compile({
loaderOptions: {
sizes: ["2x", "original", "1x", "300w"],
customOptionsFactory: (
optionsGenerator: (
width: number | undefined,
scale: number | undefined,
existingOptions: Record<string, any>
) => mockCustomOptionsFactory(width, scale, existingOptions),
) => mockOptionsGenerator(width, scale, existingOptions),
},
});

expect(bundle.execute("main.js")).toMatchSnapshot("result");

expect(mockCustomOptionsFactory).toHaveBeenCalled();
expect(mockCustomOptionsFactory).toMatchSnapshot();
expect(mockOptionsGenerator).toHaveBeenCalled();
expect(mockOptionsGenerator).toMatchSnapshot();
});
}
);
2 changes: 1 addition & 1 deletion test/e2e/validate-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe.each([4, 5] as const)("v%d validate options", (webpackVersion) => {
success: ["webpack-image-resize-loader"],
failure: [3],
},
customOptionsFactory: {
optionsGenerator: {
success: [() => ({})],
failure: [true],
},
Expand Down
22 changes: 11 additions & 11 deletions test/unit/getRequireStringWithModifiedResizeLoaderOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ it("should handle resizeLoaderPath options being string", () => {
];
const loaderIndex = 0;
const options = {};
const customOptionsFactory = undefined;
const optionsGenerator = undefined;

const result = getRequireStringWithModifiedResizeLoaderOptions(
remainingRequest,
loaders,
loaderIndex,
options,
resizeLoaderName,
customOptionsFactory
optionsGenerator
);

expect(result).toMatchInlineSnapshot(
Expand Down Expand Up @@ -72,15 +72,15 @@ it("should handle resizeLoaderPath options being object", () => {
];
const loaderIndex = 0;
const options = {};
const customOptionsFactory = undefined;
const optionsGenerator = undefined;

const result = getRequireStringWithModifiedResizeLoaderOptions(
remainingRequest,
loaders,
loaderIndex,
options,
resizeLoaderName,
customOptionsFactory
optionsGenerator
);

expect(result).toMatchInlineSnapshot(
Expand Down Expand Up @@ -116,15 +116,15 @@ it("should handle resizeLoaderPath options being undefined", () => {
];
const loaderIndex = 0;
const options = {};
const customOptionsFactory = undefined;
const optionsGenerator = undefined;

const result = getRequireStringWithModifiedResizeLoaderOptions(
remainingRequest,
loaders,
loaderIndex,
options,
resizeLoaderName,
customOptionsFactory
optionsGenerator
);

expect(result).toMatchInlineSnapshot(
Expand Down Expand Up @@ -160,7 +160,7 @@ it("should throw if resizeLoader not found", () => {
];
const loaderIndex = 0;
const options = {};
const customOptionsFactory = undefined;
const optionsGenerator = undefined;

expect(() =>
getRequireStringWithModifiedResizeLoaderOptions(
Expand All @@ -169,14 +169,14 @@ it("should throw if resizeLoader not found", () => {
loaderIndex,
options,
"file-loader",
customOptionsFactory
optionsGenerator
)
).toThrowErrorMatchingInlineSnapshot(
`"Can't find file-loader in the list of loaders"`
);
});

it("should handle customOptionsFactory", () => {
it("should handle optionsGenerator", () => {
const urlLoaderPath = require.resolve("url-loader");
const resizeLoaderName = "webpack-image-resize-loader";
const resizeLoaderPath = require.resolve(resizeLoaderName);
Expand Down Expand Up @@ -204,15 +204,15 @@ it("should handle customOptionsFactory", () => {
];
const loaderIndex = 0;
const options = {};
const customOptionsFactory = (): Record<string, any> => ({ test: "true" });
const optionsGenerator = (): Record<string, any> => ({ test: "true" });

const result = getRequireStringWithModifiedResizeLoaderOptions(
remainingRequest,
loaders,
loaderIndex,
options,
resizeLoaderName,
customOptionsFactory
optionsGenerator
);

expect(result).toMatchInlineSnapshot(
Expand Down

0 comments on commit 678af94

Please sign in to comment.