保誠-保戶業務員媒合平台
tomasysh
2022-05-25 43d0eed31f4b2a59e23c06ceba3616aac3f549f6
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
'use strict';
 
const fs = require('fs');
 
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;
    }
}
 
/**
 * 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 {String|Undefined}
 */
// eslint-disable-next-line complexity
module.exports = function(asset, dir, options, decl, warn, result, addDependency) {
    const file = getFile(asset, options, dir, warn);
 
    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 stats = fs.statSync(file.path);
 
        if (stats.size >= maxSize) {
            return processFallback.apply(this, arguments);
        }
    }
 
    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;
};