/
postcss-url-parser.js
158 lines (124 loc) · 4.34 KB
/
postcss-url-parser.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';
import { normalizeUrl } from '../utils';
const pluginName = 'postcss-url-parser';
const isUrlFunc = /url/i;
const isImageSetFunc = /^(?:-webkit-)?image-set$/i;
const needParseDecl = /(?:url|(?:-webkit-)?image-set)\(/i;
function getNodeFromUrlFunc(node) {
return node.nodes && node.nodes[0];
}
function walkUrls(parsed, callback) {
parsed.walk((node) => {
if (node.type !== 'function') {
return;
}
if (isUrlFunc.test(node.value)) {
const { nodes } = node;
const isStringValue = nodes.length !== 0 && nodes[0].type === 'string';
const url = isStringValue ? nodes[0].value : valueParser.stringify(nodes);
callback(getNodeFromUrlFunc(node), url, false, isStringValue);
// Do not traverse inside `url`
// eslint-disable-next-line consistent-return
return false;
}
if (isImageSetFunc.test(node.value)) {
for (const nNode of node.nodes) {
const { type, value } = nNode;
if (type === 'function' && isUrlFunc.test(value)) {
const { nodes } = nNode;
const isStringValue =
nodes.length !== 0 && nodes[0].type === 'string';
const url = isStringValue
? nodes[0].value
: valueParser.stringify(nodes);
callback(getNodeFromUrlFunc(nNode), url, false, isStringValue);
}
if (type === 'string') {
callback(nNode, value, true, true);
}
}
// Do not traverse inside `image-set`
// eslint-disable-next-line consistent-return
return false;
}
});
}
export default postcss.plugin(pluginName, (options) => (css, result) => {
const importsMap = new Map();
const replacementsMap = new Map();
let hasHelper = false;
css.walkDecls((decl) => {
if (!needParseDecl.test(decl.value)) {
return;
}
const parsed = valueParser(decl.value);
walkUrls(parsed, (node, url, needQuotes, isStringValue) => {
// https://www.w3.org/TR/css-syntax-3/#typedef-url-token
if (url.replace(/^[\s]+|[\s]+$/g, '').length === 0) {
result.warn(
`Unable to find uri in '${decl ? decl.toString() : decl.value}'`,
{ node: decl }
);
return;
}
if (options.filter && !options.filter(url)) {
return;
}
const splittedUrl = url.split(/(\?)?#/);
const [urlWithoutHash, singleQuery, hashValue] = splittedUrl;
const hash =
singleQuery || hashValue
? `${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}`
: '';
const normalizedUrl = normalizeUrl(urlWithoutHash, isStringValue);
const importKey = normalizedUrl;
let importName = importsMap.get(importKey);
if (!importName) {
importName = `___CSS_LOADER_URL_IMPORT_${importsMap.size}___`;
importsMap.set(importKey, importName);
if (!hasHelper) {
const urlToHelper = require.resolve('../runtime/getUrl.js');
result.messages.push({
pluginName,
type: 'import',
value: {
importName: '___CSS_LOADER_GET_URL_IMPORT___',
url: options.urlHandler
? options.urlHandler(urlToHelper)
: urlToHelper,
},
});
hasHelper = true;
}
result.messages.push({
pluginName,
type: 'import',
value: {
importName,
url: options.urlHandler
? options.urlHandler(normalizedUrl)
: normalizedUrl,
},
});
}
const replacementKey = JSON.stringify({ importKey, hash, needQuotes });
let replacementName = replacementsMap.get(replacementKey);
if (!replacementName) {
replacementName = `___CSS_LOADER_URL_REPLACEMENT_${replacementsMap.size}___`;
replacementsMap.set(replacementKey, replacementName);
result.messages.push({
pluginName,
type: 'url-replacement',
value: { replacementName, importName, hash, needQuotes },
});
}
// eslint-disable-next-line no-param-reassign
node.type = 'word';
// eslint-disable-next-line no-param-reassign
node.value = replacementName;
});
// eslint-disable-next-line no-param-reassign
decl.value = parsed.toString();
});
});