css-loader 6.2.0 → 6.5.1

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/README.md CHANGED
@@ -54,69 +54,19 @@ module.exports = {
54
54
 
55
55
  And run `webpack` via your preferred method.
56
56
 
57
- ### `toString`
58
-
59
- You can also use the css-loader results directly as a string, such as in Angular's component style.
60
-
61
- **webpack.config.js**
62
-
63
- ```js
64
- module.exports = {
65
- module: {
66
- rules: [
67
- {
68
- test: /\.css$/i,
69
- use: ["to-string-loader", "css-loader"],
70
- },
71
- ],
72
- },
73
- };
74
- ```
75
-
76
- or
77
-
78
- ```js
79
- const css = require("./test.css").toString();
80
-
81
- console.log(css); // {String}
82
- ```
83
-
84
- If there are SourceMaps, they will also be included in the result string.
85
-
86
- If, for one reason or another, you need to extract CSS as a
87
- plain string resource (i.e. not wrapped in a JS module) you
88
- might want to check out the [extract-loader](https://github.com/peerigon/extract-loader).
89
- It's useful when you, for instance, need to post process the CSS as a string.
90
-
91
- **webpack.config.js**
92
-
93
- ```js
94
- module.exports = {
95
- module: {
96
- rules: [
97
- {
98
- test: /\.css$/i,
99
- use: [
100
- "handlebars-loader", // handlebars loader expects raw resource string
101
- "extract-loader",
102
- "css-loader",
103
- ],
104
- },
105
- ],
106
- },
107
- };
108
- ```
57
+ If, for one reason or another, you need to extract CSS as a file (i.e. do not store CSS in a JS module) you might want to check out the [recommend example](https://github.com/webpack-contrib/css-loader#recommend).
109
58
 
110
59
  ## Options
111
60
 
112
- | Name | Type | Default | Description |
113
- | :-----------------------------------: | :-------------------------: | :----------------: | :----------------------------------------------------------------------------------------------------------------------- |
114
- | **[`url`](#url)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `url()`/`image-set()` functions handling |
115
- | **[`import`](#import)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `@import` at-rules handling |
116
- | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Allows to enables/disables or setup CSS Modules options |
117
- | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
118
- | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Allows enables/disables or setups number of loaders applied before CSS loader for `@import`/CSS Modules and ICSS imports |
119
- | **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
61
+ | Name | Type | Default | Description |
62
+ | :-----------------------------------: | :------------------------------------------: | :----------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
63
+ | **[`url`](#url)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `url()`/`image-set()` functions handling |
64
+ | **[`import`](#import)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `@import` at-rules handling |
65
+ | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Allows to enables/disables or setup CSS Modules options |
66
+ | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
67
+ | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Allows enables/disables or setups number of loaders applied before CSS loader for `@import`/CSS Modules and ICSS imports |
68
+ | **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
69
+ | **[`exportType`](#exporttype)** | `{'array' \| 'string' \| 'css-style-sheet'}` | `array` | Allows exporting styles as array with modules, string or [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)) |
120
70
 
121
71
  ### `url`
122
72
 
@@ -745,13 +695,14 @@ For more information on options see:
745
695
 
746
696
  Supported template strings:
747
697
 
748
- - [name] the basename of the resource
749
- - [path] the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
750
- - [file] - filename and path.
751
- - [ext] - extension with leading .
752
- - [hash] - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
753
- - [<hashFunction>:hash:<hashDigest>:<hashDigestLength>] - hash with hash settings.
754
- - [local] - original class.
698
+ - `[name]` the basename of the resource
699
+ - `[folder]` the folder the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
700
+ - `[path]` the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
701
+ - `[file]` - filename and path.
702
+ - `[ext]` - extension with leading `.`.
703
+ - `[hash]` - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
704
+ - `[<hashFunction>:hash:<hashDigest>:<hashDigestLength>]` - hash with hash settings.
705
+ - `[local]` - original class.
755
706
 
756
707
  Recommendations:
757
708
 
@@ -1290,6 +1241,203 @@ module.exports = {
1290
1241
  };
1291
1242
  ```
1292
1243
 
1244
+ ### `exportType`
1245
+
1246
+ Type: `'array' | 'string' | 'css-style-sheet'`
1247
+ Default: `'array'`
1248
+
1249
+ Allows exporting styles as array with modules, string or [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).
1250
+ Default value is `'array'`, i.e. loader exports array of modules with specific API which is used in `style-loader` or other.
1251
+
1252
+ **webpack.config.js**
1253
+
1254
+ ```js
1255
+ module.exports = {
1256
+ module: {
1257
+ rules: [
1258
+ {
1259
+ assert: { type: "css" },
1260
+ loader: "css-loader",
1261
+ options: {
1262
+ exportType: "css-style-sheet",
1263
+ },
1264
+ },
1265
+ ],
1266
+ },
1267
+ };
1268
+ ```
1269
+
1270
+ **src/index.js**
1271
+
1272
+ ```js
1273
+ import sheet from "./styles.css" assert { type: "css" };
1274
+
1275
+ document.adoptedStyleSheets = [sheet];
1276
+ shadowRoot.adoptedStyleSheets = [sheet];
1277
+ ```
1278
+
1279
+ #### `'array'`
1280
+
1281
+ The default export is array of modules with specific API which is used in `style-loader` or other.
1282
+
1283
+ **webpack.config.js**
1284
+
1285
+ ```js
1286
+ module.exports = {
1287
+ module: {
1288
+ rules: [
1289
+ {
1290
+ test: /\.(sa|sc|c)ss$/i,
1291
+ use: ["style-loader", "css-loader", "postcss-loader", "sass-loader"],
1292
+ },
1293
+ ],
1294
+ },
1295
+ };
1296
+ ```
1297
+
1298
+ **src/index.js**
1299
+
1300
+ ```js
1301
+ // `style-loader` applies styles to DOM
1302
+ import "./styles.css";
1303
+ ```
1304
+
1305
+ #### `'string'`
1306
+
1307
+ > ⚠ You don't need [`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
1308
+ > ⚠ The `esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
1309
+
1310
+ The default export is `string`.
1311
+
1312
+ **webpack.config.js**
1313
+
1314
+ ```js
1315
+ module.exports = {
1316
+ module: {
1317
+ rules: [
1318
+ {
1319
+ test: /\.(sa|sc|c)ss$/i,
1320
+ use: ["css-loader", "postcss-loader", "sass-loader"],
1321
+ },
1322
+ ],
1323
+ },
1324
+ };
1325
+ ```
1326
+
1327
+ **src/index.js**
1328
+
1329
+ ```js
1330
+ import sheet from "./styles.css";
1331
+
1332
+ console.log(sheet);
1333
+ ```
1334
+
1335
+ #### `'css-style-sheet'`
1336
+
1337
+ > ⚠ `@import` rules not yet allowed, more [information](https://web.dev/css-module-scripts/#@import-rules-not-yet-allowed)
1338
+ > ⚠ You don't need [`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
1339
+ > ⚠ The `esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
1340
+ > ⚠ Source maps are not currently supported in `Chrome` due [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1174094&q=CSSStyleSheet%20source%20maps&can=2)
1341
+
1342
+ The default export is a [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).
1343
+
1344
+ Useful for [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) and shadow DOM.
1345
+
1346
+ More information:
1347
+
1348
+ - [Using CSS Module Scripts to import stylesheets](https://web.dev/css-module-scripts/)
1349
+ - [Constructable Stylesheets: seamless reusable styles](https://developers.google.com/web/updates/2019/02/constructable-stylesheets)
1350
+
1351
+ **webpack.config.js**
1352
+
1353
+ ```js
1354
+ module.exports = {
1355
+ module: {
1356
+ rules: [
1357
+ {
1358
+ assert: { type: "css" },
1359
+ loader: "css-loader",
1360
+ options: {
1361
+ exportType: "css-style-sheet",
1362
+ },
1363
+ },
1364
+
1365
+ // For Sass/SCSS:
1366
+ //
1367
+ // {
1368
+ // assert: { type: "css" },
1369
+ // rules: [
1370
+ // {
1371
+ // loader: "css-loader",
1372
+ // options: {
1373
+ // exportType: "css-style-sheet",
1374
+ // // Other options
1375
+ // },
1376
+ // },
1377
+ // {
1378
+ // loader: "sass-loader",
1379
+ // options: {
1380
+ // // Other options
1381
+ // },
1382
+ // },
1383
+ // ],
1384
+ // },
1385
+ ],
1386
+ },
1387
+ };
1388
+ ```
1389
+
1390
+ **src/index.js**
1391
+
1392
+ ```js
1393
+ // Example for Sass/SCSS:
1394
+ // import sheet from "./styles.scss" assert { type: "css" };
1395
+
1396
+ // Example for CSS modules:
1397
+ // import sheet, { myClass } from "./styles.scss" assert { type: "css" };
1398
+
1399
+ // Example for CSS:
1400
+ import sheet from "./styles.css" assert { type: "css" };
1401
+
1402
+ document.adoptedStyleSheets = [sheet];
1403
+ shadowRoot.adoptedStyleSheets = [sheet];
1404
+ ```
1405
+
1406
+ For migration purposes, you can use the following configuration:
1407
+
1408
+ ```js
1409
+ module.exports = {
1410
+ module: {
1411
+ rules: [
1412
+ {
1413
+ test: /\.css$/i,
1414
+ oneOf: [
1415
+ {
1416
+ assert: { type: "css" },
1417
+ loader: "css-loader",
1418
+ options: {
1419
+ exportType: "css-style-sheet",
1420
+ // Other options
1421
+ },
1422
+ },
1423
+ {
1424
+ use: [
1425
+ "style-loader",
1426
+ {
1427
+ loader: "css-loader",
1428
+ options: {
1429
+ // Other options
1430
+ },
1431
+ },
1432
+ ],
1433
+ },
1434
+ ],
1435
+ },
1436
+ ],
1437
+ },
1438
+ };
1439
+ ```
1440
+
1293
1441
  ## Examples
1294
1442
 
1295
1443
  ### Recommend
@@ -1310,7 +1458,7 @@ module.exports = {
1310
1458
  module: {
1311
1459
  rules: [
1312
1460
  {
1313
- test: /\.(sa|sc|c)ss$/,
1461
+ test: /\.(sa|sc|c)ss$/i,
1314
1462
  use: [
1315
1463
  devMode ? "style-loader" : MiniCssExtractPlugin.loader,
1316
1464
  "css-loader",
@@ -1533,8 +1681,8 @@ module.exports = {
1533
1681
  // --------
1534
1682
  // SCSS ALL EXCEPT MODULES
1535
1683
  {
1536
- test: /\.scss$/,
1537
- exclude: /\.module\.scss$/,
1684
+ test: /\.scss$/i,
1685
+ exclude: /\.module\.scss$/i,
1538
1686
  use: [
1539
1687
  {
1540
1688
  loader: "style-loader",
@@ -1556,7 +1704,7 @@ module.exports = {
1556
1704
  // --------
1557
1705
  // SCSS MODULES
1558
1706
  {
1559
- test: /\.module\.scss$/,
1707
+ test: /\.module\.scss$/i,
1560
1708
  use: [
1561
1709
  {
1562
1710
  loader: "style-loader",
package/dist/index.js CHANGED
@@ -49,24 +49,23 @@ 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
- const resolver = this.getResolve({
55
- dependencyType: "css",
56
- conditionNames: ["style"],
57
- mainFields: ["css", "style", "main", "..."],
58
- mainFiles: ["index", "..."],
59
- extensions: [".css", "..."],
60
- preferRelative: true
61
- });
62
61
  plugins.push((0, _plugins.importParser)({
62
+ isSupportAbsoluteURL: false,
63
+ isSupportDataURL: false,
64
+ isCSSStyleSheet: options.exportType === "css-style-sheet",
65
+ loaderContext: this,
63
66
  imports: importPluginImports,
64
67
  api: importPluginApi,
65
- context: this.context,
66
- rootContext: this.rootContext,
67
- resourcePath: this.resourcePath,
68
- filter: (0, _utils.getFilter)(options.import.filter, this.resourcePath),
69
- resolver,
68
+ filter: options.import.filter,
70
69
  urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
71
70
  }));
72
71
  }
@@ -75,22 +74,21 @@ async function loader(content, map, meta) {
75
74
 
76
75
  if ((0, _utils.shouldUseURLPlugin)(options)) {
77
76
  const needToResolveURL = !options.esModule;
78
- const isSupportDataURLInNewURL = options.esModule && Boolean("fsStartTime" in this._compiler);
79
77
  plugins.push((0, _plugins.urlParser)({
78
+ isSupportAbsoluteURL,
79
+ isSupportDataURL,
80
80
  imports: urlPluginImports,
81
81
  replacements,
82
82
  context: this.context,
83
83
  rootContext: this.rootContext,
84
84
  filter: (0, _utils.getFilter)(options.url.filter, this.resourcePath),
85
- needToResolveURL,
86
85
  resolver: needToResolveURL ? this.getResolve({
87
86
  mainFiles: [],
88
87
  extensions: []
89
88
  }) : // eslint-disable-next-line no-undefined
90
89
  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
90
+ urlHandler: url => (0, _utils.stringifyRequest)(this, url) // Support data urls as input in new URL added in webpack@5.38.0
91
+
94
92
  }));
95
93
  }
96
94
 
@@ -99,22 +97,12 @@ async function loader(content, map, meta) {
99
97
  const needToUseIcssPlugin = (0, _utils.shouldUseIcssPlugin)(options);
100
98
 
101
99
  if (needToUseIcssPlugin) {
102
- const icssResolver = this.getResolve({
103
- dependencyType: "icss",
104
- conditionNames: ["style"],
105
- extensions: ["..."],
106
- mainFields: ["css", "style", "main", "..."],
107
- mainFiles: ["index", "..."],
108
- preferRelative: true
109
- });
110
100
  plugins.push((0, _plugins.icssParser)({
101
+ loaderContext: this,
111
102
  imports: icssPluginImports,
112
103
  api: icssPluginApi,
113
104
  replacements,
114
105
  exports,
115
- context: this.context,
116
- rootContext: this.rootContext,
117
- resolver: icssResolver,
118
106
  urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
119
107
  }));
120
108
  } // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
@@ -172,9 +160,13 @@ async function loader(content, map, meta) {
172
160
 
173
161
  if (options.sourceMap) {
174
162
  imports.unshift({
175
- type: "api_sourcemap_import",
176
163
  importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
177
- url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/cssWithMappingToString"))
164
+ url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/sourceMaps"))
165
+ });
166
+ } else {
167
+ imports.unshift({
168
+ importName: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___",
169
+ url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/noSourceMaps"))
178
170
  });
179
171
  }
180
172
  }
package/dist/options.json CHANGED
@@ -193,6 +193,11 @@
193
193
  "description": "Use the ES modules syntax.",
194
194
  "link": "https://github.com/webpack-contrib/css-loader#esmodule",
195
195
  "type": "boolean"
196
+ },
197
+ "exportType": {
198
+ "description": "Allows exporting styles as array with modules, string or constructable stylesheet (i.e. `CSSStyleSheet`).",
199
+ "link": "https://github.com/webpack-contrib/css-loader#exporttype",
200
+ "enum": ["array", "string", "css-style-sheet"]
196
201
  }
197
202
  },
198
203
  "type": "object"
@@ -3,16 +3,16 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "importParser", {
6
+ Object.defineProperty(exports, "icssParser", {
7
7
  enumerable: true,
8
8
  get: function () {
9
- return _postcssImportParser.default;
9
+ return _postcssIcssParser.default;
10
10
  }
11
11
  });
12
- Object.defineProperty(exports, "icssParser", {
12
+ Object.defineProperty(exports, "importParser", {
13
13
  enumerable: true,
14
14
  get: function () {
15
- return _postcssIcssParser.default;
15
+ return _postcssImportParser.default;
16
16
  }
17
17
  });
18
18
  Object.defineProperty(exports, "urlParser", {
@@ -20,7 +20,18 @@ const plugin = (options = {}) => {
20
20
  icssExports
21
21
  } = (0, _icssUtils.extractICSS)(root);
22
22
  const imports = new Map();
23
- const tasks = []; // eslint-disable-next-line guard-for-in
23
+ const tasks = [];
24
+ const {
25
+ loaderContext
26
+ } = options;
27
+ const resolver = loaderContext.getResolve({
28
+ dependencyType: "icss",
29
+ conditionNames: ["style"],
30
+ extensions: ["..."],
31
+ mainFields: ["css", "style", "main", "..."],
32
+ mainFiles: ["index", "..."],
33
+ preferRelative: true
34
+ }); // eslint-disable-next-line guard-for-in
24
35
 
25
36
  for (const url in icssImports) {
26
37
  const tokens = icssImports[url];
@@ -39,14 +50,10 @@ const plugin = (options = {}) => {
39
50
  prefix = queryParts.join("!");
40
51
  }
41
52
 
42
- const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), options.rootContext);
53
+ const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), loaderContext.rootContext);
43
54
 
44
55
  const doResolve = async () => {
45
- const {
46
- resolver,
47
- context
48
- } = options;
49
- const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([normalizedUrl, request])]);
56
+ const resolvedUrl = await (0, _utils.resolveRequests)(resolver, loaderContext.context, [...new Set([normalizedUrl, request])]);
50
57
 
51
58
  if (!resolvedUrl) {
52
59
  return;