Skip to content

Commit 7ed891c

Browse files
committed
fix: inline loader options not being parsed correctly
1 parent a570c12 commit 7ed891c

5 files changed

Lines changed: 69 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
"dependencies": {
4545
"@calvin-l/webpack-loader-util": "^1.0.2",
46+
"loader-utils": "^2.0.0",
4647
"schema-utils": "^3.0.0",
4748
"sharp": "^0.26.3"
4849
},

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseQuery } from "loader-utils";
12
import { validate } from "schema-utils";
23
import { Schema } from "schema-utils/declarations/validate";
34
import sharp from "sharp";
@@ -142,7 +143,10 @@ function addOptionsToResizeLoader(
142143
const nextLoader = loaders[loaderIndex + 1];
143144
const isNextLoaderQueryLoader = getIsLoaderQueryLoader(nextLoader);
144145

145-
const resizeLoaderOptions = nextLoader.options;
146+
const resizeLoaderOptions =
147+
typeof nextLoader.options === "string"
148+
? parseQuery("?" + nextLoader.options)
149+
: nextLoader.options;
146150
const resizeLoaderRequest = nextLoader.request;
147151
const resizeLoaderPath = nextLoader.path;
148152

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`v4 resize loader with options should work with options in query: result 1`] = `
4+
Object {
5+
"default": "76ab12b37821a8c18371948d1be1b6c1.jpg 300w, bd2b9fdf768b5eb139702c4c3b4573de.jpg",
6+
}
7+
`;
8+
9+
exports[`v4 resize loader with options should work with options in webpackConfig: result 1`] = `"76ab12b37821a8c18371948d1be1b6c1.jpg 300w, bd2b9fdf768b5eb139702c4c3b4573de.jpg"`;
10+
11+
exports[`v5 resize loader with options should work with options in query: result 1`] = `
12+
Object {
13+
"default": "76ab12b37821a8c18371948d1be1b6c1.jpg 300w, bd2b9fdf768b5eb139702c4c3b4573de.jpg",
14+
}
15+
`;
16+
17+
exports[`v5 resize loader with options should work with options in webpackConfig: result 1`] = `"76ab12b37821a8c18371948d1be1b6c1.jpg 300w, bd2b9fdf768b5eb139702c4c3b4573de.jpg"`;

test/e2e/helpers/WISLWebpackTestCompiler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import {
99
interface WISLCompileOptions extends Omit<CompileOptions, "entryFilePath"> {
1010
entryFileName?: string;
1111
loaderOptions?: any;
12+
resizeLoaderOptions?: any;
1213
useQueryLoader?: boolean;
1314
}
1415

1516
export default class WISLWebpackTestCompiler extends WebpackTestCompiler {
1617
compile(options: WISLCompileOptions = {}): Promise<WebpackTestBundle> {
1718
const {
1819
loaderOptions = {},
20+
resizeLoaderOptions,
1921
entryFileName = "index.js",
2022
useQueryLoader = false,
2123
} = options;
@@ -27,7 +29,7 @@ export default class WISLWebpackTestCompiler extends WebpackTestCompiler {
2729
rules: [
2830
useQueryLoader
2931
? {
30-
test: /\.(png|jpg|svg)/i,
32+
test: /\.(png|jpg|svg)$/i,
3133
use: [
3234
{
3335
loader: "webpack-query-loader",
@@ -52,14 +54,15 @@ export default class WISLWebpackTestCompiler extends WebpackTestCompiler {
5254
],
5355
}
5456
: {
55-
test: /\.(png|jpg|svg)/i,
57+
test: /\.(png|jpg|svg)$/i,
5658
use: [
5759
{
5860
loader: path.resolve(__dirname, "../../../test-dist/cjs.js"),
5961
options: loaderOptions,
6062
},
6163
{
6264
loader: "webpack-image-resize-loader",
65+
options: resizeLoaderOptions,
6366
},
6467
],
6568
},
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import path from "path";
2+
3+
import WISLWebpackTestCompiler from "./helpers/WISLWebpackTestCompiler";
4+
5+
describe.each([4, 5] as const)(
6+
"v%d resize loader with options",
7+
(webpackVersion) => {
8+
it("should work with options in webpackConfig", async () => {
9+
const compiler = new WISLWebpackTestCompiler({ webpackVersion });
10+
const bundle = await compiler.compile({
11+
loaderOptions: {
12+
sizes: ["300w", "original"],
13+
},
14+
resizeLoaderOptions: {
15+
quality: 80,
16+
},
17+
});
18+
19+
expect(bundle.execute("main.js")).toMatchSnapshot("result");
20+
});
21+
22+
it("should work with options in query", async () => {
23+
const loaderPath = path.resolve(__dirname, "../../test-dist/cjs.js");
24+
const loaderOptions = {
25+
sizes: ["300w", "original"],
26+
};
27+
const loaderOptionsString = JSON.stringify(loaderOptions);
28+
const resizeLoaderOptions = {
29+
quality: 80,
30+
};
31+
const resizeLoaderOptionssString = JSON.stringify(resizeLoaderOptions);
32+
33+
const compiler = new WISLWebpackTestCompiler({ webpackVersion });
34+
const bundle = await compiler.compile({
35+
fileContentOverride: `__export__ = require('-!${loaderPath}?${loaderOptionsString}!webpack-image-resize-loader?${resizeLoaderOptionssString}!./Macaca_nigra_self-portrait_large.jpg');`,
36+
});
37+
38+
expect(bundle.execute("main.js")).toMatchSnapshot("result");
39+
});
40+
}
41+
);

0 commit comments

Comments
 (0)