-
-
Notifications
You must be signed in to change notification settings - Fork 590
/
index.js
executable file
·49 lines (40 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { readFileSync } from 'fs';
import { extname } from 'path';
import { createFilter } from '@rollup/pluginutils';
const defaults = {
dom: false,
exclude: null,
include: null
};
const mimeTypes = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.webp': 'image/webp'
};
export default function image(opts = {}) {
const options = Object.assign({}, defaults, opts);
const filter = createFilter(options.include, options.exclude);
return {
name: 'image',
load(id) {
if (!filter(id)) {
return null;
}
const mime = mimeTypes[extname(id)];
if (!mime) {
// not an image
return null;
}
const format = mime === mimeTypes['.svg'] ? 'utf-8' : 'base64';
const source = readFileSync(id, format).replace(/[\r\n]+/gm, '');
const data = `'data:${mime};${format},${source}'`;
const code = options.dom
? `var img = new Image(); img.src = ${data}; export default img;`
: `const img = ${data}; export default img;`;
return code;
}
};
}