css-loader 6.4.0 → 6.5.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/dist/index.js CHANGED
@@ -49,9 +49,18 @@ async function loader(content, map, meta) {
49
49
 
50
50
  const importPluginImports = [];
51
51
  const importPluginApi = [];
52
+ let isSupportAbsoluteURL = false; // TODO enable by default in the next major release
53
+
54
+ if (this._compilation && this._compilation.options && this._compilation.options.experiments && this._compilation.options.experiments.buildHttp) {
55
+ isSupportAbsoluteURL = true;
56
+ }
57
+
58
+ const isSupportDataURL = options.esModule && Boolean("fsStartTime" in this._compiler);
52
59
 
53
60
  if ((0, _utils.shouldUseImportPlugin)(options)) {
54
61
  plugins.push((0, _plugins.importParser)({
62
+ isSupportAbsoluteURL: false,
63
+ isSupportDataURL: false,
55
64
  isCSSStyleSheet: options.exportType === "css-style-sheet",
56
65
  loaderContext: this,
57
66
  imports: importPluginImports,
@@ -65,22 +74,21 @@ async function loader(content, map, meta) {
65
74
 
66
75
  if ((0, _utils.shouldUseURLPlugin)(options)) {
67
76
  const needToResolveURL = !options.esModule;
68
- const isSupportDataURLInNewURL = options.esModule && Boolean("fsStartTime" in this._compiler);
69
77
  plugins.push((0, _plugins.urlParser)({
78
+ isSupportAbsoluteURL,
79
+ isSupportDataURL,
70
80
  imports: urlPluginImports,
71
81
  replacements,
72
82
  context: this.context,
73
83
  rootContext: this.rootContext,
74
84
  filter: (0, _utils.getFilter)(options.url.filter, this.resourcePath),
75
- needToResolveURL,
76
85
  resolver: needToResolveURL ? this.getResolve({
77
86
  mainFiles: [],
78
87
  extensions: []
79
88
  }) : // eslint-disable-next-line no-undefined
80
89
  undefined,
81
- urlHandler: url => (0, _utils.stringifyRequest)(this, url),
82
- // Support data urls as input in new URL added in webpack@5.38.0
83
- isSupportDataURLInNewURL
90
+ urlHandler: url => (0, _utils.stringifyRequest)(this, url) // Support data urls as input in new URL added in webpack@5.38.0
91
+
84
92
  }));
85
93
  }
86
94
 
@@ -11,7 +11,7 @@ var _utils = require("../utils");
11
11
 
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
13
 
14
- function parseNode(atRule, key) {
14
+ function parseNode(atRule, key, options) {
15
15
  // Convert only top-level @import
16
16
  if (atRule.parent.type !== "root") {
17
17
  return;
@@ -74,10 +74,13 @@ function parseNode(atRule, key) {
74
74
  }
75
75
 
76
76
  url = (0, _utils.normalizeUrl)(url, isStringValue);
77
- const isRequestable = (0, _utils.isUrlRequestable)(url);
77
+ const {
78
+ requestable,
79
+ needResolve
80
+ } = (0, _utils.isURLRequestable)(url, options);
78
81
  let prefix;
79
82
 
80
- if (isRequestable) {
83
+ if (requestable && needResolve) {
81
84
  const queryParts = url.split("!");
82
85
 
83
86
  if (queryParts.length > 1) {
@@ -139,7 +142,8 @@ function parseNode(atRule, key) {
139
142
  layer,
140
143
  supports,
141
144
  media,
142
- isRequestable
145
+ requestable,
146
+ needResolve
143
147
  };
144
148
  }
145
149
 
@@ -157,10 +161,17 @@ const plugin = (options = {}) => {
157
161
  return;
158
162
  }
159
163
 
164
+ const {
165
+ isSupportDataURL,
166
+ isSupportAbsoluteURL
167
+ } = options;
160
168
  let parsedAtRule;
161
169
 
162
170
  try {
163
- parsedAtRule = parseNode(atRule, "params", result);
171
+ parsedAtRule = parseNode(atRule, "params", {
172
+ isSupportAbsoluteURL,
173
+ isSupportDataURL
174
+ });
164
175
  } catch (error) {
165
176
  result.warn(error.message, {
166
177
  node: error.node
@@ -195,7 +206,8 @@ const plugin = (options = {}) => {
195
206
  const resolvedAtRules = await Promise.all(parsedAtRules.map(async parsedAtRule => {
196
207
  const {
197
208
  atRule,
198
- isRequestable,
209
+ requestable,
210
+ needResolve,
199
211
  prefix,
200
212
  url,
201
213
  layer,
@@ -211,7 +223,7 @@ const plugin = (options = {}) => {
211
223
  }
212
224
  }
213
225
 
214
- if (isRequestable) {
226
+ if (needResolve) {
215
227
  const request = (0, _utils.requestify)(url, loaderContext.rootContext);
216
228
  const resolvedUrl = await (0, _utils.resolveRequests)(resolver, loaderContext.context, [...new Set([request, url])]);
217
229
 
@@ -232,7 +244,7 @@ const plugin = (options = {}) => {
232
244
  supports,
233
245
  media,
234
246
  prefix,
235
- isRequestable
247
+ requestable
236
248
  };
237
249
  }
238
250
 
@@ -244,7 +256,7 @@ const plugin = (options = {}) => {
244
256
  supports,
245
257
  media,
246
258
  prefix,
247
- isRequestable
259
+ requestable
248
260
  };
249
261
  }));
250
262
  const urlToNameMap = new Map();
@@ -259,13 +271,13 @@ const plugin = (options = {}) => {
259
271
 
260
272
  const {
261
273
  url,
262
- isRequestable,
274
+ requestable,
263
275
  layer,
264
276
  supports,
265
277
  media
266
278
  } = resolvedAtRule;
267
279
 
268
- if (!isRequestable) {
280
+ if (!requestable) {
269
281
  options.api.push({
270
282
  url,
271
283
  layer,
@@ -49,32 +49,21 @@ function getWebpackIgnoreCommentValue(index, nodes, inBetween) {
49
49
  return matched && matched[2] === "true";
50
50
  }
51
51
 
52
- function shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL) {
52
+ function shouldHandleURL(url, declaration, result, options) {
53
53
  if (url.length === 0) {
54
54
  result.warn(`Unable to find uri in '${declaration.toString()}'`, {
55
55
  node: declaration
56
56
  });
57
- return false;
57
+ return {
58
+ requestable: false,
59
+ needResolve: false
60
+ };
58
61
  }
59
62
 
60
- if ((0, _utils.isDataUrl)(url) && isSupportDataURLInNewURL) {
61
- try {
62
- decodeURIComponent(url);
63
- } catch (ignoreError) {
64
- return false;
65
- }
66
-
67
- return true;
68
- }
69
-
70
- if (!(0, _utils.isUrlRequestable)(url)) {
71
- return false;
72
- }
73
-
74
- return true;
63
+ return (0, _utils.isURLRequestable)(url, options);
75
64
  }
76
65
 
77
- function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
66
+ function parseDeclaration(declaration, key, result, options) {
78
67
  if (!needParseDeclaration.test(declaration[key])) {
79
68
  return;
80
69
  }
@@ -126,9 +115,13 @@ function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
126
115
  } = valueNode;
127
116
  const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
128
117
  let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
129
- url = (0, _utils.normalizeUrl)(url, isStringValue); // Do not traverse inside `url`
118
+ url = (0, _utils.normalizeUrl)(url, isStringValue);
119
+ const {
120
+ requestable,
121
+ needResolve
122
+ } = shouldHandleURL(url, declaration, result, options); // Do not traverse inside `url`
130
123
 
131
- if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
124
+ if (!requestable) {
132
125
  // eslint-disable-next-line consistent-return
133
126
  return false;
134
127
  }
@@ -147,7 +140,8 @@ function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
147
140
  node: getNodeFromUrlFunc(valueNode),
148
141
  prefix,
149
142
  url,
150
- needQuotes: false
143
+ needQuotes: false,
144
+ needResolve
151
145
  }); // eslint-disable-next-line consistent-return
152
146
 
153
147
  return false;
@@ -176,9 +170,13 @@ function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
176
170
  } = nNode;
177
171
  const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
178
172
  let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
179
- url = (0, _utils.normalizeUrl)(url, isStringValue); // Do not traverse inside `url`
173
+ url = (0, _utils.normalizeUrl)(url, isStringValue);
174
+ const {
175
+ requestable,
176
+ needResolve
177
+ } = shouldHandleURL(url, declaration, result, options); // Do not traverse inside `url`
180
178
 
181
- if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
179
+ if (!requestable) {
182
180
  // eslint-disable-next-line consistent-return
183
181
  return false;
184
182
  }
@@ -197,7 +195,8 @@ function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
197
195
  node: getNodeFromUrlFunc(nNode),
198
196
  prefix,
199
197
  url,
200
- needQuotes: false
198
+ needQuotes: false,
199
+ needResolve
201
200
  });
202
201
  } else if (type === "string") {
203
202
  needIgnore = getWebpackIgnoreCommentValue(innerIndex, valueNode.nodes);
@@ -212,9 +211,13 @@ function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
212
211
  continue;
213
212
  }
214
213
 
215
- let url = (0, _utils.normalizeUrl)(value, true); // Do not traverse inside `url`
214
+ let url = (0, _utils.normalizeUrl)(value, true);
215
+ const {
216
+ requestable,
217
+ needResolve
218
+ } = shouldHandleURL(url, declaration, result, options); // Do not traverse inside `url`
216
219
 
217
- if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
220
+ if (!requestable) {
218
221
  // eslint-disable-next-line consistent-return
219
222
  return false;
220
223
  }
@@ -233,7 +236,8 @@ function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
233
236
  node: nNode,
234
237
  prefix,
235
238
  url,
236
- needQuotes: true
239
+ needQuotes: true,
240
+ needResolve
237
241
  });
238
242
  }
239
243
  } // Do not traverse inside `image-set`
@@ -256,9 +260,13 @@ const plugin = (options = {}) => {
256
260
  return {
257
261
  Declaration(declaration) {
258
262
  const {
259
- isSupportDataURLInNewURL
263
+ isSupportDataURL,
264
+ isSupportAbsoluteURL
260
265
  } = options;
261
- const parsedURL = parseDeclaration(declaration, "value", result, isSupportDataURLInNewURL);
266
+ const parsedURL = parseDeclaration(declaration, "value", result, {
267
+ isSupportDataURL,
268
+ isSupportAbsoluteURL
269
+ });
262
270
 
263
271
  if (!parsedURL) {
264
272
  return;
@@ -274,7 +282,8 @@ const plugin = (options = {}) => {
274
282
 
275
283
  const resolvedDeclarations = await Promise.all(parsedDeclarations.map(async parsedDeclaration => {
276
284
  const {
277
- url
285
+ url,
286
+ needResolve
278
287
  } = parsedDeclaration;
279
288
 
280
289
  if (options.filter) {
@@ -286,7 +295,7 @@ const plugin = (options = {}) => {
286
295
  }
287
296
  }
288
297
 
289
- if ((0, _utils.isDataUrl)(url)) {
298
+ if (!needResolve) {
290
299
  // eslint-disable-next-line consistent-return
291
300
  return parsedDeclaration;
292
301
  }
@@ -296,12 +305,12 @@ const plugin = (options = {}) => {
296
305
  let hash = query ? "?" : "";
297
306
  hash += hashOrQuery ? `#${hashOrQuery}` : "";
298
307
  const {
299
- needToResolveURL,
308
+ resolver,
300
309
  rootContext
301
310
  } = options;
302
- const request = (0, _utils.requestify)(pathname, rootContext, needToResolveURL);
311
+ const request = (0, _utils.requestify)(pathname, rootContext, Boolean(resolver));
303
312
 
304
- if (!needToResolveURL) {
313
+ if (!resolver) {
305
314
  // eslint-disable-next-line consistent-return
306
315
  return { ...parsedDeclaration,
307
316
  url: request,
@@ -309,20 +318,16 @@ const plugin = (options = {}) => {
309
318
  };
310
319
  }
311
320
 
312
- const {
313
- resolver,
314
- context
315
- } = options;
316
- const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, url])]);
321
+ const resolvedURL = await (0, _utils.resolveRequests)(resolver, options.context, [...new Set([request, url])]);
317
322
 
318
- if (!resolvedUrl) {
323
+ if (!resolvedURL) {
319
324
  // eslint-disable-next-line consistent-return
320
325
  return;
321
326
  } // eslint-disable-next-line consistent-return
322
327
 
323
328
 
324
329
  return { ...parsedDeclaration,
325
- url: resolvedUrl,
330
+ url: resolvedURL,
326
331
  hash
327
332
  };
328
333
  }));
@@ -361,7 +366,7 @@ const plugin = (options = {}) => {
361
366
  options.imports.push({
362
367
  type: "url",
363
368
  importName,
364
- url: options.needToResolveURL ? options.urlHandler(newUrl) : JSON.stringify(newUrl),
369
+ url: options.resolver ? options.urlHandler(newUrl) : JSON.stringify(newUrl),
365
370
  index
366
371
  });
367
372
  }
@@ -51,8 +51,8 @@ module.exports = function (cssWithMappingToString) {
51
51
  var alreadyImportedModules = {};
52
52
 
53
53
  if (dedupe) {
54
- for (var _i = 0; _i < this.length; _i++) {
55
- var id = this[_i][0];
54
+ for (var k = 0; k < this.length; k++) {
55
+ var id = this[k][0];
56
56
 
57
57
  if (id != null) {
58
58
  alreadyImportedModules[id] = true;
@@ -60,8 +60,8 @@ module.exports = function (cssWithMappingToString) {
60
60
  }
61
61
  }
62
62
 
63
- for (var _i2 = 0; _i2 < modules.length; _i2++) {
64
- var item = [].concat(modules[_i2]);
63
+ for (var _k = 0; _k < modules.length; _k++) {
64
+ var item = [].concat(modules[_k]);
65
65
 
66
66
  if (dedupe && alreadyImportedModules[item[0]]) {
67
67
  continue;
package/dist/utils.js CHANGED
@@ -14,7 +14,7 @@ exports.getModulesOptions = getModulesOptions;
14
14
  exports.getModulesPlugins = getModulesPlugins;
15
15
  exports.getPreRequester = getPreRequester;
16
16
  exports.isDataUrl = isDataUrl;
17
- exports.isUrlRequestable = isUrlRequestable;
17
+ exports.isURLRequestable = isURLRequestable;
18
18
  exports.normalizeOptions = normalizeOptions;
19
19
  exports.normalizeSourceMap = normalizeSourceMap;
20
20
  exports.normalizeUrl = normalizeUrl;
@@ -55,9 +55,14 @@ function isAbsolutePath(str) {
55
55
 
56
56
  function isRelativePath(str) {
57
57
  return matchRelativePath.test(str);
58
- }
58
+ } // TODO simplify for the next major release
59
+
59
60
 
60
61
  function stringifyRequest(loaderContext, request) {
62
+ if (typeof loaderContext.utils !== "undefined" && typeof loaderContext.utils.contextify === "function") {
63
+ return JSON.stringify(loaderContext.utils.contextify(loaderContext.context, request));
64
+ }
65
+
61
66
  const splitted = request.split("!");
62
67
  const {
63
68
  context
@@ -844,6 +849,13 @@ function normalizeSourceMapForRuntime(map, loaderContext) {
844
849
 
845
850
  if (resultMap) {
846
851
  delete resultMap.file;
852
+ /* eslint-disable no-underscore-dangle */
853
+
854
+ if (loaderContext._compilation && loaderContext._compilation.options && loaderContext._compilation.options.devtool && loaderContext._compilation.options.devtool.includes("nosources")) {
855
+ /* eslint-enable no-underscore-dangle */
856
+ delete resultMap.sourcesContent;
857
+ }
858
+
847
859
  resultMap.sourceRoot = "";
848
860
  resultMap.sources = resultMap.sources.map(source => {
849
861
  // Non-standard syntax from `postcss`
@@ -902,7 +914,13 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
902
914
  return "";
903
915
  }
904
916
 
905
- const sourceMapValue = options.sourceMap ? `,${normalizeSourceMapForRuntime(result.map, loaderContext)}` : "";
917
+ let sourceMapValue = "";
918
+
919
+ if (options.sourceMap) {
920
+ const sourceMap = result.map;
921
+ sourceMapValue = `,${normalizeSourceMapForRuntime(sourceMap, loaderContext)}`;
922
+ }
923
+
906
924
  let code = JSON.stringify(result.css);
907
925
  let beforeCode = `var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(${options.sourceMap ? "___CSS_LOADER_API_SOURCEMAP_IMPORT___" : "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___"});\n`;
908
926
 
@@ -1061,28 +1079,67 @@ async function resolveRequests(resolve, context, possibleRequests) {
1061
1079
  });
1062
1080
  }
1063
1081
 
1064
- function isUrlRequestable(url) {
1082
+ function isURLRequestable(url, options = {}) {
1065
1083
  // Protocol-relative URLs
1066
1084
  if (/^\/\//.test(url)) {
1067
- return false;
1085
+ return {
1086
+ requestable: false,
1087
+ needResolve: false
1088
+ };
1089
+ } // `#` URLs
1090
+
1091
+
1092
+ if (/^#/.test(url)) {
1093
+ return {
1094
+ requestable: false,
1095
+ needResolve: false
1096
+ };
1097
+ } // Data URI
1098
+
1099
+
1100
+ if (isDataUrl(url) && options.isSupportDataURL) {
1101
+ try {
1102
+ decodeURIComponent(url);
1103
+ } catch (ignoreError) {
1104
+ return {
1105
+ requestable: false,
1106
+ needResolve: false
1107
+ };
1108
+ }
1109
+
1110
+ return {
1111
+ requestable: true,
1112
+ needResolve: false
1113
+ };
1068
1114
  } // `file:` protocol
1069
1115
 
1070
1116
 
1071
1117
  if (/^file:/i.test(url)) {
1072
- return true;
1118
+ return {
1119
+ requestable: true,
1120
+ needResolve: true
1121
+ };
1073
1122
  } // Absolute URLs
1074
1123
 
1075
1124
 
1076
1125
  if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !NATIVE_WIN32_PATH.test(url)) {
1077
- return false;
1078
- } // `#` URLs
1079
-
1126
+ if (options.isSupportAbsoluteURL && /^https?:/i.test(url)) {
1127
+ return {
1128
+ requestable: true,
1129
+ needResolve: false
1130
+ };
1131
+ }
1080
1132
 
1081
- if (/^#/.test(url)) {
1082
- return false;
1133
+ return {
1134
+ requestable: false,
1135
+ needResolve: false
1136
+ };
1083
1137
  }
1084
1138
 
1085
- return true;
1139
+ return {
1140
+ requestable: true,
1141
+ needResolve: true
1142
+ };
1086
1143
  }
1087
1144
 
1088
1145
  function sort(a, b) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-loader",
3
- "version": "6.4.0",
3
+ "version": "6.5.0",
4
4
  "description": "css loader module for webpack",
5
5
  "license": "MIT",
6
6
  "repository": "webpack-contrib/css-loader",
@@ -63,7 +63,7 @@
63
63
  "del": "^6.0.0",
64
64
  "del-cli": "^4.0.1",
65
65
  "es-check": "^6.0.0",
66
- "eslint": "^7.30.0",
66
+ "eslint": "^8.1.0",
67
67
  "eslint-config-prettier": "^8.3.0",
68
68
  "eslint-plugin-import": "^2.23.4",
69
69
  "file-loader": "^6.2.0",