css-loader 5.2.4 → 6.0.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
@@ -5,14 +5,10 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = loader;
7
7
 
8
- var _loaderUtils = require("loader-utils");
9
-
10
8
  var _postcss = _interopRequireDefault(require("postcss"));
11
9
 
12
10
  var _package = _interopRequireDefault(require("postcss/package.json"));
13
11
 
14
- var _schemaUtils = require("schema-utils");
15
-
16
12
  var _semver = require("semver");
17
13
 
18
14
  var _CssSyntaxError = _interopRequireDefault(require("./CssSyntaxError"));
@@ -32,11 +28,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
32
28
  Author Tobias Koppers @sokra
33
29
  */
34
30
  async function loader(content, map, meta) {
35
- const rawOptions = (0, _loaderUtils.getOptions)(this);
36
- (0, _schemaUtils.validate)(_options.default, rawOptions, {
37
- name: "CSS Loader",
38
- baseDataPath: "options"
39
- });
31
+ const rawOptions = this.getOptions(_options.default);
40
32
  const plugins = [];
41
33
  const callback = this.async();
42
34
  let options;
@@ -60,51 +52,60 @@ async function loader(content, map, meta) {
60
52
 
61
53
  if ((0, _utils.shouldUseImportPlugin)(options)) {
62
54
  const resolver = this.getResolve({
55
+ dependencyType: "css",
63
56
  conditionNames: ["style"],
64
- extensions: [".css"],
65
57
  mainFields: ["css", "style", "main", "..."],
66
- mainFiles: ["index", "..."]
58
+ mainFiles: ["index", "..."],
59
+ extensions: [".css", "..."],
60
+ preferRelative: true
67
61
  });
68
62
  plugins.push((0, _plugins.importParser)({
69
63
  imports: importPluginImports,
70
64
  api: importPluginApi,
71
65
  context: this.context,
72
66
  rootContext: this.rootContext,
73
- filter: (0, _utils.getFilter)(options.import, this.resourcePath),
67
+ resourcePath: this.resourcePath,
68
+ filter: (0, _utils.getFilter)(options.import.filter, this.resourcePath),
74
69
  resolver,
75
- urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
70
+ urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
76
71
  }));
77
72
  }
78
73
 
79
74
  const urlPluginImports = [];
80
75
 
81
76
  if ((0, _utils.shouldUseURLPlugin)(options)) {
82
- const urlResolver = this.getResolve({
83
- conditionNames: ["asset"],
84
- mainFields: ["asset"],
85
- mainFiles: [],
86
- extensions: []
87
- });
77
+ const needToResolveURL = !options.esModule;
78
+ const isSupportDataURLInNewURL = options.esModule && Boolean("fsStartTime" in this._compiler);
88
79
  plugins.push((0, _plugins.urlParser)({
89
80
  imports: urlPluginImports,
90
81
  replacements,
91
82
  context: this.context,
92
83
  rootContext: this.rootContext,
93
- filter: (0, _utils.getFilter)(options.url, this.resourcePath),
94
- resolver: urlResolver,
95
- urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, url)
84
+ filter: (0, _utils.getFilter)(options.url.filter, this.resourcePath),
85
+ needToResolveURL,
86
+ resolver: needToResolveURL ? this.getResolve({
87
+ mainFiles: [],
88
+ extensions: []
89
+ }) : // eslint-disable-next-line no-undefined
90
+ undefined,
91
+ urlHandler: url => (0, _utils.stringifyRequest)(this, url),
92
+ // Support data urls as input in new URL added in webpack@5.38.0
93
+ isSupportDataURLInNewURL
96
94
  }));
97
95
  }
98
96
 
99
97
  const icssPluginImports = [];
100
98
  const icssPluginApi = [];
99
+ const needToUseIcssPlugin = (0, _utils.shouldUseIcssPlugin)(options);
101
100
 
102
- if ((0, _utils.shouldUseIcssPlugin)(options)) {
101
+ if (needToUseIcssPlugin) {
103
102
  const icssResolver = this.getResolve({
103
+ dependencyType: "icss",
104
104
  conditionNames: ["style"],
105
- extensions: [],
105
+ extensions: ["..."],
106
106
  mainFields: ["css", "style", "main", "..."],
107
- mainFiles: ["index", "..."]
107
+ mainFiles: ["index", "..."],
108
+ preferRelative: true
108
109
  });
109
110
  plugins.push((0, _plugins.icssParser)({
110
111
  imports: icssPluginImports,
@@ -114,7 +115,7 @@ async function loader(content, map, meta) {
114
115
  context: this.context,
115
116
  rootContext: this.rootContext,
116
117
  resolver: icssResolver,
117
- urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
118
+ urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
118
119
  }));
119
120
  } // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
120
121
 
@@ -164,20 +165,22 @@ async function loader(content, map, meta) {
164
165
 
165
166
  if (options.modules.exportOnlyLocals !== true) {
166
167
  imports.unshift({
168
+ type: "api_import",
167
169
  importName: "___CSS_LOADER_API_IMPORT___",
168
- url: (0, _loaderUtils.stringifyRequest)(this, require.resolve("./runtime/api"))
170
+ url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/api"))
169
171
  });
170
172
 
171
173
  if (options.sourceMap) {
172
174
  imports.unshift({
175
+ type: "api_sourcemap_import",
173
176
  importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
174
- url: (0, _loaderUtils.stringifyRequest)(this, require.resolve("./runtime/cssWithMappingToString"))
177
+ url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/cssWithMappingToString"))
175
178
  });
176
179
  }
177
180
  }
178
181
 
179
182
  const importCode = (0, _utils.getImportCode)(imports, options);
180
183
  const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
181
- const exportCode = (0, _utils.getExportCode)(exports, replacements, options);
184
+ const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options);
182
185
  callback(null, `${importCode}${moduleCode}${exportCode}`);
183
186
  }
package/dist/options.json CHANGED
@@ -1,45 +1,54 @@
1
1
  {
2
+ "title": "CSS Loader options",
2
3
  "additionalProperties": false,
3
4
  "properties": {
4
5
  "url": {
5
- "description": "Enables/Disables 'url'/'image-set' functions handling (https://github.com/webpack-contrib/css-loader#url).",
6
+ "description": "Allows to enables/disables `url()`/`image-set()` functions handling (https://github.com/webpack-contrib/css-loader#url).",
6
7
  "anyOf": [
7
8
  {
8
9
  "type": "boolean"
9
10
  },
10
11
  {
11
- "instanceof": "Function"
12
+ "type": "object",
13
+ "properties": {
14
+ "filter": {
15
+ "instanceof": "Function"
16
+ }
17
+ },
18
+ "additionalProperties": false
12
19
  }
13
20
  ]
14
21
  },
15
22
  "import": {
16
- "description": "Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).",
23
+ "description": "Allows to enables/disables `@import` at-rules handling (https://github.com/webpack-contrib/css-loader#import).",
17
24
  "anyOf": [
18
25
  {
19
26
  "type": "boolean"
20
27
  },
21
28
  {
22
- "instanceof": "Function"
29
+ "type": "object",
30
+ "properties": {
31
+ "filter": {
32
+ "instanceof": "Function"
33
+ }
34
+ },
35
+ "additionalProperties": false
23
36
  }
24
37
  ]
25
38
  },
26
39
  "modules": {
27
- "description": "Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).",
40
+ "description": "Allows to enable/disable CSS Modules or ICSS and setup configuration (https://github.com/webpack-contrib/css-loader#modules).",
28
41
  "anyOf": [
29
42
  {
30
43
  "type": "boolean"
31
44
  },
32
45
  {
33
- "enum": ["local", "global", "pure"]
46
+ "enum": ["local", "global", "pure", "icss"]
34
47
  },
35
48
  {
36
49
  "type": "object",
37
50
  "additionalProperties": false,
38
51
  "properties": {
39
- "compileType": {
40
- "description": "Controls the extent to which css-loader will process module code (https://github.com/webpack-contrib/css-loader#type)",
41
- "enum": ["module", "icss"]
42
- },
43
52
  "auto": {
44
53
  "description": "Allows auto enable CSS modules based on filename (https://github.com/webpack-contrib/css-loader#auto).",
45
54
  "anyOf": [
@@ -58,7 +67,7 @@
58
67
  "description": "Setup `mode` option (https://github.com/webpack-contrib/css-loader#mode).",
59
68
  "anyOf": [
60
69
  {
61
- "enum": ["local", "global", "pure"]
70
+ "enum": ["local", "global", "pure", "icss"]
62
71
  },
63
72
  {
64
73
  "instanceof": "Function"
@@ -75,11 +84,25 @@
75
84
  "type": "string",
76
85
  "minLength": 1
77
86
  },
78
- "localIdentHashPrefix": {
79
- "description": "Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashprefix).",
87
+ "localIdentHashSalt": {
88
+ "description": "Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashsalt).",
89
+ "type": "string",
90
+ "minLength": 1
91
+ },
92
+ "localIdentHashFunction": {
93
+ "description": "Allows to specify hash function to generate classes (https://github.com/webpack-contrib/css-loader#localidenthashfunction).",
80
94
  "type": "string",
81
95
  "minLength": 1
82
96
  },
97
+ "localIdentHashDigest": {
98
+ "description": "Allows to specify hash digest to generate classes (https://github.com/webpack-contrib/css-loader#localidenthashdigest).",
99
+ "type": "string",
100
+ "minLength": 1
101
+ },
102
+ "localIdentHashDigestLength": {
103
+ "description": "Allows to specify hash digest length to generate classes (https://github.com/webpack-contrib/css-loader#localidenthashdigestlength).",
104
+ "type": "number"
105
+ },
83
106
  "localIdentRegExp": {
84
107
  "description": "Allows to specify custom RegExp for local ident name (https://github.com/webpack-contrib/css-loader#localidentregexp).",
85
108
  "anyOf": [
@@ -123,11 +146,11 @@
123
146
  ]
124
147
  },
125
148
  "sourceMap": {
126
- "description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
149
+ "description": "Allows to enable/disable source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
127
150
  "type": "boolean"
128
151
  },
129
152
  "importLoaders": {
130
- "description": "Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).",
153
+ "description": "Allows enables/disables or setups number of loaders applied before CSS loader for `@import`/CSS Modules and ICSS imports (https://github.com/webpack-contrib/css-loader#importloaders).",
131
154
  "anyOf": [
132
155
  {
133
156
  "type": "boolean"
@@ -81,6 +81,7 @@ const plugin = (options = {}) => {
81
81
  importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
82
82
  imports.set(importKey, importName);
83
83
  options.imports.push({
84
+ type: "icss_import",
84
85
  importName,
85
86
  url: options.urlHandler(newUrl),
86
87
  icss: true,
@@ -171,6 +171,11 @@ const plugin = (options = {}) => {
171
171
  return;
172
172
  }
173
173
 
174
+ if (resolvedUrl === options.resourcePath) {
175
+ atRule.remove();
176
+ return;
177
+ }
178
+
174
179
  atRule.remove(); // eslint-disable-next-line consistent-return
175
180
 
176
181
  return {
@@ -226,6 +231,7 @@ const plugin = (options = {}) => {
226
231
  importName = `___CSS_LOADER_AT_RULE_IMPORT_${urlToNameMap.size}___`;
227
232
  urlToNameMap.set(newUrl, importName);
228
233
  options.imports.push({
234
+ type: "rule_import",
229
235
  importName,
230
236
  url: options.urlHandler(newUrl),
231
237
  index
@@ -49,7 +49,7 @@ function getWebpackIgnoreCommentValue(index, nodes, inBetween) {
49
49
  return matched && matched[2] === "true";
50
50
  }
51
51
 
52
- function shouldHandleURL(url, declaration, result) {
52
+ function shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL) {
53
53
  if (url.length === 0) {
54
54
  result.warn(`Unable to find uri in '${declaration.toString()}'`, {
55
55
  node: declaration
@@ -57,6 +57,16 @@ function shouldHandleURL(url, declaration, result) {
57
57
  return false;
58
58
  }
59
59
 
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
+
60
70
  if (!(0, _utils.isUrlRequestable)(url)) {
61
71
  return false;
62
72
  }
@@ -64,7 +74,7 @@ function shouldHandleURL(url, declaration, result) {
64
74
  return true;
65
75
  }
66
76
 
67
- function parseDeclaration(declaration, key, result) {
77
+ function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
68
78
  if (!needParseDeclaration.test(declaration[key])) {
69
79
  return;
70
80
  }
@@ -118,7 +128,7 @@ function parseDeclaration(declaration, key, result) {
118
128
  let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
119
129
  url = (0, _utils.normalizeUrl)(url, isStringValue); // Do not traverse inside `url`
120
130
 
121
- if (!shouldHandleURL(url, declaration, result)) {
131
+ if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
122
132
  // eslint-disable-next-line consistent-return
123
133
  return false;
124
134
  }
@@ -168,7 +178,7 @@ function parseDeclaration(declaration, key, result) {
168
178
  let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
169
179
  url = (0, _utils.normalizeUrl)(url, isStringValue); // Do not traverse inside `url`
170
180
 
171
- if (!shouldHandleURL(url, declaration, result)) {
181
+ if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
172
182
  // eslint-disable-next-line consistent-return
173
183
  return false;
174
184
  }
@@ -204,7 +214,7 @@ function parseDeclaration(declaration, key, result) {
204
214
 
205
215
  let url = (0, _utils.normalizeUrl)(value, true); // Do not traverse inside `url`
206
216
 
207
- if (!shouldHandleURL(url, declaration, result)) {
217
+ if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
208
218
  // eslint-disable-next-line consistent-return
209
219
  return false;
210
220
  }
@@ -245,7 +255,10 @@ const plugin = (options = {}) => {
245
255
  const parsedDeclarations = [];
246
256
  return {
247
257
  Declaration(declaration) {
248
- const parsedURL = parseDeclaration(declaration, "value", result);
258
+ const {
259
+ isSupportDataURLInNewURL
260
+ } = options;
261
+ const parsedURL = parseDeclaration(declaration, "value", result, isSupportDataURLInNewURL);
249
262
 
250
263
  if (!parsedURL) {
251
264
  return;
@@ -268,15 +281,34 @@ const plugin = (options = {}) => {
268
281
  const needKeep = await options.filter(url);
269
282
 
270
283
  if (!needKeep) {
284
+ // eslint-disable-next-line consistent-return
271
285
  return;
272
286
  }
273
287
  }
274
288
 
289
+ if ((0, _utils.isDataUrl)(url)) {
290
+ // eslint-disable-next-line consistent-return
291
+ return parsedDeclaration;
292
+ }
293
+
275
294
  const splittedUrl = url.split(/(\?)?#/);
276
295
  const [pathname, query, hashOrQuery] = splittedUrl;
277
296
  let hash = query ? "?" : "";
278
297
  hash += hashOrQuery ? `#${hashOrQuery}` : "";
279
- const request = (0, _utils.requestify)(pathname, options.rootContext);
298
+ const {
299
+ needToResolveURL,
300
+ rootContext
301
+ } = options;
302
+ const request = (0, _utils.requestify)(pathname, rootContext, needToResolveURL);
303
+
304
+ if (!needToResolveURL) {
305
+ // eslint-disable-next-line consistent-return
306
+ return { ...parsedDeclaration,
307
+ url: request,
308
+ hash
309
+ };
310
+ }
311
+
280
312
  const {
281
313
  resolver,
282
314
  context
@@ -284,6 +316,7 @@ const plugin = (options = {}) => {
284
316
  const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, url])]);
285
317
 
286
318
  if (!resolvedUrl) {
319
+ // eslint-disable-next-line consistent-return
287
320
  return;
288
321
  } // eslint-disable-next-line consistent-return
289
322
 
@@ -307,6 +340,7 @@ const plugin = (options = {}) => {
307
340
 
308
341
  if (!hasUrlImportHelper) {
309
342
  options.imports.push({
343
+ type: "get_url_import",
310
344
  importName: "___CSS_LOADER_GET_URL_IMPORT___",
311
345
  url: options.urlHandler(require.resolve("../runtime/getUrl.js")),
312
346
  index: -1
@@ -325,8 +359,9 @@ const plugin = (options = {}) => {
325
359
  importName = `___CSS_LOADER_URL_IMPORT_${urlToNameMap.size}___`;
326
360
  urlToNameMap.set(newUrl, importName);
327
361
  options.imports.push({
362
+ type: "url",
328
363
  importName,
329
- url: options.urlHandler(newUrl),
364
+ url: options.needToResolveURL ? options.urlHandler(newUrl) : JSON.stringify(newUrl),
330
365
  index
331
366
  });
332
367
  }
@@ -8,7 +8,7 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
8
8
 
9
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
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; }
11
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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
12
 
13
13
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
14
14
 
@@ -17,6 +17,10 @@ module.exports = function cssWithMappingToString(item) {
17
17
  content = _item[1],
18
18
  cssMapping = _item[3];
19
19
 
20
+ if (!cssMapping) {
21
+ return content;
22
+ }
23
+
20
24
  if (typeof btoa === "function") {
21
25
  // eslint-disable-next-line no-undef
22
26
  var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(cssMapping))));
@@ -4,15 +4,14 @@ module.exports = function (url, options) {
4
4
  if (!options) {
5
5
  // eslint-disable-next-line no-param-reassign
6
6
  options = {};
7
- } // eslint-disable-next-line no-underscore-dangle, no-param-reassign
8
-
9
-
10
- url = url && url.__esModule ? url.default : url;
7
+ }
11
8
 
12
- if (typeof url !== "string") {
9
+ if (!url) {
13
10
  return url;
14
- } // If url is already wrapped in quotes, remove them
11
+ } // eslint-disable-next-line no-underscore-dangle, no-param-reassign
12
+
15
13
 
14
+ url = String(url.__esModule ? url.default : url); // If url is already wrapped in quotes, remove them
16
15
 
17
16
  if (/^['"].*['"]$/.test(url)) {
18
17
  // eslint-disable-next-line no-param-reassign
@@ -26,7 +25,7 @@ module.exports = function (url, options) {
26
25
  // See https://drafts.csswg.org/css-values-3/#urls
27
26
 
28
27
 
29
- if (/["'() \t\n]/.test(url) || options.needQuotes) {
28
+ if (/["'() \t\n]|(%20)/.test(url) || options.needQuotes) {
30
29
  return "\"".concat(url.replace(/"/g, '\\"').replace(/\n/g, "\\n"), "\"");
31
30
  }
32
31