1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Compiler = require('../visitor/compiler')
|
13 | , events = require('../renderer').events
|
14 | , nodes = require('../nodes')
|
15 | , parse = require('url').parse
|
16 | , extname = require('path').extname
|
17 | , utils = require('../utils')
|
18 | , fs = require('fs');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var defaultMimes = {
|
25 | '.gif': 'image/gif'
|
26 | , '.png': 'image/png'
|
27 | , '.jpg': 'image/jpeg'
|
28 | , '.jpeg': 'image/jpeg'
|
29 | , '.svg': 'image/svg+xml'
|
30 | , '.webp': 'image/webp'
|
31 | , '.ttf': 'application/x-font-ttf'
|
32 | , '.eot': 'application/vnd.ms-fontobject'
|
33 | , '.woff': 'application/font-woff'
|
34 | , '.woff2': 'application/font-woff2'
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | var encodingTypes = {
|
41 | BASE_64: 'base64',
|
42 | UTF8: 'charset=utf-8'
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | module.exports = function(options) {
|
66 | options = options || {};
|
67 |
|
68 | var _paths = options.paths || [];
|
69 | var sizeLimit = null != options.limit ? options.limit : 30000;
|
70 | var mimes = options.mimes || defaultMimes;
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 | function fn(url, enc) {
|
78 |
|
79 | var compiler = new Compiler(url)
|
80 | , encoding = encodingTypes.BASE_64;
|
81 |
|
82 | compiler.isURL = true;
|
83 | url = url.nodes.map(function(node) {
|
84 | return compiler.visit(node);
|
85 | }).join('');
|
86 |
|
87 |
|
88 | url = parse(url);
|
89 | var ext = extname(url.pathname)
|
90 | , mime = mimes[ext]
|
91 | , hash = url.hash || ''
|
92 | , literal = new nodes.Literal('url("' + url.href + '")')
|
93 | , paths = _paths.concat(this.paths)
|
94 | , buf
|
95 | , result;
|
96 |
|
97 |
|
98 | if(!mime) return literal;
|
99 |
|
100 |
|
101 | if(url.protocol) return literal;
|
102 |
|
103 |
|
104 | var found = utils.lookup(url.pathname, paths);
|
105 |
|
106 |
|
107 | if(!found) {
|
108 | events.emit(
|
109 | 'file not found'
|
110 | , 'File ' + literal + ' could not be found, literal url retained!'
|
111 | );
|
112 |
|
113 | return literal;
|
114 | }
|
115 |
|
116 |
|
117 | var str = fs.readFileSync(found, 'utf8');
|
118 |
|
119 |
|
120 | if(false !== sizeLimit && str.length > sizeLimit) return literal;
|
121 |
|
122 | if(enc && 'utf8' == enc.first.val.toLowerCase()) {
|
123 | encoding = encodingTypes.UTF8;
|
124 | result = str.replace(/\s+/g, ' ')
|
125 | .replace(/[{}\|\\\^~\[\]`"<>#%]/g, function(match) {
|
126 | return '%' + match[0].charCodeAt(0).toString(16).toUpperCase();
|
127 | }).trim();
|
128 | } else {
|
129 | result = Buffer.from(str.replace(/\r\n?/g, '\n')).toString(encoding) + hash;
|
130 | }
|
131 |
|
132 |
|
133 | return new nodes.Literal('url("data:' + mime + ';' + encoding + ',' + result + '")');
|
134 | };
|
135 |
|
136 | fn.raw = true;
|
137 | return fn;
|
138 | };
|
139 |
|
140 |
|
141 | module.exports.mimes = defaultMimes;
|
142 |
|