Skip to content

Commit 7cefed8

Browse files
docs(guides,config) asset module follow up (#3382)
1 parent 4b75a3e commit 7cefed8

File tree

2 files changed

+66
-63
lines changed

2 files changed

+66
-63
lines changed

src/content/configuration/module.mdx

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,19 @@ module.exports = {
249249
}
250250
```
251251

252-
If `Rule.type` is an `asset` then `Rules.parser` option may be an object or function that describes a condition whether to encode file contents to Base64 or emit it as a separate file into the output directory.
252+
If `Rule.type` is an `asset` then `Rules.parser` option may be an object or a function that describes a condition whether to encode file contents to Base64 or emit it as a separate file into the output directory.
253253

254-
If `Rule.type` is an `asset` or `asset/inline` then `Rule.generator` option may be an object that describes an encoding of the module source or a function that encodes a module source by a custom algorithm.
254+
If `Rule.type` is an `asset` or `asset/inline` then `Rule.generator` option may be an object that describes the encoding of the module source or a function that encodes module's source by a custom algorithm.
255255

256-
For more info - see [Asset Modules](/guides/asset-modules/)
256+
See [Asset Modules guide](/guides/asset-modules/) for additional information and use cases.
257257

258-
## `Rule.parse.dataUrlCondition` as an object
258+
## `Rule.parser.dataUrlCondition`
259+
260+
`object = { maxSize number = 8096 }` `function (source, { filename, module }) => boolean`
261+
262+
If a module source size is less than `maxSize` then module will be injected into the bundle as a Base64-encoded string, otherwise module file will be emitted into the output directory.
263+
264+
__webpack.config.js__
259265

260266
```js
261267
module.exports = {
@@ -266,7 +272,7 @@ module.exports = {
266272
//...
267273
parser: {
268274
dataUrlCondition: {
269-
maxSize: 8096
275+
maxSize: 4 * 1024
270276
}
271277
}
272278
}
@@ -275,19 +281,39 @@ module.exports = {
275281
}
276282
```
277283

278-
## `dataUrlCondition.minSize`
284+
When a function is given, returning `true` tells webpack to inject the module into the bundle as Base64-encoded string, otherwise module file will be emitted into the output directory.
279285

280-
`number`
286+
__webpack.config.js__
281287

282-
If a module source size is less than `maxSize` then module will be injected into the bundle as a Base64-encoded string, otherwise module file will be emitted into the output directory.
288+
```js
289+
module.exports = {
290+
//...
291+
module: {
292+
rules: [
293+
{
294+
//...
295+
parser: {
296+
dataUrlCondition: (source, { filename, module })) => {
297+
const content = source.toString();
298+
return content.includes('some marker');
299+
}
300+
}
301+
}
302+
]
303+
}
304+
}
305+
```
283306

284-
## `Rule.parse.dataUrlCondition` as a function
307+
## `Rule.generator.dataUrl`
285308

286-
`function(source, { filename, module }) => boolean`
309+
`object = { encoding string = 'base64' | false, mimetype string = undefined | false }` `function (content, { filename, module }) => string`
287310

288-
If the function returns true, then module will be injected into the bundle as base64-encoded string, otherwise module file will be emitted into the output directory.
311+
When `Rule.generator.dataUrl` is used as an object, you can configure two properties:
289312

290-
## `Rule.generator.dataUrl` as an object
313+
- encoding: When set to `'base64'`, module source will be encoded using Base64 algorithm. Setting `encoding` to false will disable encoding.
314+
- mimetype: A mimetype for data URI. Resolves from module resource extension by default.
315+
316+
__webpack.config.js__
291317

292318
```js
293319
module.exports = {
@@ -298,55 +324,36 @@ module.exports = {
298324
//...
299325
generator: {
300326
encoding: 'base64',
301-
mimetype: ''
327+
mimetype: 'mimetype/png'
302328
}
303329
}
304330
]
305331
}
306332
}
307333
```
308334

309-
## `dataUrl.encoding`
310-
311-
`'base' | false`
312-
313-
- `base64` - encode a module source with Base64 algorithm
314-
- `false` - don't encode a module source
315-
316-
## `dataUrl.mimetype`
317-
318-
`string`
319-
320-
A mimetype for data-url. Resolves from module resource extension by default
321-
322-
## `Rule.generator.dataUrl` as a function
335+
When used a a function, it executes for every module and must return a data URI string.
323336

324-
`function(source, { filename, module }) => string`
325-
326-
A function that executes for a module and should return a data-url string
327-
328-
__webpack.config.js__
329-
330-
``` js
337+
```js
331338
module.exports = {
332-
experiments: {
333-
asset: true
334-
},
339+
//...
335340
module: {
336341
rules: [
337342
{
338-
test: /\.txt/,
339-
type: 'asset',
340-
parser: {
341-
dataUrlCondition(source, { filename, module }) {
342-
content = content.toString();
343-
return content.includes('some marker');
343+
//...
344+
generator: {
345+
dataUrl: content => {
346+
const svgToMiniDataURI = require('mini-svg-data-uri');
347+
if (typeof content !== 'string') {
348+
content = content.toString();
349+
}
350+
return svgToMiniDataURI(content);
344351
}
345352
}
346353
}
347354
]
348355
}
349-
};
356+
}
350357
```
351358

352359
## `Rule.resource`
@@ -444,7 +451,7 @@ module.exports = {
444451
};
445452
```
446453

447-
> See [Asset Modules](/guides/asset-modules/) for more about `asset*` type
454+
> See [Asset Modules guide](/guides/asset-modules/) for more about `asset*` type.
448455
449456
## `Rule.use`
450457

src/content/guides/asset-modules.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
---
22
title: Asset Modules
3+
sort: 24
34
contributors:
45
- smelukov
6+
- EugeneHlushko
57
---
68

7-
Asset Modules is a type of modules that allows to use assets files (fonts, icons, etc) without configuring additional loaders.
9+
Asset Modules is a type of module that allows to use asset files (fonts, icons, etc) without configuring additional loaders.
810

911
Prior to webpack 5 it was common to use:
1012

@@ -19,7 +21,7 @@ Asset Modules type replaces all of these loaders by adding 4 new module types:
1921
- `asset/source` exports the source code of the asset. Previously achievable by using `raw-loader`.
2022
- `asset` automatically chooses between exporting a data URI and emitting a separate file. Previously achievable by using `url-loader` with asset size limit.
2123

22-
W> This is an experimental feature. Enable Asset Modules by setting `experiments.asset: true` in [experiments](/configuration/experiments/) option of your webpack configuration
24+
W> This is an experimental feature. Enable Asset Modules by setting `experiments.asset: true` in [experiments](/configuration/experiments/) option of your webpack configuration.
2325

2426
__webpack.config.js__
2527

@@ -67,11 +69,9 @@ module.exports = {
6769

6870
__src/index.js__
6971

70-
``` js
72+
```js
7173
import mainImage from './images/main.png';
7274

73-
// ...
74-
7575
img.src = mainImage; // '/dist/151cfcfa1bd74779aadb.png'
7676
```
7777

@@ -81,11 +81,11 @@ All `.png` files will be emitted to the output directory and their paths will be
8181

8282
By default, `asset/resource` modules are emitting with `[hash][ext]` filename into output directory.
8383

84-
You can modify this template by setting `output.assetModuleFilename` in your configuration:
84+
You can modify this template by setting [`output.assetModuleFilename`](/configuration/output/#outputassetmodulefilename) in your webpack configuration:
8585

8686
__webpack.config.js__
8787

88-
``` diff
88+
```diff
8989
const path = require('path');
9090

9191
module.exports = {
@@ -141,12 +141,10 @@ module.exports = {
141141

142142
__src/index.js__
143143

144-
``` diff
144+
```diff
145145
- import mainImage from './images/main.png';
146146
+ import metroMap from './images/matro.svg';
147147

148-
// ...
149-
150148
- img.src = mainImage; // '/dist/151cfcfa1bd74779aadb.png'
151149
+ block.style.background = `url(${metroMap}); // url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=)
152150
```
@@ -161,7 +159,7 @@ If you want to use a custom encoding algorithm, you may specify a custom functio
161159

162160
__webpack.config.js__
163161

164-
``` diff
162+
```diff
165163
const path = require('path');
166164
+ const svgToMiniDataURI = require('mini-svg-data-uri');
167165

@@ -197,7 +195,7 @@ Now all `.svg` files will be encoded by `mini-svg-data-uri` package.
197195

198196
__webpack.config.js__
199197

200-
``` diff
198+
```diff
201199
const path = require('path');
202200
- const svgToMiniDataURI = require('mini-svg-data-uri');
203201

@@ -231,18 +229,16 @@ module.exports = {
231229

232230
__src/example.txt__
233231

234-
``` text
232+
```text
235233
Hello world
236234
```
237235

238236
__src/index.js__
239237

240-
``` diff
238+
```diff
241239
- import metroMap from './images/matro.svg';
242240
+ import exampleText from './example.txt';
243241

244-
// ...
245-
246242
- block.style.background = `url(${metroMap}); // url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=)
247243
+ block.textContent = exampleText; // 'Hello world'
248244
```
@@ -278,7 +274,7 @@ module.exports = {
278274

279275
Now webpack will automatically choose between `resource` and `inline` by following a default condition: a file with size less than 8kb will be treated as a `inline` module type and `resource` module type otherwise.
280276

281-
You can change this condition by setting a `parser.dataUrlCondition.maxSize` option on the module rule of your webpack configuration:
277+
You can change this condition by setting a [`Rule.parser.dataUrlCondition.maxSize`](/configuration/module/#ruleparserdataurlcondition) option on the module rule level of your webpack configuration:
282278

283279
__webpack.config.js__
284280

@@ -310,4 +306,4 @@ module.exports = {
310306
};
311307
```
312308

313-
Also you can [specify a function](/configuration/module/#ruleparsedataurlcondition-as-a-function) to decide to inlining a module or not.
309+
Also you can [specify a function](/configuration/module/#ruleparserdataurlcondition) to decide to inlining a module or not.

0 commit comments

Comments
 (0)