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
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -207,6 +207,35 @@ module.exports = {
};
```

### `encoding`

Type: `String`
Default: `base64`

Specify the encoding which the file will be in-lined with. It supports (Node.js Buffer Encodings)[https://www.w3resource.com/node.js/nodejs-buffer.php] If the encoding is not supported the default `base64` encoding will be used.

**webpack.config.js**

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

### `esModule`

Type: `Boolean`
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Expand Up @@ -45,10 +45,18 @@ export default function loader(src) {
const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : true;

let encodedData;
try {
encodedData = src.toString(options.encoding || 'base64');
} catch (e) {
options.encoding = 'base64';
encodedData = src.toString(options.encoding);
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved
}

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

Expand Down
4 changes: 4 additions & 0 deletions src/options.json
Expand Up @@ -5,6 +5,10 @@
"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"
},
"mimetype": {
"description": "The MIME type for the file to be transformed (https://github.com/webpack-contrib/url-loader#mimetype).",
"type": "string"
Expand Down