/
inline.js
91 lines (76 loc) · 2.75 KB
/
inline.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
const processCopy = require('./copy');
const processRebase = require('./rebase');
const encodeFile = require('../lib/encode');
const getFile = require('../lib/get-file');
/**
* @param {String} originUrl
* @param {PostcssUrl~Dir} dir
* @param {PostcssUrl~Option} options
*
* @returns {String|Undefined}
*/
function processFallback(originUrl, dir, options) {
if (typeof options.fallback === 'function') {
return options.fallback.apply(null, arguments);
}
switch (options.fallback) {
case 'copy':
return processCopy.apply(null, arguments);
case 'rebase':
return processRebase.apply(null, arguments);
default:
return Promise.resolve();
}
}
const inlineProcess = (file, asset, warn, addDependency, options) => {
const isSvg = file.mimeType === 'image/svg+xml';
const defaultEncodeType = isSvg ? 'encodeURIComponent' : 'base64';
const encodeType = options.encodeType || defaultEncodeType;
// Warn for svg with hashes/fragments
if (isSvg && asset.hash && !options.ignoreFragmentWarning) {
// eslint-disable-next-line max-len
warn(`Image type is svg and link contains #. Postcss-url cant handle svg fragments. SVG file fully inlined. ${file.path}`);
}
addDependency(file.path);
const optimizeSvgEncode = isSvg && options.optimizeSvgEncode;
const encodedStr = encodeFile(file, encodeType, optimizeSvgEncode);
const resultValue = options.includeUriFragment && asset.hash
? encodedStr + asset.hash
: encodedStr;
// wrap url by quotes if percent-encoded svg
return isSvg && encodeType !== 'base64' ? `"${resultValue}"` : resultValue;
};
/**
* Inline image in url()
*
* @type {PostcssUrl~UrlProcessor}
* @param {PostcssUrl~Asset} asset
* @param {PostcssUrl~Dir} dir
* @param {PostcssUrl~Options} options
* @param {PostcssUrl~Decl} decl
* @param {Function} warn
* @param {Result} result
* @param {Function} addDependency
*
* @returns {Promise<String|Undefined>}
*/
// eslint-disable-next-line complexity
module.exports = function(asset, dir, options, decl, warn, result, addDependency) {
return getFile(asset, options, dir, warn)
.then((file) => {
if (!file) return;
if (!file.mimeType) {
warn(`Unable to find asset mime-type for ${file.path}`);
return;
}
const maxSize = (options.maxSize || 0) * 1024;
if (maxSize) {
const size = Buffer.byteLength(file.contents);
if (size >= maxSize) {
return processFallback.apply(this, arguments);
}
}
return inlineProcess(file, asset, warn, addDependency, options);
});
};