-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (53 loc) · 1.33 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
const filesystem = require('fs');
const lodash = require('lodash');
const mimeTypes = require('mime-types');
class DataUriCompiler {
constructor(config) {
this.config = lodash.defaultsDeep(
lodash.cloneDeep(config && config.plugins && config.plugins.datauri || {}),
{
pattern: /\.(gif|jpg|png|svg)/
}
);
if (this.config.pattern) {
this.pattern = this.config.pattern;
}
}
getDataUri(path) {
return new Promise(
(resolve, reject) => {
filesystem.readFile(
path,
(error, data) => {
if (error) {
reject(error);
}
resolve('data:' + mimeTypes.lookup(path) + ';base64,' + data.toString('base64'));
}
);
}
);
}
compile(file) {
return new Promise(
(resolve, reject) => {
const result = this.getDataUri(file.path);
result.catch(
(error) => {
reject(error);
}
);
result.then(
(data) => {
resolve('module.exports = ' + JSON.stringify(data) + ';');
}
);
}
);
}
}
DataUriCompiler.prototype.brunchPlugin = true;
DataUriCompiler.prototype.pattern = /\.(gif|jpg|png|svg)/;
DataUriCompiler.prototype.type = 'template';
module.exports = DataUriCompiler;