1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _path = _interopRequireDefault(require("path"));
|
---|
9 |
|
---|
10 | var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
---|
11 |
|
---|
12 | var _normalizeUrl = _interopRequireDefault(require("normalize-url"));
|
---|
13 |
|
---|
14 | var _isAbsoluteUrl = _interopRequireDefault(require("is-absolute-url"));
|
---|
15 |
|
---|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
17 |
|
---|
18 | const multiline = /\\[\r\n]/; // eslint-disable-next-line no-useless-escape
|
---|
19 |
|
---|
20 | const escapeChars = /([\s\(\)"'])/g;
|
---|
21 |
|
---|
22 | function convert(url, options) {
|
---|
23 | if ((0, _isAbsoluteUrl.default)(url) || url.startsWith('//')) {
|
---|
24 | let normalizedURL = null;
|
---|
25 |
|
---|
26 | try {
|
---|
27 | normalizedURL = (0, _normalizeUrl.default)(url, options);
|
---|
28 | } catch (e) {
|
---|
29 | normalizedURL = url;
|
---|
30 | }
|
---|
31 |
|
---|
32 | return normalizedURL;
|
---|
33 | } // `path.normalize` always returns backslashes on Windows, need replace in `/`
|
---|
34 |
|
---|
35 |
|
---|
36 | return _path.default.normalize(url).replace(new RegExp('\\' + _path.default.sep, 'g'), '/');
|
---|
37 | }
|
---|
38 |
|
---|
39 | function transformNamespace(rule) {
|
---|
40 | rule.params = (0, _postcssValueParser.default)(rule.params).walk(node => {
|
---|
41 | if (node.type === 'function' && node.value.toLowerCase() === 'url' && node.nodes.length) {
|
---|
42 | node.type = 'string';
|
---|
43 | node.quote = node.nodes[0].quote || '"';
|
---|
44 | node.value = node.nodes[0].value;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (node.type === 'string') {
|
---|
48 | node.value = node.value.trim();
|
---|
49 | }
|
---|
50 |
|
---|
51 | return false;
|
---|
52 | }).toString();
|
---|
53 | }
|
---|
54 |
|
---|
55 | function transformDecl(decl, opts) {
|
---|
56 | decl.value = (0, _postcssValueParser.default)(decl.value).walk(node => {
|
---|
57 | if (node.type !== 'function' || node.value.toLowerCase() !== 'url') {
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | node.before = node.after = '';
|
---|
62 |
|
---|
63 | if (!node.nodes.length) {
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 |
|
---|
67 | let url = node.nodes[0];
|
---|
68 | let escaped;
|
---|
69 | url.value = url.value.trim().replace(multiline, ''); // Skip empty URLs
|
---|
70 | // Empty URL function equals request to current stylesheet where it is declared
|
---|
71 |
|
---|
72 | if (url.value.length === 0) {
|
---|
73 | url.quote = '';
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (/^data:(.*)?,/i.test(url.value)) {
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (!/^.+-extension:\//i.test(url.value)) {
|
---|
82 | url.value = convert(url.value, opts);
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (escapeChars.test(url.value) && url.type === 'string') {
|
---|
86 | escaped = url.value.replace(escapeChars, '\\$1');
|
---|
87 |
|
---|
88 | if (escaped.length < url.value.length + 2) {
|
---|
89 | url.value = escaped;
|
---|
90 | url.type = 'word';
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | url.type = 'word';
|
---|
94 | }
|
---|
95 |
|
---|
96 | return false;
|
---|
97 | }).toString();
|
---|
98 | }
|
---|
99 |
|
---|
100 | function pluginCreator(opts) {
|
---|
101 | opts = Object.assign({}, {
|
---|
102 | normalizeProtocol: false,
|
---|
103 | sortQueryParameters: false,
|
---|
104 | stripHash: false,
|
---|
105 | stripWWW: false,
|
---|
106 | stripTextFragment: false
|
---|
107 | }, opts);
|
---|
108 | return {
|
---|
109 | postcssPlugin: 'postcss-normalize-url',
|
---|
110 |
|
---|
111 | OnceExit(css) {
|
---|
112 | css.walk(node => {
|
---|
113 | if (node.type === 'decl') {
|
---|
114 | return transformDecl(node, opts);
|
---|
115 | } else if (node.type === 'atrule' && node.name.toLowerCase() === 'namespace') {
|
---|
116 | return transformNamespace(node);
|
---|
117 | }
|
---|
118 | });
|
---|
119 | }
|
---|
120 |
|
---|
121 | };
|
---|
122 | }
|
---|
123 |
|
---|
124 | pluginCreator.postcss = true;
|
---|
125 | var _default = pluginCreator;
|
---|
126 | exports.default = _default;
|
---|
127 | module.exports = exports.default; |
---|