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

Commit d82e453

Browse files
fix: limit should always be a number and 0 value handles as number (#180)
BREAKING CHANGE: `limit` should always be a number and 0 value handles as number
1 parent 3c24545 commit d82e453

6 files changed

+56
-32
lines changed

src/index.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,21 @@ import mime from 'mime';
1111
import normalizeFallback from './utils/normalizeFallback';
1212
import schema from './options.json';
1313

14-
// Loader Mode
15-
export const raw = true;
16-
1714
export default function loader(src) {
1815
// Loader Options
1916
const options = getOptions(this) || {};
2017

2118
validateOptions(schema, options, 'URL Loader');
2219

23-
const file = this.resourcePath;
2420
// Set limit for resource inlining (file size)
25-
let limit = options.limit;
26-
27-
if (limit) {
28-
limit = parseInt(limit, 10);
29-
}
30-
// Get MIME type
31-
const mimetype = options.mimetype || mime.getType(file);
21+
const limit = options.limit ? parseInt(options.limit, 10) : options.limit;
3222

3323
// No limit or within the specified limit
34-
if (!limit || src.length <= limit) {
24+
if ((!limit && typeof limit !== 'number') || src.length <= limit) {
25+
const file = this.resourcePath;
26+
// Get MIME type
27+
const mimetype = options.mimetype || mime.getType(file);
28+
3529
if (typeof src === 'string') {
3630
src = Buffer.from(src);
3731
}
@@ -58,3 +52,6 @@ export default function loader(src) {
5852

5953
return fallback.call(fallbackLoaderContext, src);
6054
}
55+
56+
// Loader Mode
57+
export const raw = true;

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": ["string", "number"]
5+
"type": ["number"]
66
},
77
"mimetype": {
88
"type": "string"

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

Lines changed: 6 additions & 4 deletions
Large diffs are not rendered by default.

test/__snapshots__/errors.test.js.snap renamed to test/__snapshots__/validate-options.test.js.snap

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

1010
exports[`validation 2`] = `
1111
"URL Loader Invalid Options
1212
13-
options.mimetype should be string
13+
options.limit should be number
1414
"
1515
`;
1616

1717
exports[`validation 3`] = `
1818
"URL Loader Invalid Options
1919
20+
options.mimetype should be string
21+
"
22+
`;
23+
24+
exports[`validation 4`] = `
25+
"URL Loader Invalid Options
26+
2027
options.fallback should be string
2128
options.fallback should be object
2229
options.fallback should match some schema in anyOf

test/limit-option.test.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ describe('limit option', () => {
1515
expect(source).toMatchSnapshot();
1616
});
1717

18-
it('{Number} (6776)', async () => {
18+
it('{Number} (0)', async () => {
1919
// Image size is 6777
2020
const config = {
2121
loader: {
2222
test: /\.png$/,
2323
options: {
24-
limit: 6776,
24+
limit: 0,
2525
},
2626
},
2727
};
@@ -32,13 +32,13 @@ describe('limit option', () => {
3232
expect(source).toMatchSnapshot();
3333
});
3434

35-
it('{Number} (6777)', async () => {
35+
it('{Number} (0.1)', async () => {
3636
// Image size is 6777
3737
const config = {
3838
loader: {
3939
test: /\.png$/,
4040
options: {
41-
limit: 6777,
41+
limit: 0.1,
4242
},
4343
},
4444
};
@@ -49,13 +49,13 @@ describe('limit option', () => {
4949
expect(source).toMatchSnapshot();
5050
});
5151

52-
it('{Number} (6778)', async () => {
52+
it('{Number} (6776)', async () => {
5353
// Image size is 6777
5454
const config = {
5555
loader: {
5656
test: /\.png$/,
5757
options: {
58-
limit: 6778,
58+
limit: 6776,
5959
},
6060
},
6161
};
@@ -66,12 +66,13 @@ describe('limit option', () => {
6666
expect(source).toMatchSnapshot();
6767
});
6868

69-
it('{Number} (big)', async () => {
69+
it('{Number} (6777)', async () => {
70+
// Image size is 6777
7071
const config = {
7172
loader: {
7273
test: /\.png$/,
7374
options: {
74-
limit: Number.MAX_SAFE_INTEGER,
75+
limit: 6777,
7576
},
7677
},
7778
};
@@ -82,12 +83,13 @@ describe('limit option', () => {
8283
expect(source).toMatchSnapshot();
8384
});
8485

85-
it('{Number} (less)', async () => {
86+
it('{Number} (6778)', async () => {
87+
// Image size is 6777
8688
const config = {
8789
loader: {
8890
test: /\.png$/,
8991
options: {
90-
limit: Number.MIN_SAFE_INTEGER,
92+
limit: 6778,
9193
},
9294
},
9395
};
@@ -98,12 +100,28 @@ describe('limit option', () => {
98100
expect(source).toMatchSnapshot();
99101
});
100102

101-
it('{String} (big)', async () => {
103+
it('{Number} (Number.MAX_SAFE_INTEGER)', async () => {
102104
const config = {
103105
loader: {
104106
test: /\.png$/,
105107
options: {
106-
limit: '8192',
108+
limit: Number.MAX_SAFE_INTEGER,
109+
},
110+
},
111+
};
112+
113+
const stats = await webpack('fixture.js', config);
114+
const [{ source }] = stats.toJson().modules;
115+
116+
expect(source).toMatchSnapshot();
117+
});
118+
119+
it('{Number} (Number.MIN_SAFE_INTEGER)', async () => {
120+
const config = {
121+
loader: {
122+
test: /\.png$/,
123+
options: {
124+
limit: Number.MIN_SAFE_INTEGER,
107125
},
108126
},
109127
};

test/errors.test.js renamed to test/validate-options.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ it('validation', async () => {
1717
// The `fallback` loader can have any optsions so we use `additionalProperties: false` to avoid problems.
1818
expect(() => validate({ unknown: 'unknown' })).not.toThrow();
1919

20-
expect(() => validate({ limit: '8192' })).not.toThrow();
2120
expect(() => validate({ limit: 8192 })).not.toThrow();
21+
expect(() => validate({ limit: '8192' })).toThrowErrorMatchingSnapshot();
2222
expect(() => validate({ limit: true })).toThrowErrorMatchingSnapshot();
2323

2424
expect(() => validate({ mimetype: 'image/png' })).not.toThrow();

0 commit comments

Comments
 (0)