css-loader 4.2.2 → 5.0.2

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.
@@ -5,30 +5,25 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _util = require("util");
9
-
10
- var _postcss = _interopRequireDefault(require("postcss"));
11
-
12
8
  var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
13
9
 
14
10
  var _utils = require("../utils");
15
11
 
16
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
13
 
18
- const pluginName = 'postcss-url-parser';
19
14
  const isUrlFunc = /url/i;
20
15
  const isImageSetFunc = /^(?:-webkit-)?image-set$/i;
21
- const needParseDecl = /(?:url|(?:-webkit-)?image-set)\(/i;
16
+ const needParseDeclaration = /(?:url|(?:-webkit-)?image-set)\(/i;
22
17
 
23
18
  function getNodeFromUrlFunc(node) {
24
19
  return node.nodes && node.nodes[0];
25
20
  }
26
21
 
27
- function shouldHandleRule(rule, decl, result) {
22
+ function shouldHandleRule(rule, node, result) {
28
23
  // https://www.w3.org/TR/css-syntax-3/#typedef-url-token
29
- if (rule.url.replace(/^[\s]+|[\s]+$/g, '').length === 0) {
30
- result.warn(`Unable to find uri in '${decl.toString()}'`, {
31
- node: decl
24
+ if (rule.url.replace(/^[\s]+|[\s]+$/g, "").length === 0) {
25
+ result.warn(`Unable to find uri in '${node.toString()}'`, {
26
+ node
32
27
  });
33
28
  return false;
34
29
  }
@@ -40,218 +35,227 @@ function shouldHandleRule(rule, decl, result) {
40
35
  return true;
41
36
  }
42
37
 
43
- function walkCss(css, result, options, callback) {
44
- const accumulator = [];
45
- css.walkDecls(decl => {
46
- if (!needParseDecl.test(decl.value)) {
38
+ function visitor(result, parsedResults, node, key) {
39
+ if (!needParseDeclaration.test(node[key])) {
40
+ return;
41
+ }
42
+
43
+ const parsed = (0, _postcssValueParser.default)(node[key]);
44
+ parsed.walk(valueNode => {
45
+ if (valueNode.type !== "function") {
47
46
  return;
48
47
  }
49
48
 
50
- const parsed = (0, _postcssValueParser.default)(decl.value);
51
- parsed.walk(node => {
52
- if (node.type !== 'function') {
53
- return;
54
- }
49
+ if (isUrlFunc.test(valueNode.value)) {
50
+ const {
51
+ nodes
52
+ } = valueNode;
53
+ const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
54
+ const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
55
+ const rule = {
56
+ node: getNodeFromUrlFunc(valueNode),
57
+ url,
58
+ needQuotes: false,
59
+ isStringValue
60
+ };
61
+
62
+ if (shouldHandleRule(rule, node, result)) {
63
+ parsedResults.push({
64
+ node,
65
+ rule,
66
+ parsed
67
+ });
68
+ } // Do not traverse inside `url`
69
+ // eslint-disable-next-line consistent-return
70
+
55
71
 
56
- if (isUrlFunc.test(node.value)) {
72
+ return false;
73
+ } else if (isImageSetFunc.test(valueNode.value)) {
74
+ for (const nNode of valueNode.nodes) {
57
75
  const {
58
- nodes
59
- } = node;
60
- const isStringValue = nodes.length !== 0 && nodes[0].type === 'string';
61
- const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
62
- const rule = {
63
- node: getNodeFromUrlFunc(node),
64
- url,
65
- needQuotes: false,
66
- isStringValue
67
- };
68
-
69
- if (shouldHandleRule(rule, decl, result)) {
70
- accumulator.push({
71
- decl,
72
- rule,
73
- parsed
74
- });
75
- } // Do not traverse inside `url`
76
- // eslint-disable-next-line consistent-return
77
-
78
-
79
- return false;
80
- } else if (isImageSetFunc.test(node.value)) {
81
- for (const nNode of node.nodes) {
76
+ type,
77
+ value
78
+ } = nNode;
79
+
80
+ if (type === "function" && isUrlFunc.test(value)) {
82
81
  const {
83
- type,
84
- value
82
+ nodes
85
83
  } = nNode;
86
-
87
- if (type === 'function' && isUrlFunc.test(value)) {
88
- const {
89
- nodes
90
- } = nNode;
91
- const isStringValue = nodes.length !== 0 && nodes[0].type === 'string';
92
- const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
93
- const rule = {
94
- node: getNodeFromUrlFunc(nNode),
95
- url,
96
- needQuotes: false,
97
- isStringValue
98
- };
99
-
100
- if (shouldHandleRule(rule, decl, result)) {
101
- accumulator.push({
102
- decl,
103
- rule,
104
- parsed
105
- });
106
- }
107
- } else if (type === 'string') {
108
- const rule = {
109
- node: nNode,
110
- url: value,
111
- needQuotes: true,
112
- isStringValue: true
113
- };
114
-
115
- if (shouldHandleRule(rule, decl, result)) {
116
- accumulator.push({
117
- decl,
118
- rule,
119
- parsed
120
- });
121
- }
84
+ const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
85
+ const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
86
+ const rule = {
87
+ node: getNodeFromUrlFunc(nNode),
88
+ url,
89
+ needQuotes: false,
90
+ isStringValue
91
+ };
92
+
93
+ if (shouldHandleRule(rule, node, result)) {
94
+ parsedResults.push({
95
+ node,
96
+ rule,
97
+ parsed
98
+ });
99
+ }
100
+ } else if (type === "string") {
101
+ const rule = {
102
+ node: nNode,
103
+ url: value,
104
+ needQuotes: true,
105
+ isStringValue: true
106
+ };
107
+
108
+ if (shouldHandleRule(rule, node, result)) {
109
+ parsedResults.push({
110
+ node,
111
+ rule,
112
+ parsed
113
+ });
122
114
  }
123
- } // Do not traverse inside `image-set`
124
- // eslint-disable-next-line consistent-return
115
+ }
116
+ } // Do not traverse inside `image-set`
117
+ // eslint-disable-next-line consistent-return
125
118
 
126
119
 
127
- return false;
128
- }
129
- });
120
+ return false;
121
+ }
130
122
  });
131
- callback(null, accumulator);
132
123
  }
133
124
 
134
- const asyncWalkCss = (0, _util.promisify)(walkCss);
125
+ const plugin = (options = {}) => {
126
+ return {
127
+ postcssPlugin: "postcss-url-parser",
135
128
 
136
- var _default = _postcss.default.plugin(pluginName, options => async (css, result) => {
137
- const parsedResults = await asyncWalkCss(css, result, options);
129
+ prepare(result) {
130
+ const parsedResults = [];
131
+ return {
132
+ Declaration(declaration) {
133
+ visitor(result, parsedResults, declaration, "value");
134
+ },
138
135
 
139
- if (parsedResults.length === 0) {
140
- return Promise.resolve();
141
- }
136
+ async OnceExit() {
137
+ if (parsedResults.length === 0) {
138
+ return;
139
+ }
142
140
 
143
- const tasks = [];
144
- const imports = new Map();
145
- const replacements = new Map();
146
- let hasUrlImportHelper = false;
147
-
148
- for (const parsedResult of parsedResults) {
149
- const {
150
- url,
151
- isStringValue
152
- } = parsedResult.rule;
153
- let normalizedUrl = url;
154
- let prefix = '';
155
- const queryParts = normalizedUrl.split('!');
156
-
157
- if (queryParts.length > 1) {
158
- normalizedUrl = queryParts.pop();
159
- prefix = queryParts.join('!');
160
- }
141
+ const tasks = [];
142
+ const imports = new Map();
143
+ const replacements = new Map();
144
+ let hasUrlImportHelper = false;
145
+
146
+ for (const parsedResult of parsedResults) {
147
+ const {
148
+ url,
149
+ isStringValue
150
+ } = parsedResult.rule;
151
+ let normalizedUrl = url;
152
+ let prefix = "";
153
+ const queryParts = normalizedUrl.split("!");
154
+
155
+ if (queryParts.length > 1) {
156
+ normalizedUrl = queryParts.pop();
157
+ prefix = queryParts.join("!");
158
+ }
161
159
 
162
- normalizedUrl = (0, _utils.normalizeUrl)(normalizedUrl, isStringValue);
160
+ normalizedUrl = (0, _utils.normalizeUrl)(normalizedUrl, isStringValue);
163
161
 
164
- if (!options.filter(normalizedUrl)) {
165
- // eslint-disable-next-line no-continue
166
- continue;
167
- }
162
+ if (!options.filter(normalizedUrl)) {
163
+ // eslint-disable-next-line no-continue
164
+ continue;
165
+ }
168
166
 
169
- if (!hasUrlImportHelper) {
170
- options.imports.push({
171
- importName: '___CSS_LOADER_GET_URL_IMPORT___',
172
- url: options.urlHandler(require.resolve('../runtime/getUrl.js')),
173
- index: -1
174
- });
175
- hasUrlImportHelper = true;
176
- }
167
+ if (!hasUrlImportHelper) {
168
+ options.imports.push({
169
+ importName: "___CSS_LOADER_GET_URL_IMPORT___",
170
+ url: options.urlHandler(require.resolve("../runtime/getUrl.js")),
171
+ index: -1
172
+ });
173
+ hasUrlImportHelper = true;
174
+ }
177
175
 
178
- const splittedUrl = normalizedUrl.split(/(\?)?#/);
179
- const [pathname, query, hashOrQuery] = splittedUrl;
180
- let hash = query ? '?' : '';
181
- hash += hashOrQuery ? `#${hashOrQuery}` : '';
182
- const request = (0, _utils.requestify)(pathname, options.rootContext);
183
- tasks.push((async () => {
184
- const {
185
- resolver,
186
- context
187
- } = options;
188
- const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, normalizedUrl])]);
189
- return {
190
- url: resolvedUrl,
191
- prefix,
192
- hash,
193
- parsedResult
194
- };
195
- })());
196
- }
176
+ const splittedUrl = normalizedUrl.split(/(\?)?#/);
177
+ const [pathname, query, hashOrQuery] = splittedUrl;
178
+ let hash = query ? "?" : "";
179
+ hash += hashOrQuery ? `#${hashOrQuery}` : "";
180
+ const request = (0, _utils.requestify)(pathname, options.rootContext);
181
+ tasks.push((async () => {
182
+ const {
183
+ resolver,
184
+ context
185
+ } = options;
186
+ const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, normalizedUrl])]);
187
+ return {
188
+ url: resolvedUrl,
189
+ prefix,
190
+ hash,
191
+ parsedResult
192
+ };
193
+ })());
194
+ }
197
195
 
198
- const results = await Promise.all(tasks);
199
-
200
- for (let index = 0; index <= results.length - 1; index++) {
201
- const {
202
- url,
203
- prefix,
204
- hash,
205
- parsedResult: {
206
- decl,
207
- rule,
208
- parsed
209
- }
210
- } = results[index];
211
- const newUrl = prefix ? `${prefix}!${url}` : url;
212
- const importKey = newUrl;
213
- let importName = imports.get(importKey);
214
-
215
- if (!importName) {
216
- importName = `___CSS_LOADER_URL_IMPORT_${imports.size}___`;
217
- imports.set(importKey, importName);
218
- options.imports.push({
219
- importName,
220
- url: options.urlHandler(newUrl),
221
- index
222
- });
223
- }
196
+ const results = await Promise.all(tasks);
224
197
 
225
- const {
226
- needQuotes
227
- } = rule;
228
- const replacementKey = JSON.stringify({
229
- newUrl,
230
- hash,
231
- needQuotes
232
- });
233
- let replacementName = replacements.get(replacementKey);
198
+ for (let index = 0; index <= results.length - 1; index++) {
199
+ const {
200
+ url,
201
+ prefix,
202
+ hash,
203
+ parsedResult: {
204
+ node,
205
+ rule,
206
+ parsed
207
+ }
208
+ } = results[index];
209
+ const newUrl = prefix ? `${prefix}!${url}` : url;
210
+ const importKey = newUrl;
211
+ let importName = imports.get(importKey);
212
+
213
+ if (!importName) {
214
+ importName = `___CSS_LOADER_URL_IMPORT_${imports.size}___`;
215
+ imports.set(importKey, importName);
216
+ options.imports.push({
217
+ importName,
218
+ url: options.urlHandler(newUrl),
219
+ index
220
+ });
221
+ }
234
222
 
235
- if (!replacementName) {
236
- replacementName = `___CSS_LOADER_URL_REPLACEMENT_${replacements.size}___`;
237
- replacements.set(replacementKey, replacementName);
238
- options.replacements.push({
239
- replacementName,
240
- importName,
241
- hash,
242
- needQuotes
243
- });
244
- } // eslint-disable-next-line no-param-reassign
223
+ const {
224
+ needQuotes
225
+ } = rule;
226
+ const replacementKey = JSON.stringify({
227
+ newUrl,
228
+ hash,
229
+ needQuotes
230
+ });
231
+ let replacementName = replacements.get(replacementKey);
232
+
233
+ if (!replacementName) {
234
+ replacementName = `___CSS_LOADER_URL_REPLACEMENT_${replacements.size}___`;
235
+ replacements.set(replacementKey, replacementName);
236
+ options.replacements.push({
237
+ replacementName,
238
+ importName,
239
+ hash,
240
+ needQuotes
241
+ });
242
+ } // eslint-disable-next-line no-param-reassign
245
243
 
246
244
 
247
- rule.node.type = 'word'; // eslint-disable-next-line no-param-reassign
245
+ rule.node.type = "word"; // eslint-disable-next-line no-param-reassign
248
246
 
249
- rule.node.value = replacementName; // eslint-disable-next-line no-param-reassign
247
+ rule.node.value = replacementName; // eslint-disable-next-line no-param-reassign
250
248
 
251
- decl.value = parsed.toString();
252
- }
249
+ node.value = parsed.toString();
250
+ }
251
+ }
253
252
 
254
- return Promise.resolve();
255
- });
253
+ };
254
+ }
255
+
256
+ };
257
+ };
256
258
 
259
+ plugin.postcss = true;
260
+ var _default = plugin;
257
261
  exports.default = _default;
@@ -6,27 +6,27 @@
6
6
  */
7
7
  // css base code, injected by the css-loader
8
8
  // eslint-disable-next-line func-names
9
- module.exports = function (useSourceMap) {
9
+ module.exports = function (cssWithMappingToString) {
10
10
  var list = []; // return the list of modules as css string
11
11
 
12
12
  list.toString = function toString() {
13
13
  return this.map(function (item) {
14
- var content = cssWithMappingToString(item, useSourceMap);
14
+ var content = cssWithMappingToString(item);
15
15
 
16
16
  if (item[2]) {
17
17
  return "@media ".concat(item[2], " {").concat(content, "}");
18
18
  }
19
19
 
20
20
  return content;
21
- }).join('');
21
+ }).join("");
22
22
  }; // import a list of modules into the list
23
23
  // eslint-disable-next-line func-names
24
24
 
25
25
 
26
26
  list.i = function (modules, mediaQuery, dedupe) {
27
- if (typeof modules === 'string') {
27
+ if (typeof modules === "string") {
28
28
  // eslint-disable-next-line no-param-reassign
29
- modules = [[null, modules, '']];
29
+ modules = [[null, modules, ""]];
30
30
  }
31
31
 
32
32
  var alreadyImportedModules = {};
@@ -63,32 +63,4 @@ module.exports = function (useSourceMap) {
63
63
  };
64
64
 
65
65
  return list;
66
- };
67
-
68
- function cssWithMappingToString(item, useSourceMap) {
69
- var content = item[1] || ''; // eslint-disable-next-line prefer-destructuring
70
-
71
- var cssMapping = item[3];
72
-
73
- if (!cssMapping) {
74
- return content;
75
- }
76
-
77
- if (useSourceMap && typeof btoa === 'function') {
78
- var sourceMapping = toComment(cssMapping);
79
- var sourceURLs = cssMapping.sources.map(function (source) {
80
- return "/*# sourceURL=".concat(cssMapping.sourceRoot || '').concat(source, " */");
81
- });
82
- return [content].concat(sourceURLs).concat([sourceMapping]).join('\n');
83
- }
84
-
85
- return [content].join('\n');
86
- } // Adapted from convert-source-map (MIT)
87
-
88
-
89
- function toComment(sourceMap) {
90
- // eslint-disable-next-line no-undef
91
- var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
92
- var data = "sourceMappingURL=data:application/json;charset=utf-8;base64,".concat(base64);
93
- return "/*# ".concat(data, " */");
94
- }
66
+ };
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
4
+
5
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
6
+
7
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
+
9
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
10
+
11
+ function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
12
+
13
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
14
+
15
+ module.exports = function cssWithMappingToString(item) {
16
+ var _item = _slicedToArray(item, 4),
17
+ content = _item[1],
18
+ cssMapping = _item[3];
19
+
20
+ if (typeof btoa === "function") {
21
+ // eslint-disable-next-line no-undef
22
+ var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(cssMapping))));
23
+ var data = "sourceMappingURL=data:application/json;charset=utf-8;base64,".concat(base64);
24
+ var sourceMapping = "/*# ".concat(data, " */");
25
+ var sourceURLs = cssMapping.sources.map(function (source) {
26
+ return "/*# sourceURL=".concat(cssMapping.sourceRoot || "").concat(source, " */");
27
+ });
28
+ return [content].concat(sourceURLs).concat([sourceMapping]).join("\n");
29
+ }
30
+
31
+ return [content].join("\n");
32
+ };
@@ -9,7 +9,7 @@ module.exports = function (url, options) {
9
9
 
10
10
  url = url && url.__esModule ? url.default : url;
11
11
 
12
- if (typeof url !== 'string') {
12
+ if (typeof url !== "string") {
13
13
  return url;
14
14
  } // If url is already wrapped in quotes, remove them
15
15
 
@@ -27,7 +27,7 @@ module.exports = function (url, options) {
27
27
 
28
28
 
29
29
  if (/["'() \t\n]/.test(url) || options.needQuotes) {
30
- return "\"".concat(url.replace(/"/g, '\\"').replace(/\n/g, '\\n'), "\"");
30
+ return "\"".concat(url.replace(/"/g, '\\"').replace(/\n/g, "\\n"), "\"");
31
31
  }
32
32
 
33
33
  return url;