-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
docs(guides,config) asset module follow up #3382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
67a4d3e
docs(guides,config) asset module follow up
EugeneHlushko 64e8106
Merge branch 'master' into asset-modules/followup
EugeneHlushko 3fd93f9
docs(config) fix typos
EugeneHlushko 7b66940
Update src/content/guides/asset-modules.md
EugeneHlushko 4313cb6
Merge branch 'master' into asset-modules/followup
EugeneHlushko b1d98a7
Update src/content/configuration/module.mdx
EugeneHlushko 53ef056
Update src/content/configuration/module.mdx
EugeneHlushko 5dedf9f
Update src/content/configuration/module.mdx
EugeneHlushko 32d1b80
Update src/content/configuration/module.mdx
EugeneHlushko 6613b5a
Update src/content/configuration/module.mdx
EugeneHlushko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -249,13 +249,19 @@ module.exports = { | |||||
} | ||||||
``` | ||||||
|
||||||
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. | ||||||
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. | ||||||
|
||||||
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. | ||||||
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. | ||||||
|
||||||
For more info - see [Asset Modules](/guides/asset-modules/) | ||||||
See [Asset Modules gude](/guides/asset-modules/) for additional information and use cases. | ||||||
|
||||||
## `Rule.parse.dataUrlCondition` as an object | ||||||
## `Rule.parser.dataUrlCondition` | ||||||
|
||||||
`object = { maxSize number = 8096 }` `function (source, { filename, module }) => boolean` | ||||||
|
||||||
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. | ||||||
|
||||||
__webpack.config.js__ | ||||||
|
||||||
```js | ||||||
module.exports = { | ||||||
|
@@ -266,7 +272,7 @@ module.exports = { | |||||
//... | ||||||
parser: { | ||||||
dataUrlCondition: { | ||||||
maxSize: 8096 | ||||||
maxSize: 4 * 1024 | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -275,19 +281,39 @@ module.exports = { | |||||
} | ||||||
``` | ||||||
|
||||||
## `dataUrlCondition.minSize` | ||||||
When a function given, returning `true` tells webpack to inject the module into the bundle as Base64-encoded string, otherwise module file will be emitted info output directory. | ||||||
EugeneHlushko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
||||||
`number` | ||||||
__webpack.config.js__ | ||||||
|
||||||
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. | ||||||
```js | ||||||
module.exports = { | ||||||
//... | ||||||
module: { | ||||||
rules: [ | ||||||
{ | ||||||
//... | ||||||
parser: { | ||||||
dataUrlCondition: (source, { filename, module })) => { | ||||||
const content = source.toString(); | ||||||
return content.includes('some marker'); | ||||||
} | ||||||
} | ||||||
} | ||||||
] | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
## `Rule.parse.dataUrlCondition` as a function | ||||||
## `Rule.generator.dataUrl` | ||||||
|
||||||
`function(source, { filename, module }) => boolean` | ||||||
`object = { encoding string = 'base64' | false, mimetype string = undefined | false }` `function (content, { filename, module }) => string` | ||||||
|
||||||
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. | ||||||
When `Rule.generator.dataUrl` is used as an object, you can configure two properties: | ||||||
|
||||||
## `Rule.generator.dataUrl` as an object | ||||||
- encoding: When set to `'base64'`, module source will be encoded with Base64 algorithm. Setting `encoding` to false will disable encoding. | ||||||
EugeneHlushko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
- mimetype: A mimetype for data URI. Resolves from module resource extension by default. | ||||||
|
||||||
__webpack.config.js__ | ||||||
|
||||||
```js | ||||||
module.exports = { | ||||||
|
@@ -298,55 +324,36 @@ module.exports = { | |||||
//... | ||||||
generator: { | ||||||
encoding: 'base64', | ||||||
mimetype: '' | ||||||
mimetype: 'mimetype/png' | ||||||
} | ||||||
} | ||||||
] | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
## `dataUrl.encoding` | ||||||
|
||||||
`'base' | false` | ||||||
|
||||||
- `base64` - encode a module source with Base64 algorithm | ||||||
- `false` - don't encode a module source | ||||||
|
||||||
## `dataUrl.mimetype` | ||||||
|
||||||
`string` | ||||||
|
||||||
A mimetype for data-url. Resolves from module resource extension by default | ||||||
|
||||||
## `Rule.generator.dataUrl` as a function | ||||||
When used a a function, it executes for every module and must return a data URI string. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
`function(source, { filename, module }) => string` | ||||||
|
||||||
A function that executes for a module and should return a data-url string | ||||||
|
||||||
__webpack.config.js__ | ||||||
|
||||||
``` js | ||||||
```js | ||||||
module.exports = { | ||||||
experiments: { | ||||||
asset: true | ||||||
}, | ||||||
//... | ||||||
module: { | ||||||
rules: [ | ||||||
{ | ||||||
test: /\.txt/, | ||||||
type: 'asset', | ||||||
parser: { | ||||||
dataUrlCondition(source, { filename, module }) { | ||||||
content = content.toString(); | ||||||
return content.includes('some marker'); | ||||||
//... | ||||||
generator: { | ||||||
dataUrl: content => { | ||||||
const svgToMiniDataURI = require('mini-svg-data-uri'); | ||||||
if (typeof content !== 'string') { | ||||||
content = content.toString(); | ||||||
} | ||||||
return svgToMiniDataURI(content); | ||||||
} | ||||||
} | ||||||
} | ||||||
] | ||||||
} | ||||||
}; | ||||||
} | ||||||
``` | ||||||
|
||||||
## `Rule.resource` | ||||||
|
@@ -444,7 +451,7 @@ module.exports = { | |||||
}; | ||||||
``` | ||||||
|
||||||
> See [Asset Modules](/guides/asset-modules/) for more about `asset*` type | ||||||
> See [Asset Modules guide](/guides/asset-modules/) for more about `asset*` type. | ||||||
|
||||||
## `Rule.use` | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.