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

feat: add encoding & generator options #210

Merged
merged 15 commits into from Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions README.md
Expand Up @@ -207,6 +207,92 @@ module.exports = {
};
```

### `encoding`

Type: `String`
Default: `base64`

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"]`.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/i,
use: [
{
loader: 'url-loader',
options: {
mimetype: 'utf8',
},
},
],
},
],
},
};
```

### `source`
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved

Type: `Function`
Default: `data:${mimetype || ''};${encoding},${src.toString(encoding)}`
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved

You can create you own custom implementation for encoding data. in the example we are compressing svg files using [mini-svg-data-uri](https://github.com/tigt/mini-svg-data-uri) implementation.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/i,
use: [
{
loader: 'url-loader',
options: {
source: (svgContent) => {
const svgToMiniDataURI = require('mini-svg-data-uri');

return svgToMiniDataURI(svgContent);
},
},
},
],
},
],
},
};
```

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:

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/i,
use: [
{
loader: 'url-loader',
options: {
source: (svgContent) => {
return `data:image/svg;utf8,${svgContent}`;
},
},
},
],
},
],
},
};
```

### `esModule`

Type: `Boolean`
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"loader-utils": "^2.0.0",
"mime-types": "^2.1.26",
"mini-svg-data-uri": "^1.1.3",
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved
"schema-utils": "^2.6.5"
},
"devDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Expand Up @@ -36,6 +36,7 @@ export default function loader(src) {
if (shouldTransform(options.limit, src.length)) {
const file = this.resourcePath;
const mimetype = options.mimetype || mime.contentType(path.extname(file));
const encoding = options.encoding || 'base64';

if (typeof src === 'string') {
// eslint-disable-next-line no-param-reassign
Expand All @@ -45,11 +46,13 @@ export default function loader(src) {
const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : true;

const encodedData = options.source
? options.source(src)
: `data:${mimetype || ''};${encoding},${src.toString(encoding)}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look here https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

encoding is optinal, so

data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E is valid, but in our case we always insert ;

Rewrite it to "data:${mimetype || ''}${encoding? ;${encoding} : ''},${src.toString(encoding)}`;" + test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added boolean type for encoding, now we can disable it, if it's enabled it will use the default base64


return `${
esModule ? 'export default' : 'module.exports ='
} ${JSON.stringify(
`data:${mimetype || ''};base64,${src.toString('base64')}`
)}`;
} ${JSON.stringify(encodedData)}`;
}

// Normalize the fallback.
Expand Down
18 changes: 18 additions & 0 deletions src/options.json
Expand Up @@ -5,6 +5,24 @@
"description": "Enables/Disables transformation target file into base64 URIs (https://github.com/webpack-contrib/url-loader#limit).",
"type": ["boolean", "number", "string"]
},
"encoding": {
"description": "Specify the encoding which the file will be in-lined with.",
"type": "string",
"enum": [
"utf8",
"utf16le",
"latin1",
"base64",
"hex",
"ascii",
"binary",
"ucs2"
]
},
"source": {
"description": "Adding custom implementation for encoding files.",
"instanceof": "Function"
},
"mimetype": {
"description": "The MIME type for the file to be transformed (https://github.com/webpack-contrib/url-loader#mimetype).",
"type": "string"
Expand Down
305 changes: 305 additions & 0 deletions test/__snapshots__/encoding-option.test.js.snap

Large diffs are not rendered by default.