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

Commit 60d2cb3

Browse files
feat: limit option can be boolean (#181)
1 parent d82e453 commit 60d2cb3

7 files changed

+89
-34
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,18 @@ For example, to set the quality option of a responsive-loader above use:
9191

9292
### `limit`
9393

94-
Type: `Number`
94+
Type: `Number|Boolean`
9595
Default: `undefined`
9696

97+
The limit can be specified via loader options and defaults to no limit.
98+
99+
#### `Number`
100+
97101
A `Number` specifying the maximum size of a file in bytes. If the file size is
98102
**equal** or **greater** than the limit [`file-loader`](https://github.com/webpack-contrib/file-loader)
99103
will be used (by default) and all query parameters are passed to it.
100104
Using an alternative to `file-loader` is enabled via the `fallback` option.
101105

102-
The limit can be specified via loader options and defaults to no limit.
103-
104106
```js
105107
// webpack.config.js
106108
{
@@ -111,6 +113,21 @@ The limit can be specified via loader options and defaults to no limit.
111113
}
112114
```
113115

116+
#### `Boolean`
117+
118+
Enable or disable transform files into base64.
119+
120+
```js
121+
// webpack.config.js
122+
{
123+
loader: 'url-loader',
124+
options: {
125+
// Disable transformation to base64
126+
limit: false
127+
}
128+
}
129+
```
130+
114131
### `mimetype`
115132

116133
Type: `String`

src/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@ import mime from 'mime';
1111
import normalizeFallback from './utils/normalizeFallback';
1212
import schema from './options.json';
1313

14+
function shouldTransform(limit, size) {
15+
if (typeof limit === 'boolean') {
16+
return limit;
17+
}
18+
19+
if (typeof limit === 'number') {
20+
return size <= parseInt(limit, 10);
21+
}
22+
23+
return true;
24+
}
25+
1426
export default function loader(src) {
1527
// Loader Options
1628
const options = getOptions(this) || {};
1729

1830
validateOptions(schema, options, 'URL Loader');
1931

20-
// Set limit for resource inlining (file size)
21-
const limit = options.limit ? parseInt(options.limit, 10) : options.limit;
22-
2332
// No limit or within the specified limit
24-
if ((!limit && typeof limit !== 'number') || src.length <= limit) {
33+
if (shouldTransform(options.limit, src.length)) {
2534
const file = this.resourcePath;
2635
// Get MIME type
2736
const mimetype = options.mimetype || mime.getType(file);

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

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

Lines changed: 12 additions & 8 deletions
Large diffs are not rendered by default.

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

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

1010
exports[`validation 2`] = `
1111
"URL Loader Invalid Options
1212
13-
options.limit should be number
14-
"
15-
`;
16-
17-
exports[`validation 3`] = `
18-
"URL Loader Invalid Options
19-
2013
options.mimetype should be string
2114
"
2215
`;
2316

24-
exports[`validation 4`] = `
17+
exports[`validation 3`] = `
2518
"URL Loader Invalid Options
2619
2720
options.fallback should be string

test/limit-option.test.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import webpack from './helpers/compiler';
22

33
describe('limit option', () => {
4-
it('{undefined}', async () => {
4+
it('not specify', async () => {
55
const config = {
66
loader: {
77
test: /\.png$/,
@@ -15,7 +15,7 @@ describe('limit option', () => {
1515
expect(source).toMatchSnapshot();
1616
});
1717

18-
it('{Number} (0)', async () => {
18+
it('0 ({Number})', async () => {
1919
// Image size is 6777
2020
const config = {
2121
loader: {
@@ -32,7 +32,7 @@ describe('limit option', () => {
3232
expect(source).toMatchSnapshot();
3333
});
3434

35-
it('{Number} (0.1)', async () => {
35+
it('0.1 ({Number})', async () => {
3636
// Image size is 6777
3737
const config = {
3838
loader: {
@@ -49,7 +49,7 @@ describe('limit option', () => {
4949
expect(source).toMatchSnapshot();
5050
});
5151

52-
it('{Number} (6776)', async () => {
52+
it('6776 ({Number})', async () => {
5353
// Image size is 6777
5454
const config = {
5555
loader: {
@@ -66,7 +66,7 @@ describe('limit option', () => {
6666
expect(source).toMatchSnapshot();
6767
});
6868

69-
it('{Number} (6777)', async () => {
69+
it('6777 ({Number})', async () => {
7070
// Image size is 6777
7171
const config = {
7272
loader: {
@@ -83,7 +83,7 @@ describe('limit option', () => {
8383
expect(source).toMatchSnapshot();
8484
});
8585

86-
it('{Number} (6778)', async () => {
86+
it('6778 ({Number})', async () => {
8787
// Image size is 6777
8888
const config = {
8989
loader: {
@@ -100,7 +100,7 @@ describe('limit option', () => {
100100
expect(source).toMatchSnapshot();
101101
});
102102

103-
it('{Number} (Number.MAX_SAFE_INTEGER)', async () => {
103+
it('Number.MAX_SAFE_INTEGER ({Number})', async () => {
104104
const config = {
105105
loader: {
106106
test: /\.png$/,
@@ -116,7 +116,7 @@ describe('limit option', () => {
116116
expect(source).toMatchSnapshot();
117117
});
118118

119-
it('{Number} (Number.MIN_SAFE_INTEGER)', async () => {
119+
it('Number.MIN_SAFE_INTEGER ({Number})', async () => {
120120
const config = {
121121
loader: {
122122
test: /\.png$/,
@@ -131,4 +131,36 @@ describe('limit option', () => {
131131

132132
expect(source).toMatchSnapshot();
133133
});
134+
135+
it('true ({Boolean})', async () => {
136+
const config = {
137+
loader: {
138+
test: /\.png$/,
139+
options: {
140+
limit: true,
141+
},
142+
},
143+
};
144+
145+
const stats = await webpack('fixture.js', config);
146+
const [{ source }] = stats.toJson().modules;
147+
148+
expect(source).toMatchSnapshot();
149+
});
150+
151+
it('false ({Boolean})', async () => {
152+
const config = {
153+
loader: {
154+
test: /\.png$/,
155+
options: {
156+
limit: false,
157+
},
158+
},
159+
};
160+
161+
const stats = await webpack('fixture.js', config);
162+
const [{ source }] = stats.toJson().modules;
163+
164+
expect(source).toMatchSnapshot();
165+
});
134166
});

test/validate-options.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ it('validation', async () => {
1818
expect(() => validate({ unknown: 'unknown' })).not.toThrow();
1919

2020
expect(() => validate({ limit: 8192 })).not.toThrow();
21+
expect(() => validate({ limit: true })).not.toThrow();
2122
expect(() => validate({ limit: '8192' })).toThrowErrorMatchingSnapshot();
22-
expect(() => validate({ limit: true })).toThrowErrorMatchingSnapshot();
2323

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

0 commit comments

Comments
 (0)