css-loader 4.3.0 → 5.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.
@@ -5,210 +5,235 @@ 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-import-parser';
19
-
20
- function walkAtRules(css, result, options, callback) {
21
- const accumulator = [];
22
- css.walkAtRules(/^import$/i, atRule => {
23
- // Convert only top-level @import
24
- if (atRule.parent.type !== 'root') {
25
- return;
26
- } // Nodes do not exists - `@import url('http://') :root {}`
14
+ function visitor(result, parsedResults, node, key) {
15
+ // Convert only top-level @import
16
+ if (node.parent.type !== "root") {
17
+ return;
18
+ }
27
19
 
20
+ if (node.raws.afterName && node.raws.afterName.trim().length > 0) {
21
+ const lastCommentIndex = node.raws.afterName.lastIndexOf("/*");
22
+ const matched = node.raws.afterName.slice(lastCommentIndex).match(_utils.webpackIgnoreCommentRegexp);
28
23
 
29
- if (atRule.nodes) {
30
- result.warn("It looks like you didn't end your @import statement correctly. Child nodes are attached to it.", {
31
- node: atRule
32
- });
24
+ if (matched && matched[2] === "true") {
33
25
  return;
34
26
  }
27
+ }
35
28
 
36
- const {
37
- nodes: paramsNodes
38
- } = (0, _postcssValueParser.default)(atRule.params); // No nodes - `@import ;`
39
- // Invalid type - `@import foo-bar;`
29
+ const prevNode = node.prev();
40
30
 
41
- if (paramsNodes.length === 0 || paramsNodes[0].type !== 'string' && paramsNodes[0].type !== 'function') {
42
- result.warn(`Unable to find uri in "${atRule.toString()}"`, {
43
- node: atRule
44
- });
45
- return;
46
- }
31
+ if (prevNode && prevNode.type === "comment") {
32
+ const matched = prevNode.text.match(_utils.webpackIgnoreCommentRegexp);
47
33
 
48
- let isStringValue;
49
- let url;
50
-
51
- if (paramsNodes[0].type === 'string') {
52
- isStringValue = true;
53
- url = paramsNodes[0].value;
54
- } else {
55
- // Invalid function - `@import nourl(test.css);`
56
- if (paramsNodes[0].value.toLowerCase() !== 'url') {
57
- result.warn(`Unable to find uri in "${atRule.toString()}"`, {
58
- node: atRule
59
- });
60
- return;
61
- }
62
-
63
- isStringValue = paramsNodes[0].nodes.length !== 0 && paramsNodes[0].nodes[0].type === 'string';
64
- url = isStringValue ? paramsNodes[0].nodes[0].value : _postcssValueParser.default.stringify(paramsNodes[0].nodes);
65
- } // Empty url - `@import "";` or `@import url();`
66
-
67
-
68
- if (url.trim().length === 0) {
69
- result.warn(`Unable to find uri in "${atRule.toString()}"`, {
70
- node: atRule
71
- });
34
+ if (matched && matched[2] === "true") {
72
35
  return;
73
36
  }
37
+ } // Nodes do not exists - `@import url('http://') :root {}`
74
38
 
75
- accumulator.push({
76
- atRule,
77
- url,
78
- isStringValue,
79
- mediaNodes: paramsNodes.slice(1)
80
- });
81
- });
82
- callback(null, accumulator);
83
- }
84
39
 
85
- const asyncWalkAtRules = (0, _util.promisify)(walkAtRules);
40
+ if (node.nodes) {
41
+ result.warn("It looks like you didn't end your @import statement correctly. Child nodes are attached to it.", {
42
+ node
43
+ });
44
+ return;
45
+ }
86
46
 
87
- var _default = _postcss.default.plugin(pluginName, options => async (css, result) => {
88
- const parsedResults = await asyncWalkAtRules(css, result, options);
47
+ const {
48
+ nodes: paramsNodes
49
+ } = (0, _postcssValueParser.default)(node[key]); // No nodes - `@import ;`
50
+ // Invalid type - `@import foo-bar;`
89
51
 
90
- if (parsedResults.length === 0) {
91
- return Promise.resolve();
52
+ if (paramsNodes.length === 0 || paramsNodes[0].type !== "string" && paramsNodes[0].type !== "function") {
53
+ result.warn(`Unable to find uri in "${node.toString()}"`, {
54
+ node
55
+ });
56
+ return;
92
57
  }
93
58
 
94
- const imports = new Map();
95
- const tasks = [];
96
-
97
- for (const parsedResult of parsedResults) {
98
- const {
99
- atRule,
100
- url,
101
- isStringValue,
102
- mediaNodes
103
- } = parsedResult;
104
- let normalizedUrl = url;
105
- let prefix = '';
106
- const isRequestable = (0, _utils.isUrlRequestable)(normalizedUrl);
107
-
108
- if (isRequestable) {
109
- const queryParts = normalizedUrl.split('!');
110
-
111
- if (queryParts.length > 1) {
112
- normalizedUrl = queryParts.pop();
113
- prefix = queryParts.join('!');
114
- }
115
-
116
- normalizedUrl = (0, _utils.normalizeUrl)(normalizedUrl, isStringValue); // Empty url after normalize - `@import '\
117
- // \
118
- // \
119
- // ';
120
-
121
- if (normalizedUrl.trim().length === 0) {
122
- result.warn(`Unable to find uri in "${atRule.toString()}"`, {
123
- node: atRule
124
- }); // eslint-disable-next-line no-continue
125
-
126
- continue;
127
- }
59
+ let isStringValue;
60
+ let url;
61
+
62
+ if (paramsNodes[0].type === "string") {
63
+ isStringValue = true;
64
+ url = paramsNodes[0].value;
65
+ } else {
66
+ // Invalid function - `@import nourl(test.css);`
67
+ if (paramsNodes[0].value.toLowerCase() !== "url") {
68
+ result.warn(`Unable to find uri in "${node.toString()}"`, {
69
+ node
70
+ });
71
+ return;
128
72
  }
129
73
 
130
- let media;
131
-
132
- if (mediaNodes.length > 0) {
133
- media = _postcssValueParser.default.stringify(mediaNodes).trim().toLowerCase();
134
- }
74
+ isStringValue = paramsNodes[0].nodes.length !== 0 && paramsNodes[0].nodes[0].type === "string";
75
+ url = isStringValue ? paramsNodes[0].nodes[0].value : _postcssValueParser.default.stringify(paramsNodes[0].nodes);
76
+ } // Empty url - `@import "";` or `@import url();`
135
77
 
136
- if (options.filter && !options.filter(normalizedUrl, media)) {
137
- // eslint-disable-next-line no-continue
138
- continue;
139
- }
140
78
 
141
- atRule.remove();
142
-
143
- if (isRequestable) {
144
- const request = (0, _utils.requestify)(normalizedUrl, options.rootContext);
145
- tasks.push((async () => {
146
- const {
147
- resolver,
148
- context
149
- } = options;
150
- const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, normalizedUrl])]);
151
- return {
152
- url: resolvedUrl,
153
- media,
154
- prefix,
155
- isRequestable
156
- };
157
- })());
158
- } else {
159
- tasks.push({
160
- url,
161
- media,
162
- prefix,
163
- isRequestable
164
- });
165
- }
79
+ if (url.trim().length === 0) {
80
+ result.warn(`Unable to find uri in "${node.toString()}"`, {
81
+ node
82
+ });
83
+ return;
166
84
  }
167
85
 
168
- const results = await Promise.all(tasks);
169
-
170
- for (let index = 0; index <= results.length - 1; index++) {
171
- const {
172
- url,
173
- isRequestable,
174
- media
175
- } = results[index];
176
-
177
- if (isRequestable) {
178
- const {
179
- prefix
180
- } = results[index];
181
- const newUrl = prefix ? `${prefix}!${url}` : url;
182
- const importKey = newUrl;
183
- let importName = imports.get(importKey);
184
-
185
- if (!importName) {
186
- importName = `___CSS_LOADER_AT_RULE_IMPORT_${imports.size}___`;
187
- imports.set(importKey, importName);
188
- options.imports.push({
189
- importName,
190
- url: options.urlHandler(newUrl),
191
- index
192
- });
193
- }
194
-
195
- options.api.push({
196
- importName,
197
- media,
198
- index
199
- }); // eslint-disable-next-line no-continue
200
-
201
- continue;
202
- }
86
+ parsedResults.push({
87
+ node,
88
+ url,
89
+ isStringValue,
90
+ mediaNodes: paramsNodes.slice(1)
91
+ });
92
+ }
203
93
 
204
- options.api.push({
205
- url,
206
- media,
207
- index
208
- });
209
- }
94
+ const plugin = (options = {}) => {
95
+ return {
96
+ postcssPlugin: "postcss-import-parser",
97
+
98
+ prepare(result) {
99
+ const parsedResults = [];
100
+ return {
101
+ AtRule: {
102
+ import(atRule) {
103
+ visitor(result, parsedResults, atRule, "params");
104
+ }
105
+
106
+ },
107
+
108
+ async OnceExit() {
109
+ if (parsedResults.length === 0) {
110
+ return;
111
+ }
112
+
113
+ const imports = new Map();
114
+ const tasks = [];
115
+
116
+ for (const parsedResult of parsedResults) {
117
+ const {
118
+ node,
119
+ url,
120
+ isStringValue,
121
+ mediaNodes
122
+ } = parsedResult;
123
+ let normalizedUrl = url;
124
+ let prefix = "";
125
+ const isRequestable = (0, _utils.isUrlRequestable)(normalizedUrl);
126
+
127
+ if (isRequestable) {
128
+ const queryParts = normalizedUrl.split("!");
129
+
130
+ if (queryParts.length > 1) {
131
+ normalizedUrl = queryParts.pop();
132
+ prefix = queryParts.join("!");
133
+ }
134
+
135
+ normalizedUrl = (0, _utils.normalizeUrl)(normalizedUrl, isStringValue); // Empty url after normalize - `@import '\
136
+ // \
137
+ // \
138
+ // ';
139
+
140
+ if (normalizedUrl.trim().length === 0) {
141
+ result.warn(`Unable to find uri in "${node.toString()}"`, {
142
+ node
143
+ }); // eslint-disable-next-line no-continue
144
+
145
+ continue;
146
+ }
147
+ }
148
+
149
+ let media;
150
+
151
+ if (mediaNodes.length > 0) {
152
+ media = _postcssValueParser.default.stringify(mediaNodes).trim().toLowerCase();
153
+ }
154
+
155
+ if (options.filter && !options.filter(normalizedUrl, media)) {
156
+ // eslint-disable-next-line no-continue
157
+ continue;
158
+ }
159
+
160
+ node.remove();
161
+
162
+ if (isRequestable) {
163
+ const request = (0, _utils.requestify)(normalizedUrl, options.rootContext);
164
+ tasks.push((async () => {
165
+ const {
166
+ resolver,
167
+ context
168
+ } = options;
169
+ const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, normalizedUrl])]);
170
+ return {
171
+ url: resolvedUrl,
172
+ media,
173
+ prefix,
174
+ isRequestable
175
+ };
176
+ })());
177
+ } else {
178
+ tasks.push({
179
+ url,
180
+ media,
181
+ prefix,
182
+ isRequestable
183
+ });
184
+ }
185
+ }
186
+
187
+ const results = await Promise.all(tasks);
188
+
189
+ for (let index = 0; index <= results.length - 1; index++) {
190
+ const {
191
+ url,
192
+ isRequestable,
193
+ media
194
+ } = results[index];
195
+
196
+ if (isRequestable) {
197
+ const {
198
+ prefix
199
+ } = results[index];
200
+ const newUrl = prefix ? `${prefix}!${url}` : url;
201
+ const importKey = newUrl;
202
+ let importName = imports.get(importKey);
203
+
204
+ if (!importName) {
205
+ importName = `___CSS_LOADER_AT_RULE_IMPORT_${imports.size}___`;
206
+ imports.set(importKey, importName);
207
+ options.imports.push({
208
+ importName,
209
+ url: options.urlHandler(newUrl),
210
+ index
211
+ });
212
+ }
213
+
214
+ options.api.push({
215
+ importName,
216
+ media,
217
+ index
218
+ }); // eslint-disable-next-line no-continue
219
+
220
+ continue;
221
+ }
222
+
223
+ options.api.push({
224
+ url,
225
+ media,
226
+ index
227
+ });
228
+ }
229
+ }
230
+
231
+ };
232
+ }
210
233
 
211
- return Promise.resolve();
212
- });
234
+ };
235
+ };
213
236
 
237
+ plugin.postcss = true;
238
+ var _default = plugin;
214
239
  exports.default = _default;