css-loader 1.0.1 → 7.1.4

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.
@@ -0,0 +1,293 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
8
+ var _utils = require("../utils");
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ function isIgnoredAfterName(atRule) {
11
+ if (atRule.raws && atRule.raws.afterName && atRule.raws.afterName.trim().length > 0) {
12
+ const lastCommentIndex = atRule.raws.afterName.lastIndexOf("/*");
13
+ const matched = atRule.raws.afterName.slice(lastCommentIndex).match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
14
+ if (matched && matched[2] === "true") {
15
+ return true;
16
+ }
17
+ }
18
+ return false;
19
+ }
20
+ function isIgnoredPrevNode(atRule) {
21
+ const prevNode = atRule.prev();
22
+ if (prevNode && prevNode.type === "comment") {
23
+ const matched = prevNode.text.match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
24
+ if (matched && matched[2] === "true") {
25
+ return true;
26
+ }
27
+ }
28
+ return false;
29
+ }
30
+ function parseNode(atRule, key, options) {
31
+ // Convert only top-level @import
32
+ if (atRule.parent.type !== "root") {
33
+ return;
34
+ }
35
+ const isIgnored = isIgnoredAfterName(atRule) || isIgnoredPrevNode(atRule);
36
+
37
+ // Nodes do not exists - `@import url('http://') :root {}`
38
+ if (atRule.nodes) {
39
+ const error = new Error("It looks like you didn't end your @import statement correctly. Child nodes are attached to it.");
40
+ error.node = atRule;
41
+ throw error;
42
+ }
43
+ const rawParams = atRule.raws && atRule.raws[key] && typeof atRule.raws[key].raw !== "undefined" ? atRule.raws[key].raw : atRule[key];
44
+ const {
45
+ nodes: paramsNodes
46
+ } = (0, _postcssValueParser.default)(rawParams);
47
+
48
+ // No nodes - `@import ;`
49
+ // Invalid type - `@import foo-bar;`
50
+ if (paramsNodes.length === 0 || paramsNodes[0].type !== "string" && paramsNodes[0].type !== "function") {
51
+ const error = new Error(`Unable to find uri in "${atRule.toString()}"`);
52
+ error.node = atRule;
53
+ throw error;
54
+ }
55
+ let isStringValue;
56
+ let url;
57
+ if (paramsNodes[0].type === "string") {
58
+ isStringValue = true;
59
+ url = paramsNodes[0].value;
60
+ } else {
61
+ // Invalid function - `@import nourl(test.css);`
62
+ if (paramsNodes[0].value.toLowerCase() !== "url") {
63
+ const error = new Error(`Unable to find uri in "${atRule.toString()}"`);
64
+ error.node = atRule;
65
+ throw error;
66
+ }
67
+ isStringValue = paramsNodes[0].nodes.length !== 0 && paramsNodes[0].nodes[0].type === "string";
68
+ url = isStringValue ? paramsNodes[0].nodes[0].value : _postcssValueParser.default.stringify(paramsNodes[0].nodes);
69
+ }
70
+ url = (0, _utils.normalizeUrl)(url, isStringValue);
71
+ let requestable = false;
72
+ let needResolve = false;
73
+ if (!isIgnored) {
74
+ ({
75
+ requestable,
76
+ needResolve
77
+ } = (0, _utils.isURLRequestable)(url, options));
78
+ }
79
+ let prefix;
80
+ if (requestable && needResolve) {
81
+ const queryParts = url.split("!");
82
+ if (queryParts.length > 1) {
83
+ url = queryParts.pop();
84
+ prefix = queryParts.join("!");
85
+ }
86
+ }
87
+
88
+ // Empty url - `@import "";` or `@import url();`
89
+ if (url.trim().length === 0) {
90
+ const error = new Error(`Unable to find uri in "${atRule.toString()}"`);
91
+ error.node = atRule;
92
+ throw error;
93
+ }
94
+ const additionalNodes = paramsNodes.slice(1);
95
+ let supports;
96
+ let layer;
97
+ let media;
98
+ if (additionalNodes.length > 0) {
99
+ let nodes = [];
100
+ for (const node of additionalNodes) {
101
+ nodes.push(node);
102
+ const isLayerFunction = node.type === "function" && node.value.toLowerCase() === "layer";
103
+ const isLayerWord = node.type === "word" && node.value.toLowerCase() === "layer";
104
+ if (isLayerFunction || isLayerWord) {
105
+ if (isLayerFunction) {
106
+ nodes.splice(nodes.length - 1, 1, ...node.nodes);
107
+ } else {
108
+ nodes.splice(nodes.length - 1, 1, {
109
+ type: "string",
110
+ value: "",
111
+ unclosed: false
112
+ });
113
+ }
114
+ layer = _postcssValueParser.default.stringify(nodes).trim().toLowerCase();
115
+ nodes = [];
116
+ } else if (node.type === "function" && node.value.toLowerCase() === "supports") {
117
+ nodes.splice(nodes.length - 1, 1, ...node.nodes);
118
+ supports = _postcssValueParser.default.stringify(nodes).trim().toLowerCase();
119
+ nodes = [];
120
+ }
121
+ }
122
+ if (nodes.length > 0) {
123
+ media = _postcssValueParser.default.stringify(nodes).trim().toLowerCase();
124
+ }
125
+ }
126
+
127
+ // eslint-disable-next-line consistent-return
128
+ return {
129
+ atRule,
130
+ prefix,
131
+ url,
132
+ layer,
133
+ supports,
134
+ media,
135
+ requestable,
136
+ needResolve
137
+ };
138
+ }
139
+ const plugin = (options = {}) => {
140
+ return {
141
+ postcssPlugin: "postcss-import-parser",
142
+ prepare(result) {
143
+ const parsedAtRules = [];
144
+ return {
145
+ AtRule: {
146
+ import(atRule) {
147
+ if (options.isCSSStyleSheet) {
148
+ options.loaderContext.emitError(new Error(atRule.error("'@import' rules are not allowed here and will not be processed").message));
149
+ return;
150
+ }
151
+ const {
152
+ isSupportDataURL,
153
+ isSupportAbsoluteURL
154
+ } = options;
155
+ let parsedAtRule;
156
+ try {
157
+ parsedAtRule = parseNode(atRule, "params", {
158
+ isSupportAbsoluteURL,
159
+ isSupportDataURL
160
+ });
161
+ } catch (error) {
162
+ result.warn(error.message, {
163
+ node: error.node
164
+ });
165
+ }
166
+ if (!parsedAtRule) {
167
+ return;
168
+ }
169
+ parsedAtRules.push(parsedAtRule);
170
+ }
171
+ },
172
+ async OnceExit() {
173
+ if (parsedAtRules.length === 0) {
174
+ return;
175
+ }
176
+ const {
177
+ loaderContext
178
+ } = options;
179
+ const resolver = loaderContext.getResolve({
180
+ dependencyType: "css",
181
+ conditionNames: ["style"],
182
+ mainFields: ["css", "style", "main", "..."],
183
+ mainFiles: ["index", "..."],
184
+ extensions: [".css", "..."],
185
+ preferRelative: true
186
+ });
187
+ const resolvedAtRules = await Promise.all(parsedAtRules.map(async parsedAtRule => {
188
+ const {
189
+ atRule,
190
+ requestable,
191
+ needResolve,
192
+ prefix,
193
+ url,
194
+ layer,
195
+ supports,
196
+ media
197
+ } = parsedAtRule;
198
+ if (options.filter) {
199
+ const needKeep = await options.filter(url, media, loaderContext.resourcePath, supports, layer);
200
+ if (!needKeep) {
201
+ return;
202
+ }
203
+ }
204
+ if (needResolve) {
205
+ const request = (0, _utils.requestify)(url, loaderContext.rootContext);
206
+ const resolvedUrl = await (0, _utils.resolveRequests)(resolver, loaderContext.context, [...new Set([request, url])]);
207
+ if (!resolvedUrl) {
208
+ return;
209
+ }
210
+ if (resolvedUrl === loaderContext.resourcePath) {
211
+ atRule.remove();
212
+ return;
213
+ }
214
+ atRule.remove();
215
+
216
+ // eslint-disable-next-line consistent-return
217
+ return {
218
+ url: resolvedUrl,
219
+ layer,
220
+ supports,
221
+ media,
222
+ prefix,
223
+ requestable
224
+ };
225
+ }
226
+ atRule.remove();
227
+
228
+ // eslint-disable-next-line consistent-return
229
+ return {
230
+ url,
231
+ layer,
232
+ supports,
233
+ media,
234
+ prefix,
235
+ requestable
236
+ };
237
+ }));
238
+ const urlToNameMap = new Map();
239
+ for (let index = 0; index <= resolvedAtRules.length - 1; index++) {
240
+ const resolvedAtRule = resolvedAtRules[index];
241
+ if (!resolvedAtRule) {
242
+ // eslint-disable-next-line no-continue
243
+ continue;
244
+ }
245
+ const {
246
+ url,
247
+ requestable,
248
+ layer,
249
+ supports,
250
+ media
251
+ } = resolvedAtRule;
252
+ if (!requestable) {
253
+ options.api.push({
254
+ url,
255
+ layer,
256
+ supports,
257
+ media,
258
+ index
259
+ });
260
+
261
+ // eslint-disable-next-line no-continue
262
+ continue;
263
+ }
264
+ const {
265
+ prefix
266
+ } = resolvedAtRule;
267
+ const newUrl = prefix ? `${prefix}!${url}` : url;
268
+ let importName = urlToNameMap.get(newUrl);
269
+ if (!importName) {
270
+ importName = `___CSS_LOADER_AT_RULE_IMPORT_${urlToNameMap.size}___`;
271
+ urlToNameMap.set(newUrl, importName);
272
+ options.imports.push({
273
+ type: "rule_import",
274
+ importName,
275
+ url: options.urlHandler(newUrl),
276
+ index
277
+ });
278
+ }
279
+ options.api.push({
280
+ importName,
281
+ layer,
282
+ supports,
283
+ media,
284
+ index
285
+ });
286
+ }
287
+ }
288
+ };
289
+ }
290
+ };
291
+ };
292
+ plugin.postcss = true;
293
+ var _default = exports.default = plugin;
@@ -0,0 +1,356 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
8
+ var _utils = require("../utils");
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ const isUrlFunc = /url/i;
11
+ const isImageSetFunc = /^(?:-webkit-)?image-set$/i;
12
+ const needParseDeclaration = /(?:url|(?:-webkit-)?image-set)\(/i;
13
+ function getNodeFromUrlFunc(node) {
14
+ return node.nodes && node.nodes[0];
15
+ }
16
+ function getWebpackIgnoreCommentValue(index, nodes, inBetween) {
17
+ if (index === 0 && typeof inBetween !== "undefined") {
18
+ return inBetween;
19
+ }
20
+ let prevValueNode = nodes[index - 1];
21
+ if (!prevValueNode) {
22
+ // eslint-disable-next-line consistent-return
23
+ return;
24
+ }
25
+ if (prevValueNode.type === "space") {
26
+ if (!nodes[index - 2]) {
27
+ // eslint-disable-next-line consistent-return
28
+ return;
29
+ }
30
+ prevValueNode = nodes[index - 2];
31
+ }
32
+ if (prevValueNode.type !== "comment") {
33
+ // eslint-disable-next-line consistent-return
34
+ return;
35
+ }
36
+ const matched = prevValueNode.value.match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
37
+ return matched && matched[2] === "true";
38
+ }
39
+ function shouldHandleURL(url, declaration, result, options) {
40
+ if (url.length === 0) {
41
+ result.warn(`Unable to find uri in '${declaration.toString()}'`, {
42
+ node: declaration
43
+ });
44
+ return {
45
+ requestable: false,
46
+ needResolve: false
47
+ };
48
+ }
49
+ return (0, _utils.isURLRequestable)(url, options);
50
+ }
51
+ function parseDeclaration(declaration, key, result, options) {
52
+ if (!needParseDeclaration.test(declaration[key])) {
53
+ return;
54
+ }
55
+ const parsed = (0, _postcssValueParser.default)(declaration.raws && declaration.raws.value && declaration.raws.value.raw ? declaration.raws.value.raw : declaration[key]);
56
+ let inBetween;
57
+ if (declaration.raws && declaration.raws.between) {
58
+ const lastCommentIndex = declaration.raws.between.lastIndexOf("/*");
59
+ const matched = declaration.raws.between.slice(lastCommentIndex).match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
60
+ if (matched) {
61
+ inBetween = matched[2] === "true";
62
+ }
63
+ }
64
+ let isIgnoreOnDeclaration = false;
65
+ const prevNode = declaration.prev();
66
+ if (prevNode && prevNode.type === "comment") {
67
+ const matched = prevNode.text.match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
68
+ if (matched) {
69
+ isIgnoreOnDeclaration = matched[2] === "true";
70
+ }
71
+ }
72
+ let needIgnore;
73
+ const parsedURLs = [];
74
+ parsed.walk((valueNode, index, valueNodes) => {
75
+ if (valueNode.type !== "function") {
76
+ return;
77
+ }
78
+ if (isUrlFunc.test(valueNode.value)) {
79
+ needIgnore = getWebpackIgnoreCommentValue(index, valueNodes, inBetween);
80
+ if (isIgnoreOnDeclaration && typeof needIgnore === "undefined" || needIgnore) {
81
+ if (needIgnore) {
82
+ // eslint-disable-next-line no-undefined
83
+ needIgnore = undefined;
84
+ }
85
+ return;
86
+ }
87
+ const {
88
+ nodes
89
+ } = valueNode;
90
+ const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
91
+ let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
92
+ url = (0, _utils.normalizeUrl)(url, isStringValue);
93
+ const {
94
+ requestable,
95
+ needResolve
96
+ } = shouldHandleURL(url, declaration, result, options);
97
+
98
+ // Do not traverse inside `url`
99
+ if (!requestable) {
100
+ // eslint-disable-next-line consistent-return
101
+ return false;
102
+ }
103
+ const queryParts = url.split("!");
104
+ let prefix;
105
+ if (queryParts.length > 1) {
106
+ url = queryParts.pop();
107
+ prefix = queryParts.join("!");
108
+ }
109
+ parsedURLs.push({
110
+ declaration,
111
+ parsed,
112
+ node: getNodeFromUrlFunc(valueNode),
113
+ prefix,
114
+ url,
115
+ needQuotes: false,
116
+ needResolve
117
+ });
118
+
119
+ // eslint-disable-next-line consistent-return
120
+ return false;
121
+ } else if (isImageSetFunc.test(valueNode.value)) {
122
+ for (const [innerIndex, nNode] of valueNode.nodes.entries()) {
123
+ const {
124
+ type,
125
+ value
126
+ } = nNode;
127
+ if (type === "function" && isUrlFunc.test(value)) {
128
+ needIgnore = getWebpackIgnoreCommentValue(innerIndex, valueNode.nodes);
129
+ if (isIgnoreOnDeclaration && typeof needIgnore === "undefined" || needIgnore) {
130
+ if (needIgnore) {
131
+ // eslint-disable-next-line no-undefined
132
+ needIgnore = undefined;
133
+ }
134
+
135
+ // eslint-disable-next-line no-continue
136
+ continue;
137
+ }
138
+ const {
139
+ nodes
140
+ } = nNode;
141
+ const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
142
+ let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
143
+ url = (0, _utils.normalizeUrl)(url, isStringValue);
144
+ const {
145
+ requestable,
146
+ needResolve
147
+ } = shouldHandleURL(url, declaration, result, options);
148
+
149
+ // Do not traverse inside `url`
150
+ if (!requestable) {
151
+ // eslint-disable-next-line consistent-return
152
+ return false;
153
+ }
154
+ const queryParts = url.split("!");
155
+ let prefix;
156
+ if (queryParts.length > 1) {
157
+ url = queryParts.pop();
158
+ prefix = queryParts.join("!");
159
+ }
160
+ parsedURLs.push({
161
+ declaration,
162
+ parsed,
163
+ node: getNodeFromUrlFunc(nNode),
164
+ prefix,
165
+ url,
166
+ needQuotes: false,
167
+ needResolve
168
+ });
169
+ } else if (type === "string") {
170
+ needIgnore = getWebpackIgnoreCommentValue(innerIndex, valueNode.nodes);
171
+ if (isIgnoreOnDeclaration && typeof needIgnore === "undefined" || needIgnore) {
172
+ if (needIgnore) {
173
+ // eslint-disable-next-line no-undefined
174
+ needIgnore = undefined;
175
+ }
176
+
177
+ // eslint-disable-next-line no-continue
178
+ continue;
179
+ }
180
+ let url = (0, _utils.normalizeUrl)(value, true);
181
+ const {
182
+ requestable,
183
+ needResolve
184
+ } = shouldHandleURL(url, declaration, result, options);
185
+
186
+ // Do not traverse inside `url`
187
+ if (!requestable) {
188
+ // eslint-disable-next-line consistent-return
189
+ return false;
190
+ }
191
+ const queryParts = url.split("!");
192
+ let prefix;
193
+ if (queryParts.length > 1) {
194
+ url = queryParts.pop();
195
+ prefix = queryParts.join("!");
196
+ }
197
+ parsedURLs.push({
198
+ declaration,
199
+ parsed,
200
+ node: nNode,
201
+ prefix,
202
+ url,
203
+ needQuotes: true,
204
+ needResolve
205
+ });
206
+ }
207
+ }
208
+
209
+ // Do not traverse inside `image-set`
210
+ // eslint-disable-next-line consistent-return
211
+ return false;
212
+ }
213
+ });
214
+
215
+ // eslint-disable-next-line consistent-return
216
+ return parsedURLs;
217
+ }
218
+ const plugin = (options = {}) => {
219
+ return {
220
+ postcssPlugin: "postcss-url-parser",
221
+ prepare(result) {
222
+ const parsedDeclarations = [];
223
+ return {
224
+ Declaration(declaration) {
225
+ const {
226
+ isSupportDataURL,
227
+ isSupportAbsoluteURL
228
+ } = options;
229
+ const parsedURL = parseDeclaration(declaration, "value", result, {
230
+ isSupportDataURL,
231
+ isSupportAbsoluteURL
232
+ });
233
+ if (!parsedURL) {
234
+ return;
235
+ }
236
+ parsedDeclarations.push(...parsedURL);
237
+ },
238
+ async OnceExit() {
239
+ if (parsedDeclarations.length === 0) {
240
+ return;
241
+ }
242
+ const resolvedDeclarations = await Promise.all(parsedDeclarations.map(async parsedDeclaration => {
243
+ const {
244
+ url,
245
+ needResolve
246
+ } = parsedDeclaration;
247
+ if (options.filter) {
248
+ const needKeep = await options.filter(url);
249
+ if (!needKeep) {
250
+ // eslint-disable-next-line consistent-return
251
+ return;
252
+ }
253
+ }
254
+ if (!needResolve) {
255
+ // eslint-disable-next-line consistent-return
256
+ return parsedDeclaration;
257
+ }
258
+ const splittedUrl = url.split(/(\?)?#/);
259
+ const [pathname, query, hashOrQuery] = splittedUrl;
260
+ let hash = query ? "?" : "";
261
+ hash += hashOrQuery ? `#${hashOrQuery}` : "";
262
+ const {
263
+ resolver,
264
+ rootContext
265
+ } = options;
266
+ const request = (0, _utils.requestify)(pathname, rootContext, Boolean(resolver));
267
+ if (!resolver) {
268
+ // eslint-disable-next-line consistent-return
269
+ return {
270
+ ...parsedDeclaration,
271
+ url: request,
272
+ hash
273
+ };
274
+ }
275
+ const resolvedURL = await (0, _utils.resolveRequests)(resolver, options.context, [...new Set([request, url])]);
276
+ if (!resolvedURL) {
277
+ // eslint-disable-next-line consistent-return
278
+ return;
279
+ }
280
+
281
+ // eslint-disable-next-line consistent-return
282
+ return {
283
+ ...parsedDeclaration,
284
+ url: resolvedURL,
285
+ hash
286
+ };
287
+ }));
288
+ const urlToNameMap = new Map();
289
+ const urlToReplacementMap = new Map();
290
+ let hasUrlImportHelper = false;
291
+ for (let index = 0; index <= resolvedDeclarations.length - 1; index++) {
292
+ const item = resolvedDeclarations[index];
293
+ if (!item) {
294
+ // eslint-disable-next-line no-continue
295
+ continue;
296
+ }
297
+ if (!hasUrlImportHelper) {
298
+ options.imports.push({
299
+ type: "get_url_import",
300
+ importName: "___CSS_LOADER_GET_URL_IMPORT___",
301
+ url: options.urlHandler(require.resolve("../runtime/getUrl.js")),
302
+ index: -1
303
+ });
304
+ hasUrlImportHelper = true;
305
+ }
306
+ const {
307
+ url,
308
+ prefix
309
+ } = item;
310
+ const newUrl = prefix ? `${prefix}!${url}` : url;
311
+ let importName = urlToNameMap.get(newUrl);
312
+ if (!importName) {
313
+ importName = `___CSS_LOADER_URL_IMPORT_${urlToNameMap.size}___`;
314
+ urlToNameMap.set(newUrl, importName);
315
+ options.imports.push({
316
+ type: "url",
317
+ importName,
318
+ url: options.resolver ? options.urlHandler(newUrl) : JSON.stringify(newUrl),
319
+ index
320
+ });
321
+ }
322
+ const {
323
+ hash,
324
+ needQuotes
325
+ } = item;
326
+ const replacementKey = JSON.stringify({
327
+ newUrl,
328
+ hash,
329
+ needQuotes
330
+ });
331
+ let replacementName = urlToReplacementMap.get(replacementKey);
332
+ if (!replacementName) {
333
+ replacementName = `___CSS_LOADER_URL_REPLACEMENT_${urlToReplacementMap.size}___`;
334
+ urlToReplacementMap.set(replacementKey, replacementName);
335
+ options.replacements.push({
336
+ replacementName,
337
+ importName,
338
+ hash,
339
+ needQuotes
340
+ });
341
+ }
342
+
343
+ // eslint-disable-next-line no-param-reassign
344
+ item.node.type = "word";
345
+ // eslint-disable-next-line no-param-reassign
346
+ item.node.value = replacementName;
347
+ // eslint-disable-next-line no-param-reassign
348
+ item.declaration.value = item.parsed.toString();
349
+ }
350
+ }
351
+ };
352
+ }
353
+ };
354
+ };
355
+ plugin.postcss = true;
356
+ var _default = exports.default = plugin;