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

Commit 488b6ec

Browse files
refactor: code (#212)
1 parent 36f1354 commit 488b6ec

25 files changed

+1504
-1112
lines changed

.prettierrc.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
module.exports = {
2-
singleQuote: true,
3-
trailingComma: 'es5',
4-
arrowParens: 'always',
5-
};
1+
module.exports = { singleQuote: true };

README.md

+133-46
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,25 @@ And run `webpack` via your preferred method.
5959

6060
## Options
6161

62-
### `fallback`
62+
| Name | Type | Default | Description |
63+
| :---------------------------: | :-------------------------: | :-----------------------------------------------------------: | :---------------------------------------------------------------------------------- |
64+
| **[`limit`](#limit)** | `{Boolean\|Number\|String}` | `true` | Specifying the maximum size of a file in bytes. |
65+
| **[`mimetype`](#mimetype)** | `{Boolean\|String}` | based from [mime-types](https://github.com/jshttp/mime-types) | Sets the MIME type for the file to be transformed. |
66+
| **[`encoding`](#encoding)** | `{Boolean\|String}` | `base64` | Specify the encoding which the file will be inlined with. |
67+
| **[`generator`](#generator)** | `{Function}` | `() => type/subtype;encoding,base64_data` | You can create you own custom implementation for encoding data. |
68+
| **[`fallback`](#fallback)** | `{String}` | `file-loader` | Specifies an alternative loader to use when a target file's size exceeds the limit. |
69+
| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax. |
6370

64-
Type: `String`
65-
Default: `'file-loader'`
71+
### `limit`
6672

67-
Specifies an alternative loader to use when a target file's size exceeds the
68-
limit set in the `limit` option.
73+
Type: `Boolean|Number|String`
74+
Default: `undefined`
75+
76+
The limit can be specified via loader options and defaults to no limit.
77+
78+
#### `Boolean`
79+
80+
Enable or disable transform files into base64.
6981

7082
**webpack.config.js**
7183

@@ -79,7 +91,7 @@ module.exports = {
7991
{
8092
loader: 'url-loader',
8193
options: {
82-
fallback: require.resolve('responsive-loader'),
94+
limit: false,
8395
},
8496
},
8597
],
@@ -89,9 +101,12 @@ module.exports = {
89101
};
90102
```
91103

92-
The fallback loader will receive the same configuration options as url-loader.
104+
#### `Number|String`
93105

94-
For example, to set the quality option of a responsive-loader above use:
106+
A `Number` or `String` specifying the maximum size of a file in bytes.
107+
If the file size is **equal** or **greater** than the limit [`file-loader`](https://github.com/webpack-contrib/file-loader) will be used (by default) and all query parameters are passed to it.
108+
109+
Using an alternative to `file-loader` is enabled via the `fallback` option.
95110

96111
**webpack.config.js**
97112

@@ -105,8 +120,7 @@ module.exports = {
105120
{
106121
loader: 'url-loader',
107122
options: {
108-
fallback: require.resolve('responsive-loader'),
109-
quality: 85,
123+
limit: 8192,
110124
},
111125
},
112126
],
@@ -116,19 +130,18 @@ module.exports = {
116130
};
117131
```
118132

119-
### `limit`
133+
### `mimetype`
120134

121-
Type: `Number|Boolean|String`
122-
Default: `undefined`
135+
Type: `Boolean|String`
136+
Default: based from [mime-types](https://github.com/jshttp/mime-types)
123137

124-
The limit can be specified via loader options and defaults to no limit.
138+
Specify the `mimetype` which the file will be inlined with.
139+
If unspecified the `mimetype` value will be used from [mime-types](https://github.com/jshttp/mime-types).
125140

126-
#### `Number|String`
141+
#### `Boolean`
127142

128-
A `Number` or `String` specifying the maximum size of a file in bytes. If the file size is
129-
**equal** or **greater** than the limit [`file-loader`](https://github.com/webpack-contrib/file-loader)
130-
will be used (by default) and all query parameters are passed to it.
131-
Using an alternative to `file-loader` is enabled via the `fallback` option.
143+
The `true` value allows to generation the `mimetype` part from [mime-types](https://github.com/jshttp/mime-types).
144+
The `false` value removes the `mediatype` part from a Data URL (if omitted, defaults to `text/plain;charset=US-ASCII`).
132145

133146
**webpack.config.js**
134147

@@ -142,7 +155,7 @@ module.exports = {
142155
{
143156
loader: 'url-loader',
144157
options: {
145-
limit: 8192,
158+
mimetype: false,
146159
},
147160
},
148161
],
@@ -152,9 +165,9 @@ module.exports = {
152165
};
153166
```
154167

155-
#### `Boolean`
168+
#### `String`
156169

157-
Enable or disable transform files into base64.
170+
Sets the MIME type for the file to be transformed.
158171

159172
**webpack.config.js**
160173

@@ -168,7 +181,7 @@ module.exports = {
168181
{
169182
loader: 'url-loader',
170183
options: {
171-
limit: false,
184+
mimetype: 'image/png',
172185
},
173186
},
174187
],
@@ -178,12 +191,17 @@ module.exports = {
178191
};
179192
```
180193

181-
### `mimetype`
194+
### `encoding`
182195

183-
Type: `String`
184-
Default: `(file extension)`
196+
Type: `Boolean|String`
197+
Default: `base64`
198+
199+
Specify the `encoding` which the file will be inlined with.
200+
If unspecified the `encoding` will be `base64`.
201+
202+
#### `Boolean`
185203

186-
Sets the MIME type for the file to be transformed. If unspecified the file extensions will be used to lookup the MIME type.
204+
If you don't want to use any encoding you can set `encoding` to `false` however if you set it to `true` it will use the default encoding `base64`.
187205

188206
**webpack.config.js**
189207

@@ -192,12 +210,12 @@ module.exports = {
192210
module: {
193211
rules: [
194212
{
195-
test: /\.(png|jpg|gif)$/i,
213+
test: /\.svg$/i,
196214
use: [
197215
{
198216
loader: 'url-loader',
199217
options: {
200-
mimetype: 'image/png',
218+
encoding: false,
201219
},
202220
},
203221
],
@@ -207,14 +225,9 @@ module.exports = {
207225
};
208226
```
209227

210-
### `encoding`
211-
212-
Type: `String|Boolean`
213-
Default: `base64`
214-
215-
Specify the encoding which the file will be in-lined with. It supports [Node.js Buffers and Character Encodings](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) which are `["utf8","utf16le","latin1","base64","hex","ascii","binary","ucs2"]`.
228+
#### `String`
216229

217-
> If you don't want to use any encoding you can set `encoding` to `false` however if you set it to `true` it will use the default encoding `base64`.
230+
It supports [Node.js Buffers and Character Encodings](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) which are `["utf8","utf16le","latin1","base64","hex","ascii","binary","ucs2"]`.
218231

219232
**webpack.config.js**
220233

@@ -241,8 +254,9 @@ module.exports = {
241254
### `generator`
242255

243256
Type: `Function`
257+
Default: `(mimetype, encoding, content, resourcePath) => mimetype;encoding,base64_content`
244258

245-
You can create you own custom implementation for encoding data. `generator` argument is a [`Buffer`](https://nodejs.org/api/buffer.html) instance of the file. in the example we are compressing svg files using [mini-svg-data-uri](https://github.com/tigt/mini-svg-data-uri) implementation.
259+
You can create you own custom implementation for encoding data.
246260

247261
**webpack.config.js**
248262

@@ -251,15 +265,21 @@ module.exports = {
251265
module: {
252266
rules: [
253267
{
254-
test: /\.svg$/i,
268+
test: /\.(png|html)$/i,
255269
use: [
256270
{
257271
loader: 'url-loader',
258272
options: {
259-
generator: (svgContentBuffer) => {
260-
const svgToMiniDataURI = require('mini-svg-data-uri');
261-
262-
return svgToMiniDataURI(svgContentBuffer.toString());
273+
// The `mimetype` and `encoding` arguments will be obtained from your options
274+
// The `resourcePath` argument is path to file.
275+
generator: (content, mimetype, encoding, resourcePath) => {
276+
if (/\.html$/i.test(resourcePath)) {
277+
return `data:${mimetype},${content.toString()}`;
278+
}
279+
280+
return `data:${mimetype}${
281+
encoding ? `;${encoding}` : ''
282+
},${content.toString(encoding)}`;
263283
},
264284
},
265285
},
@@ -270,7 +290,12 @@ module.exports = {
270290
};
271291
```
272292

273-
By using your own implementation, `mimetype` and `encoding` won't have effect on the final output. until you specify them in the output manually for Example:
293+
### `fallback`
294+
295+
Type: `String`
296+
Default: `'file-loader'`
297+
298+
Specifies an alternative loader to use when a target file's size exceeds the limit set in the `limit` option.
274299

275300
**webpack.config.js**
276301

@@ -279,13 +304,39 @@ module.exports = {
279304
module: {
280305
rules: [
281306
{
282-
test: /\.svg$/i,
307+
test: /\.(png|jpg|gif)$/i,
283308
use: [
284309
{
285310
loader: 'url-loader',
286311
options: {
287-
generator: (svgContentBuffer) =>
288-
`data:image/svg;utf8,${svgContentBuffer.toString('utf8')}`,
312+
fallback: require.resolve('responsive-loader'),
313+
},
314+
},
315+
],
316+
},
317+
],
318+
},
319+
};
320+
```
321+
322+
The fallback loader will receive the same configuration options as url-loader.
323+
324+
For example, to set the quality option of a responsive-loader above use:
325+
326+
**webpack.config.js**
327+
328+
```js
329+
module.exports = {
330+
module: {
331+
rules: [
332+
{
333+
test: /\.(png|jpg|gif)$/i,
334+
use: [
335+
{
336+
loader: 'url-loader',
337+
options: {
338+
fallback: require.resolve('responsive-loader'),
339+
quality: 85,
289340
},
290341
},
291342
],
@@ -327,6 +378,38 @@ module.exports = {
327378
};
328379
```
329380

381+
## Examples
382+
383+
### SVG
384+
385+
SVG can be compressed into a more compact output, avoiding `base64`.
386+
You can read about it more [here](https://css-tricks.com/probably-dont-base64-svg/).
387+
You can do it using [mini-svg-data-uri](https://github.com/tigt/mini-svg-data-uri) package.
388+
389+
**webpack.config.js**
390+
391+
```js
392+
const svgToMiniDataURI = require('mini-svg-data-uri');
393+
394+
module.exports = {
395+
module: {
396+
rules: [
397+
{
398+
test: /\.svg$/i,
399+
use: [
400+
{
401+
loader: 'url-loader',
402+
options: {
403+
generator: (content) => svgToMiniDataURI(content.toString()),
404+
},
405+
},
406+
],
407+
},
408+
],
409+
},
410+
};
411+
```
412+
330413
## Contributing
331414

332415
Please take a moment to read our contributing guidelines if you haven't yet done so.
@@ -351,3 +434,7 @@ Please take a moment to read our contributing guidelines if you haven't yet done
351434
[chat-url]: https://gitter.im/webpack/webpack
352435
[size]: https://packagephobia.now.sh/badge?p=url-loader
353436
[size-url]: https://packagephobia.now.sh/result?p=url-loader
437+
438+
```
439+
440+
```

lint-staged.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
'*.js': ['prettier --write', 'eslint --fix', 'git add'],
3-
'*.{json,md,yml,css,ts}': ['prettier --write', 'git add'],
2+
'*.js': ['prettier --write', 'eslint --fix'],
3+
'*.{json,md,yml,css,ts}': ['prettier --write'],
44
};

0 commit comments

Comments
 (0)