index.js
3.45 KB
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _postcss = require('postcss');
var _postcss2 = _interopRequireDefault(_postcss);
var _postcssValueParser = require('postcss-value-parser');
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
var _normalizeUrl = require('normalize-url');
var _normalizeUrl2 = _interopRequireDefault(_normalizeUrl);
var _isAbsoluteUrl = require('is-absolute-url');
var _isAbsoluteUrl2 = _interopRequireDefault(_isAbsoluteUrl);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const multiline = /\\[\r\n]/;
const escapeChars = /([\s\(\)"'])/g;
function convert(url, options) {
if ((0, _isAbsoluteUrl2.default)(url) || url.startsWith('//')) {
let normalizedURL = null;
try {
normalizedURL = (0, _normalizeUrl2.default)(url, options);
} catch (e) {
normalizedURL = url;
}
return normalizedURL;
}
// `path.normalize` always returns backslashes on Windows, need replace in `/`
return _path2.default.normalize(url).replace(new RegExp('\\' + _path2.default.sep, 'g'), '/');
}
function transformNamespace(rule) {
rule.params = (0, _postcssValueParser2.default)(rule.params).walk(node => {
if (node.type === 'function' && node.value.toLowerCase() === 'url' && node.nodes.length) {
node.type = 'string';
node.quote = node.nodes[0].quote || '"';
node.value = node.nodes[0].value;
}
if (node.type === 'string') {
node.value = node.value.trim();
}
return false;
}).toString();
}
function transformDecl(decl, opts) {
decl.value = (0, _postcssValueParser2.default)(decl.value).walk(node => {
if (node.type !== 'function' || node.value.toLowerCase() !== 'url' || !node.nodes.length) {
return false;
}
let url = node.nodes[0];
let escaped;
node.before = node.after = '';
url.value = url.value.trim().replace(multiline, '');
// Skip empty URLs
// Empty URL function equals request to current stylesheet where it is declared
if (url.value.length === 0) {
url.quote = '';
return false;
}
if (/^data:(.*)?,/i.test(url.value)) {
return false;
}
if (!/^.+-extension:\//i.test(url.value)) {
url.value = convert(url.value, opts);
}
if (escapeChars.test(url.value) && url.type === 'string') {
escaped = url.value.replace(escapeChars, '\\$1');
if (escaped.length < url.value.length + 2) {
url.value = escaped;
url.type = 'word';
}
} else {
url.type = 'word';
}
return false;
}).toString();
}
exports.default = _postcss2.default.plugin('postcss-normalize-url', opts => {
opts = Object.assign({}, {
normalizeProtocol: false,
stripFragment: false,
stripWWW: false
}, opts);
return css => {
css.walk(node => {
if (node.type === 'decl') {
return transformDecl(node, opts);
} else if (node.type === 'atrule' && node.name.toLowerCase() === 'namespace') {
return transformNamespace(node);
}
});
};
});
module.exports = exports['default'];