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

feat: add encoding option (options.encoding) #79

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ module.exports = {
|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**`limit`**|`{Number}`|`undefined`|Byte limit to inline files as Data URL|
|**`prefix`**|`{String}`|`false`|Parameters for the [`file-loader`](https://github.com/webpack-contrib/file-loader) are valid too They are passed to the file-loader if used|
|**`mimetype`**|`{String}`|`extname`|Specify MIME type for the file (Otherwise it's inferred from the file extension)|
|**`prefix`**|`{String}`|`false`|Parameters for the [`file-loader`](https://github.com/webpack-contrib/file-loader) are valid too. They are passed to the file-loader if used|
|**`encoding`**|`{Boolean|String}`|`base64`|Specify the encoding which the file will be inlined with|

### `limit`

Expand All @@ -74,6 +75,17 @@ The limit can be specified via loader options and defaults to no limit.
}
```

### `prefix`

```js
{
loader: 'url-loader',
options: {
prefix: 'img'
}
}
```

### `mimetype`

Set the MIME type for the file. If unspecified the file extensions will be used to lookup the MIME type.
Expand All @@ -88,13 +100,15 @@ Set the MIME type for the file. If unspecified the file extensions will be used
}
```

### `prefix`
### `encoding`

Specify the encoding of the file.

```js
{
loader: 'url-loader',
options: {
prefix: 'img'
encoding: 'utf-8'
}
}
```
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ module.exports = function(content) {

var mimetype = options.mimetype || options.minetype || mime.lookup(this.resourcePath);

var encoding = options.encoding || 'base64';

// No limits or limit more than content length
if(!limit || content.length < limit) {
if(typeof content === "string") {
content = new Buffer(content);
}

return "module.exports = " + JSON.stringify("data:" + (mimetype ? mimetype + ";" : "") + "base64," + content.toString("base64"));
return "module.exports = " + JSON.stringify("data:" + (mimetype ? mimetype + ";" : "") + options.encoding + "," + content.toString(options.encoding));
Copy link
Member Author

Choose a reason for hiding this comment

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

"data:<mimetype>;<encoding>,<content...>"

return "module.exports = " + JSON.stringify("data:" 
 + (mimetype ? mimetype + ";" : "") 
 + (options.encoding  ? options.encoding : (?*) )  + "," 
 + content.toString(options.encoding ? options.encoding : (?*) ));

?* => Discuss/Triage

Copy link
Member

Choose a reason for hiding this comment

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

@michael-ciniawsky

return "module.exports = " + JSON.stringify("data:" + (mimetype ? mimetype + ";" : "") + encoding + "," + content.toString(encoding)

}

var fileLoader = require("file-loader");
Expand Down