evanw / evanw/esbuild

plugins::onLoad dataurl should support mimetype and base64 encoder

Open
#4,237 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

TLDR
1. need ability to specify **mimetype** when using `loader:dataurl` in result of the plugin `onLoad`
2. need ability to specify if dataurl will be encoded with **base64**

```js
build.onLoad({ filter: /.*/, namespace: "dataurl" }, async args => {
const buffer = await fs.readFile(args.path.replace(/\?dataurl$/, ""));
return {
contents: bufferBuffer,
loader: "dataurl"
}
};
```

the build output will be either
```
data:application/octet-stream,...
```
or this if the buffer does not have "binary" data
```
data:text/plain;charset=utf-8,...
```

now if I want to explicitly indicate the mimetype maybe for a `image/gif` or `image/jpeg`
I can not indicate that for the loader.

if `contents` also accepted `Blob`, or blob like object we can preserve the mime type
```js
let contents = new Blob([dataURLBuffer], { type: "image/gif" });
```

also need a way to indicate whether I want to use base64 encoding

```js
build.onLoad({ filter: /.*/, namespace: "dataurl" }, async args => {
const buffer = await fs.readFile(args.path.replace(/\?dataurl$/, ""));
let bufferBlob= new Blob([buffer], {type:"image/gif"})
return {
contents: bufferBlob,
base64: true, // not sure how to best communicate this
loader: "dataurl"
}
};
```

one downside with Blob, might be that it may copy the buffer
so maybe some struct support instead.

```js
build.onLoad({ filter: /.*/, namespace: "dataurl" }, async args => {
const buffer = await fs.readFile(args.path.replace(/\?dataurl$/, ""));
const struct = { buffer, mime:"image/gif", base64:true}
return {
contents: struct ,
loader: "dataurl"
}
};
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Begin with the plugin onLoad result and loader:dataurl behavior described in the issue. Determine how the API can expose an explicit MIME type and base64 choice, then verify that generated data URLs for binary and text contents reflect those settings.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.