css-loader 6.1.0 → 6.2.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/README.md CHANGED
@@ -1031,6 +1031,9 @@ module.exports = {
1031
1031
  };
1032
1032
  ```
1033
1033
 
1034
+ To set a custom name for namedExport, can use [`exportLocalsConvention`](#exportLocalsConvention) option as a function.
1035
+ Example below in the [`examples`](#examples) section.
1036
+
1034
1037
  ##### `exportGlobals`
1035
1038
 
1036
1039
  Type: `Boolean`
@@ -1060,11 +1063,13 @@ module.exports = {
1060
1063
 
1061
1064
  ##### `exportLocalsConvention`
1062
1065
 
1063
- Type: `String`
1066
+ Type: `String|Function`
1064
1067
  Default: based on the `modules.namedExport` option value, if `true` - `camelCaseOnly`, otherwise `asIs`
1065
1068
 
1066
1069
  Style of exported class names.
1067
1070
 
1071
+ ###### `String`
1072
+
1068
1073
  By default, the exported JSON keys mirror the class names (i.e `asIs` value).
1069
1074
 
1070
1075
  > ⚠ Only `camelCaseOnly` value allowed if you set the `namedExport` value to `true`.
@@ -1110,6 +1115,58 @@ module.exports = {
1110
1115
  };
1111
1116
  ```
1112
1117
 
1118
+ ###### `Function`
1119
+
1120
+ **webpack.config.js**
1121
+
1122
+ ```js
1123
+ module.exports = {
1124
+ module: {
1125
+ rules: [
1126
+ {
1127
+ test: /\.css$/i,
1128
+ loader: "css-loader",
1129
+ options: {
1130
+ modules: {
1131
+ exportLocalsConvention: function (name) {
1132
+ return name.replace(/-/g, "_");
1133
+ },
1134
+ },
1135
+ },
1136
+ },
1137
+ ],
1138
+ },
1139
+ };
1140
+ ```
1141
+
1142
+ **webpack.config.js**
1143
+
1144
+ ```js
1145
+ module.exports = {
1146
+ module: {
1147
+ rules: [
1148
+ {
1149
+ test: /\.css$/i,
1150
+ loader: "css-loader",
1151
+ options: {
1152
+ modules: {
1153
+ exportLocalsConvention: function (name) {
1154
+ return [
1155
+ name.replace(/-/g, "_"),
1156
+ // dashesCamelCase
1157
+ name.replace(/-+(\w)/g, (match, firstLetter) =>
1158
+ firstLetter.toUpperCase()
1159
+ ),
1160
+ ];
1161
+ },
1162
+ },
1163
+ },
1164
+ },
1165
+ ],
1166
+ },
1167
+ };
1168
+ ```
1169
+
1113
1170
  ##### `exportOnlyLocals`
1114
1171
 
1115
1172
  Type: `Boolean`
@@ -1434,6 +1491,31 @@ module.exports = {
1434
1491
  };
1435
1492
  ```
1436
1493
 
1494
+ ### Named export with custom export names
1495
+
1496
+ **webpack.config.js**
1497
+
1498
+ ```js
1499
+ module.exports = {
1500
+ module: {
1501
+ rules: [
1502
+ {
1503
+ test: /\.css$/i,
1504
+ loader: "css-loader",
1505
+ options: {
1506
+ modules: {
1507
+ namedExport: true,
1508
+ exportLocalsConvention: function (name) {
1509
+ return name.replace(/-/g, "_");
1510
+ },
1511
+ },
1512
+ },
1513
+ },
1514
+ ],
1515
+ },
1516
+ };
1517
+ ```
1518
+
1437
1519
  ### Separating `Interoperable CSS`-only and `CSS Module` features
1438
1520
 
1439
1521
  The following setup is an example of allowing `Interoperable CSS` features only (such as `:import` and `:export`) without using further `CSS Module` functionality by setting `mode` option for all files that do not match `*.module.scss` naming convention. This is for reference as having `ICSS` features applied to all files was default `css-loader` behavior before v4.
package/dist/index.js CHANGED
@@ -180,7 +180,15 @@ async function loader(content, map, meta) {
180
180
  }
181
181
 
182
182
  const importCode = (0, _utils.getImportCode)(imports, options);
183
- const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
183
+ let moduleCode;
184
+
185
+ try {
186
+ moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
187
+ } catch (error) {
188
+ callback(error);
189
+ return;
190
+ }
191
+
184
192
  const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options);
185
193
  callback(null, `${importCode}${moduleCode}${exportCode}`);
186
194
  }
package/dist/options.json CHANGED
@@ -145,12 +145,19 @@
145
145
  "exportLocalsConvention": {
146
146
  "description": "Style of exported classnames.",
147
147
  "link": "https://github.com/webpack-contrib/css-loader#localsconvention",
148
- "enum": [
149
- "asIs",
150
- "camelCase",
151
- "camelCaseOnly",
152
- "dashes",
153
- "dashesOnly"
148
+ "anyOf": [
149
+ {
150
+ "enum": [
151
+ "asIs",
152
+ "camelCase",
153
+ "camelCaseOnly",
154
+ "dashes",
155
+ "dashesOnly"
156
+ ]
157
+ },
158
+ {
159
+ "instanceof": "Function"
160
+ }
154
161
  ]
155
162
  },
156
163
  "exportOnlyLocals": {
package/dist/utils.js CHANGED
@@ -462,6 +462,11 @@ function getFilter(filter, resourcePath) {
462
462
  }
463
463
 
464
464
  function getValidLocalName(localName, exportLocalsConvention) {
465
+ if (typeof exportLocalsConvention === "function") {
466
+ const result = exportLocalsConvention(localName);
467
+ return Array.isArray(result) ? result[0] : result;
468
+ }
469
+
465
470
  if (exportLocalsConvention === "dashesOnly") {
466
471
  return dashesCamelCase(localName);
467
472
  }
@@ -560,7 +565,7 @@ function getModulesOptions(rawOptions, loaderContext) {
560
565
  throw new Error('The "modules.namedExport" option requires the "esModules" option to be enabled');
561
566
  }
562
567
 
563
- if (modulesOptions.exportLocalsConvention !== "camelCaseOnly" && modulesOptions.exportLocalsConvention !== "dashesOnly") {
568
+ if (typeof modulesOptions.exportLocalsConvention === "string" && modulesOptions.exportLocalsConvention !== "camelCaseOnly" && modulesOptions.exportLocalsConvention !== "dashesOnly") {
564
569
  throw new Error('The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "camelCaseOnly" or "dashesOnly"');
565
570
  }
566
571
  }
@@ -861,15 +866,19 @@ function getExportCode(exports, replacements, needToUseIcssPlugin, options) {
861
866
 
862
867
  let localsCode = "";
863
868
 
864
- const addExportToLocalsCode = (name, value) => {
865
- if (options.modules.namedExport) {
866
- localsCode += `export var ${name} = ${JSON.stringify(value)};\n`;
867
- } else {
868
- if (localsCode) {
869
- localsCode += `,\n`;
870
- }
869
+ const addExportToLocalsCode = (names, value) => {
870
+ const normalizedNames = Array.isArray(names) ? new Set(names) : new Set([names]);
871
871
 
872
- localsCode += `\t${JSON.stringify(name)}: ${JSON.stringify(value)}`;
872
+ for (const name of normalizedNames) {
873
+ if (options.modules.namedExport) {
874
+ localsCode += `export var ${name} = ${JSON.stringify(value)};\n`;
875
+ } else {
876
+ if (localsCode) {
877
+ localsCode += `,\n`;
878
+ }
879
+
880
+ localsCode += `\t${JSON.stringify(name)}: ${JSON.stringify(value)}`;
881
+ }
873
882
  }
874
883
  };
875
884
 
@@ -877,16 +886,17 @@ function getExportCode(exports, replacements, needToUseIcssPlugin, options) {
877
886
  name,
878
887
  value
879
888
  } of exports) {
889
+ if (typeof options.modules.exportLocalsConvention === "function") {
890
+ addExportToLocalsCode(options.modules.exportLocalsConvention(name), value); // eslint-disable-next-line no-continue
891
+
892
+ continue;
893
+ }
894
+
880
895
  switch (options.modules.exportLocalsConvention) {
881
896
  case "camelCase":
882
897
  {
883
- addExportToLocalsCode(name, value);
884
898
  const modifiedName = camelCase(name);
885
-
886
- if (modifiedName !== name) {
887
- addExportToLocalsCode(modifiedName, value);
888
- }
889
-
899
+ addExportToLocalsCode([name, modifiedName], value);
890
900
  break;
891
901
  }
892
902
 
@@ -898,13 +908,8 @@ function getExportCode(exports, replacements, needToUseIcssPlugin, options) {
898
908
 
899
909
  case "dashes":
900
910
  {
901
- addExportToLocalsCode(name, value);
902
911
  const modifiedName = dashesCamelCase(name);
903
-
904
- if (modifiedName !== name) {
905
- addExportToLocalsCode(modifiedName, value);
906
- }
907
-
912
+ addExportToLocalsCode([name, modifiedName], value);
908
913
  break;
909
914
  }
910
915
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-loader",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "css loader module for webpack",
5
5
  "license": "MIT",
6
6
  "repository": "webpack-contrib/css-loader",