css-loader 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/index.js +3 -2
- package/dist/plugins/postcss-url-parser.js +64 -33
- package/dist/runtime/url-escape.js +2 -2
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
<a name="2.1.0"></a>
|
|
6
|
+
# [2.1.0](https://github.com/webpack-contrib/css-loader/compare/v2.0.2...v2.1.0) (2018-12-25)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* support `image-set` without `url` ([#879](https://github.com/webpack-contrib/css-loader/issues/879)) ([21884e2](https://github.com/webpack-contrib/css-loader/commit/21884e2))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
5
15
|
<a name="2.0.2"></a>
|
|
6
16
|
## [2.0.2](https://github.com/webpack-contrib/css-loader/compare/v2.0.1...v2.0.2) (2018-12-21)
|
|
7
17
|
|
package/dist/index.js
CHANGED
|
@@ -230,12 +230,13 @@ function loader(content, map, meta) {
|
|
|
230
230
|
} = message;
|
|
231
231
|
const {
|
|
232
232
|
url,
|
|
233
|
-
placeholder
|
|
233
|
+
placeholder,
|
|
234
|
+
needQuotes
|
|
234
235
|
} = item; // Remove `#hash` and `?#hash` from `require`
|
|
235
236
|
|
|
236
237
|
const [normalizedUrl, singleQuery, hashValue] = url.split(/(\?)?#/);
|
|
237
238
|
const hash = singleQuery || hashValue ? `"${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}"` : '';
|
|
238
|
-
imports.push(`var ${placeholder} = urlEscape(require(${(0, _loaderUtils.stringifyRequest)(this, (0, _loaderUtils.urlToRequest)(normalizedUrl))})${hash ? ` + ${hash}` : ''});`);
|
|
239
|
+
imports.push(`var ${placeholder} = urlEscape(require(${(0, _loaderUtils.stringifyRequest)(this, (0, _loaderUtils.urlToRequest)(normalizedUrl))})${hash ? ` + ${hash}` : ''}${needQuotes ? ', true' : ''});`);
|
|
239
240
|
cssAsString = cssAsString.replace(new RegExp(placeholder, 'g'), () => `" + ${placeholder} + "`);
|
|
240
241
|
});
|
|
241
242
|
let newMap = result.map;
|
|
@@ -9,43 +9,63 @@ var _postcss = _interopRequireDefault(require("postcss"));
|
|
|
9
9
|
|
|
10
10
|
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
|
11
11
|
|
|
12
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
13
|
+
|
|
12
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
15
|
|
|
14
16
|
const pluginName = 'postcss-url-parser';
|
|
17
|
+
const isUrlFunc = /url/i;
|
|
18
|
+
const isImageSetFunc = /^(?:-webkit-)?image-set$/i;
|
|
19
|
+
const needParseDecl = /(?:url|(?:-webkit-)?image-set)\(/i;
|
|
20
|
+
|
|
21
|
+
function getNodeFromUrlFunc(node) {
|
|
22
|
+
return node.nodes && node.nodes[0];
|
|
23
|
+
}
|
|
15
24
|
|
|
16
|
-
function
|
|
17
|
-
return nodes.length !== 0 && nodes[0].type === 'string' ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
|
|
25
|
+
function getUrlFromUrlFunc(node) {
|
|
26
|
+
return node.nodes.length !== 0 && node.nodes[0].type === 'string' ? node.nodes[0].value : _postcssValueParser.default.stringify(node.nodes);
|
|
18
27
|
}
|
|
19
28
|
|
|
20
29
|
function walkUrls(parsed, callback) {
|
|
21
30
|
parsed.walk(node => {
|
|
22
|
-
if (node.type !== 'function'
|
|
31
|
+
if (node.type !== 'function') {
|
|
23
32
|
return;
|
|
24
33
|
}
|
|
25
|
-
/* eslint-disable */
|
|
26
34
|
|
|
35
|
+
if (isUrlFunc.test(node.value)) {
|
|
36
|
+
callback(getNodeFromUrlFunc(node), getUrlFromUrlFunc(node), false); // Do not traverse inside `url`
|
|
37
|
+
// eslint-disable-next-line consistent-return
|
|
38
|
+
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
27
41
|
|
|
28
|
-
node.
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
if (isImageSetFunc.test(node.value)) {
|
|
43
|
+
node.nodes.forEach(nNode => {
|
|
44
|
+
if (nNode.type === 'function' && isUrlFunc.test(nNode.value)) {
|
|
45
|
+
callback(getNodeFromUrlFunc(nNode), getUrlFromUrlFunc(nNode), false);
|
|
46
|
+
}
|
|
31
47
|
|
|
32
|
-
|
|
33
|
-
|
|
48
|
+
if (nNode.type === 'string') {
|
|
49
|
+
callback(nNode, nNode.value, true);
|
|
50
|
+
}
|
|
51
|
+
}); // Do not traverse inside `image-set`
|
|
52
|
+
// eslint-disable-next-line consistent-return
|
|
34
53
|
|
|
35
|
-
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
36
56
|
});
|
|
37
57
|
}
|
|
38
58
|
|
|
39
59
|
function walkDeclsWithUrl(css, result, filter) {
|
|
40
60
|
const items = [];
|
|
41
61
|
css.walkDecls(decl => {
|
|
42
|
-
if (
|
|
62
|
+
if (!needParseDecl.test(decl.value)) {
|
|
43
63
|
return;
|
|
44
64
|
}
|
|
45
65
|
|
|
46
66
|
const parsed = (0, _postcssValueParser.default)(decl.value);
|
|
47
67
|
const urls = [];
|
|
48
|
-
walkUrls(parsed, (node, url) => {
|
|
68
|
+
walkUrls(parsed, (node, url, needQuotes) => {
|
|
49
69
|
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
|
|
50
70
|
result.warn(`Unable to find uri in '${decl.toString()}'`, {
|
|
51
71
|
node: decl
|
|
@@ -57,7 +77,10 @@ function walkDeclsWithUrl(css, result, filter) {
|
|
|
57
77
|
return;
|
|
58
78
|
}
|
|
59
79
|
|
|
60
|
-
urls.push(
|
|
80
|
+
urls.push({
|
|
81
|
+
url,
|
|
82
|
+
needQuotes
|
|
83
|
+
});
|
|
61
84
|
});
|
|
62
85
|
|
|
63
86
|
if (urls.length === 0) {
|
|
@@ -73,48 +96,56 @@ function walkDeclsWithUrl(css, result, filter) {
|
|
|
73
96
|
return items;
|
|
74
97
|
}
|
|
75
98
|
|
|
76
|
-
function flatten(array) {
|
|
77
|
-
return array.reduce((acc, d) => [...acc, ...d], []);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function uniq(array) {
|
|
81
|
-
return array.reduce((acc, d) => acc.indexOf(d) === -1 ? [...acc, d] : acc, []);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
99
|
var _default = _postcss.default.plugin(pluginName, (options = {}) => function process(css, result) {
|
|
85
100
|
const traversed = walkDeclsWithUrl(css, result, options.filter);
|
|
86
|
-
|
|
101
|
+
|
|
102
|
+
const paths = _lodash.default.uniqWith(_lodash.default.flatten(traversed.map(item => item.urls)), _lodash.default.isEqual);
|
|
87
103
|
|
|
88
104
|
if (paths.length === 0) {
|
|
89
105
|
return;
|
|
90
106
|
}
|
|
91
107
|
|
|
92
|
-
const
|
|
108
|
+
const placeholders = [];
|
|
93
109
|
paths.forEach((path, index) => {
|
|
94
110
|
const placeholder = `___CSS_LOADER_URL___${index}___`;
|
|
95
|
-
|
|
111
|
+
const {
|
|
112
|
+
url,
|
|
113
|
+
needQuotes
|
|
114
|
+
} = path;
|
|
115
|
+
placeholders.push({
|
|
116
|
+
placeholder,
|
|
117
|
+
path
|
|
118
|
+
});
|
|
96
119
|
result.messages.push({
|
|
97
120
|
pluginName,
|
|
98
121
|
type: 'url',
|
|
99
122
|
item: {
|
|
100
|
-
url
|
|
101
|
-
placeholder
|
|
123
|
+
url,
|
|
124
|
+
placeholder,
|
|
125
|
+
needQuotes
|
|
102
126
|
}
|
|
103
127
|
});
|
|
104
128
|
});
|
|
105
129
|
traversed.forEach(item => {
|
|
106
|
-
walkUrls(item.parsed, (node, url) => {
|
|
107
|
-
const value =
|
|
130
|
+
walkUrls(item.parsed, (node, url, needQuotes) => {
|
|
131
|
+
const value = _lodash.default.find(placeholders, {
|
|
132
|
+
path: {
|
|
133
|
+
url,
|
|
134
|
+
needQuotes
|
|
135
|
+
}
|
|
136
|
+
});
|
|
108
137
|
|
|
109
138
|
if (!value) {
|
|
110
139
|
return;
|
|
111
|
-
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const {
|
|
143
|
+
placeholder
|
|
144
|
+
} = value; // eslint-disable-next-line no-param-reassign
|
|
112
145
|
|
|
146
|
+
node.type = 'word'; // eslint-disable-next-line no-param-reassign
|
|
113
147
|
|
|
114
|
-
node.
|
|
115
|
-
type: 'word',
|
|
116
|
-
value
|
|
117
|
-
}];
|
|
148
|
+
node.value = placeholder;
|
|
118
149
|
}); // eslint-disable-next-line no-param-reassign
|
|
119
150
|
|
|
120
151
|
item.decl.value = item.parsed.toString();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
module.exports = function escape(url) {
|
|
3
|
+
module.exports = function escape(url, needQuotes) {
|
|
4
4
|
if (typeof url !== 'string') {
|
|
5
5
|
return url;
|
|
6
6
|
} // If url is already wrapped in quotes, remove them
|
|
@@ -12,7 +12,7 @@ module.exports = function escape(url) {
|
|
|
12
12
|
// See https://drafts.csswg.org/css-values-3/#urls
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
if (/["'() \t\n]/.test(url)) {
|
|
15
|
+
if (/["'() \t\n]/.test(url) || needQuotes) {
|
|
16
16
|
return '"' + url.replace(/"/g, '\\"').replace(/\n/g, '\\n') + '"';
|
|
17
17
|
}
|
|
18
18
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "css-loader",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "css loader module for webpack",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "webpack-contrib/css-loader",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"icss-utils": "^4.0.0",
|
|
44
|
-
"loader-utils": "^1.
|
|
44
|
+
"loader-utils": "^1.2.1",
|
|
45
45
|
"lodash": "^4.17.11",
|
|
46
46
|
"postcss": "^7.0.6",
|
|
47
47
|
"postcss-modules-extract-imports": "^2.0.0",
|