-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (104 loc) · 3.91 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-disable max-len */
const postcss = require("postcss"),
urlReg = /url\(['"]?([^'"]+)?['"]?\)/,
valSplitReg = /(?<=\)),/g,
base64Limit = 1024 * 3,
{ existsSync, statSync, readFileSync } = require("fs"),
{ dirname, resolve, extname: pathExtname } = require("path");
const extname = src => pathExtname(src).slice(1);
const toBase64 = imgSrc =>
`data:image/${extname(imgSrc)};base64,${Buffer.from(
readFileSync(imgSrc)
).toString("base64")}`;
const searchReg = /\?.+$/,
webpReg = /\.webp(\?.+)?$/,
endReg = /(\?.+)?$/;
const PNPI = "PNPI"; // PNPI: Postcss No Parse Image
const toSrc = (srcs) => srcs.join(',');
/* eslint-disable consistent-return */
const parse = (result, newOpts, url) => {
const { strict, base64Limit: $base64Limit, webpClassName } = newOpts;
if ($base64Limit > 0) {
const imgSrc = resolve(dirname(result.opts.from), url).replace(searchReg, '');
if (existsSync(imgSrc)) {
const { size } = statSync(imgSrc);
if (size < $base64Limit) {
return ['B', toBase64(imgSrc)];
}
} else if (strict) {
return new TypeError(`NOT FOUND Image: ${imgSrc}`);
}
}
if (webpClassName !== '' && !webpReg.test(url)) {
return ['W', url.replace(endReg, '.webp$1')];
}
};
/* eslint-enable consistent-return */
const noParseReg = /no-postcss-img=0/,
// relativeReg = /^(\.{1,2}\/|(?!(?:https?:\/\/|data:image))\w)/,
absoluteReg = /^(?:https?:\/\/|data:image)/; // https:// or base64
const do_parse = process.env.NO_WEBP === '1' ? () => () => {} : (opts = {}) => (root, result) => {
const newOpts = Object.assign(
{
ignoreExts : ['svg', 'webp'],
webpClassName: "webp",
base64Limit,
strict: true,
ignore: (url) => url ? noParseReg.test(url) || newOpts.ignoreExts.includes(extname(url)) || absoluteReg.test(url) : true
},
opts
);
root.walkDecls(/^background(-image)?$/, decl => {
const { value } = decl;
if ((decl.parent.selector || PNPI).includes(PNPI)) {
return;
}
const ress = [],
parseVal = value.split(valSplitReg).map(v => {
const [, url ] = urlReg.exec(v) || [];
if (newOpts.ignore(url)) {
return v;
}
const res = parse(result, newOpts, url);
if (undefined === res) {
return v;
}
if (res instanceof Error) {
decl.error(res);
return res;
}
res.push(url);
ress.push(res);
return v.replace(url, res[1]);
});
if (ress.length === 0) {
return;
}
if (ress.some(x => x[0] === 'B')) {
ress.forEach((v) => {
if (v[0] === 'B') {
decl.value = decl.value.replace(v[2], v[1]);
}
});
if (ress.length === 1) {
return;
}
}
const { parent } = decl,
newDecl = decl.clone({
value: toSrc(parseVal.map(v => (urlReg.exec(v) || [v])[0])),
prop: 'background-image'
}),
newRule = parent.cloneAfter({
selector: parent.selector.split(',').map(v => `.${newOpts.webpClassName} ${v}`).join(','),
nodes: [
]
});
newDecl.parent = newRule;
newRule.nodes.push(newDecl);
});
};
module.exports = postcss.plugin(
"postcss-img",
do_parse
);