Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 4842f93

Browse files
fix: allow using limit as string when you use loader with query string (#185)
1 parent c0341da commit 4842f93

11 files changed

+153
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module.exports = {
118118

119119
### `limit`
120120

121-
Type: `Number|Boolean`
121+
Type: `Number|Boolean|String`
122122
Default: `undefined`
123123

124124
The limit can be specified via loader options and defaults to no limit.

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function shouldTransform(limit, size) {
1616
return limit;
1717
}
1818

19-
if (typeof limit === 'number') {
19+
if (typeof limit === 'number' || typeof limit === 'string') {
2020
return size <= parseInt(limit, 10);
2121
}
2222

src/options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "object",
33
"properties": {
44
"limit": {
5-
"type": ["boolean", "number"]
5+
"type": ["boolean", "number", "string"]
66
},
77
"mimetype": {
88
"type": "string"

test/__snapshots__/limit-option.test.js.snap

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

test/__snapshots__/mimetype-option.test.js.snap

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

test/__snapshots__/validate-options.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`validation 1`] = `
44
"URL Loader Invalid Options
55
6-
options.limit should be boolean,number
6+
options.limit should be boolean,number,string
77
"
88
`;
99

test/limit-option.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,89 @@ describe('limit option', () => {
163163

164164
expect(source).toMatchSnapshot();
165165
});
166+
167+
it('0 ({String})', async () => {
168+
// Image size is 6777
169+
const config = {
170+
loader: {
171+
test: /\.png$/,
172+
options: {
173+
limit: '0',
174+
},
175+
},
176+
};
177+
178+
const stats = await webpack('fixture.js', config);
179+
const [{ source }] = stats.toJson().modules;
180+
181+
expect(source).toMatchSnapshot();
182+
});
183+
184+
it('0.1 ({String})', async () => {
185+
// Image size is 6777
186+
const config = {
187+
loader: {
188+
test: /\.png$/,
189+
options: {
190+
limit: '0.1',
191+
},
192+
},
193+
};
194+
195+
const stats = await webpack('fixture.js', config);
196+
const [{ source }] = stats.toJson().modules;
197+
198+
expect(source).toMatchSnapshot();
199+
});
200+
201+
it('6776 ({String})', async () => {
202+
// Image size is 6777
203+
const config = {
204+
loader: {
205+
test: /\.png$/,
206+
options: {
207+
limit: '6776',
208+
},
209+
},
210+
};
211+
212+
const stats = await webpack('fixture.js', config);
213+
const [{ source }] = stats.toJson().modules;
214+
215+
expect(source).toMatchSnapshot();
216+
});
217+
218+
it('6777 ({String})', async () => {
219+
// Image size is 6777
220+
const config = {
221+
loader: {
222+
test: /\.png$/,
223+
options: {
224+
limit: '6777',
225+
},
226+
},
227+
};
228+
229+
const stats = await webpack('fixture.js', config);
230+
const [{ source }] = stats.toJson().modules;
231+
232+
expect(source).toMatchSnapshot();
233+
});
234+
235+
it('6778 ({String})', async () => {
236+
// Image size is 6777
237+
const config = {
238+
loader: {
239+
test: /\.png$/,
240+
options: {
241+
limit: '6778',
242+
},
243+
},
244+
};
245+
246+
const stats = await webpack('fixture.js', config);
247+
const [{ source }] = stats.toJson().modules;
248+
249+
expect(source).toMatchSnapshot();
250+
});
166251
});

test/loader.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,30 @@ describe('Loader', () => {
1010
};
1111

1212
const stats = await webpack('fixture.js', config);
13-
const [{ source }] = stats.toJson().modules;
13+
const { modules, errors, warnings } = stats.toJson();
1414

15-
expect(source).toMatchSnapshot();
15+
expect(modules[0].source).toMatchSnapshot();
16+
expect(errors).toMatchSnapshot('errors');
17+
expect(warnings).toMatchSnapshot('warnings');
18+
});
19+
20+
it('should works when limit as a query string', async () => {
21+
const config = {
22+
rules: [
23+
{
24+
test: /\.png$/,
25+
use: {
26+
loader: `${require.resolve('../src')}?limit=10000`,
27+
},
28+
},
29+
],
30+
};
31+
32+
const stats = await webpack('fixture.js', config);
33+
const { modules, errors, warnings } = stats.toJson();
34+
35+
expect(modules[0].source).toMatchSnapshot();
36+
expect(errors).toMatchSnapshot('errors');
37+
expect(warnings).toMatchSnapshot('warnings');
1638
});
1739
});

test/mimetype-option.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@ describe('mimetype option', () => {
3030

3131
expect(source).toMatchSnapshot();
3232
});
33+
34+
it('unknown ({String})', async () => {
35+
const config = {
36+
loader: {
37+
test: /\.png$/,
38+
options: {
39+
mimetype: 'unknown/unknown',
40+
},
41+
},
42+
};
43+
44+
const stats = await webpack('fixture.js', config);
45+
const [{ source }] = stats.toJson().modules;
46+
47+
expect(source).toMatchSnapshot();
48+
});
3349
});

test/validate-options.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ it('validation', async () => {
1919

2020
expect(() => validate({ limit: 8192 })).not.toThrow();
2121
expect(() => validate({ limit: true })).not.toThrow();
22-
expect(() => validate({ limit: '8192' })).toThrowErrorMatchingSnapshot();
22+
expect(() => validate({ limit: '8192' })).not.toThrow();
23+
expect(() => validate({ limit: [] })).toThrowErrorMatchingSnapshot();
2324

2425
expect(() => validate({ mimetype: 'image/png' })).not.toThrow();
2526
expect(() => validate({ mimetype: true })).toThrowErrorMatchingSnapshot();

0 commit comments

Comments
 (0)