-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
39 lines (34 loc) · 1.06 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
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { basename, dirname } from 'path';
import { createFilter } from 'rollup-pluginutils';
const defaultExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
export default function image(options = {}) {
const extensions = options.extensions || defaultExtensions;
const includes = extensions.map(e => `**/*${e}`);
const filter = createFilter(options.include || includes, options.exclude);
let images = [];
function generateBundle(outputOptions, rendered) {
const dir =
outputOptions.dir || dirname(outputOptions.dest || outputOptions.file);
if (!existsSync(dir)) {
mkdirSync(dir);
}
images.forEach(id => {
writeFileSync(`${dir}/${basename(id)}`, readFileSync(id));
});
}
return {
name: 'image-file',
load(id) {
if ('string' !== typeof id || !filter(id)) {
return null;
}
if (images.indexOf(id) < 0) {
images.push(id);
}
return `const img = require('./${basename(id)}'); export default img;`;
},
generateBundle,
ongenerate: generateBundle
};
}