css-loader 6.7.4 → 6.8.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
@@ -7,8 +7,6 @@ exports.default = loader;
7
7
  var _postcss = _interopRequireDefault(require("postcss"));
8
8
  var _package = _interopRequireDefault(require("postcss/package.json"));
9
9
  var _semver = require("semver");
10
- var _CssSyntaxError = _interopRequireDefault(require("./CssSyntaxError"));
11
- var _Warning = _interopRequireDefault(require("./Warning"));
12
10
  var _options = _interopRequireDefault(require("./options.json"));
13
11
  var _plugins = require("./plugins");
14
12
  var _utils = require("./utils");
@@ -125,11 +123,11 @@ async function loader(content, map, meta) {
125
123
  if (error.file) {
126
124
  this.addDependency(error.file);
127
125
  }
128
- callback(error.name === "CssSyntaxError" ? new _CssSyntaxError.default(error) : error);
126
+ callback(error.name === "CssSyntaxError" ? (0, _utils.syntaxErrorFactory)(error) : error);
129
127
  return;
130
128
  }
131
129
  for (const warning of result.warnings()) {
132
- this.emitWarning(new _Warning.default(warning));
130
+ this.emitWarning((0, _utils.warningFactory)(warning));
133
131
  }
134
132
  const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
135
133
  const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
@@ -151,14 +149,28 @@ async function loader(content, map, meta) {
151
149
  });
152
150
  }
153
151
  }
152
+ let isTemplateLiteralSupported = false;
153
+ if (
154
+ // eslint-disable-next-line no-underscore-dangle
155
+ this._compilation &&
156
+ // eslint-disable-next-line no-underscore-dangle
157
+ this._compilation.options &&
158
+ // eslint-disable-next-line no-underscore-dangle
159
+ this._compilation.options.output &&
160
+ // eslint-disable-next-line no-underscore-dangle
161
+ this._compilation.options.output.environment &&
162
+ // eslint-disable-next-line no-underscore-dangle
163
+ this._compilation.options.output.environment.templateLiteral) {
164
+ isTemplateLiteralSupported = true;
165
+ }
154
166
  const importCode = (0, _utils.getImportCode)(imports, options);
155
167
  let moduleCode;
156
168
  try {
157
- moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
169
+ moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, isTemplateLiteralSupported, this);
158
170
  } catch (error) {
159
171
  callback(error);
160
172
  return;
161
173
  }
162
- const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options);
174
+ const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options, isTemplateLiteralSupported);
163
175
  callback(null, `${importCode}${moduleCode}${exportCode}`);
164
176
  }
package/dist/utils.js CHANGED
@@ -27,6 +27,8 @@ exports.shouldUseModulesPlugins = shouldUseModulesPlugins;
27
27
  exports.shouldUseURLPlugin = shouldUseURLPlugin;
28
28
  exports.sort = sort;
29
29
  exports.stringifyRequest = stringifyRequest;
30
+ exports.syntaxErrorFactory = syntaxErrorFactory;
31
+ exports.warningFactory = warningFactory;
30
32
  var _url = require("url");
31
33
  var _path = _interopRequireDefault(require("path"));
32
34
  var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-values"));
@@ -790,7 +792,7 @@ function printParams(media, dedupe, supports, layer) {
790
792
  }
791
793
  return result;
792
794
  }
793
- function getModuleCode(result, api, replacements, options, loaderContext) {
795
+ function getModuleCode(result, api, replacements, options, isTemplateLiteralSupported, loaderContext) {
794
796
  if (options.modules.exportOnlyLocals === true) {
795
797
  return "";
796
798
  }
@@ -799,7 +801,7 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
799
801
  const sourceMap = result.map;
800
802
  sourceMapValue = `,${normalizeSourceMapForRuntime(sourceMap, loaderContext)}`;
801
803
  }
802
- let code = JSON.stringify(result.css);
804
+ let code = isTemplateLiteralSupported ? convertToTemplateLiteral(result.css) : JSON.stringify(result.css);
803
805
  let beforeCode = `var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(${options.sourceMap ? "___CSS_LOADER_API_SOURCEMAP_IMPORT___" : "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___"});\n`;
804
806
  for (const item of api) {
805
807
  const {
@@ -825,7 +827,7 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
825
827
  localName
826
828
  } = item;
827
829
  if (localName) {
828
- code = code.replace(new RegExp(replacementName, "g"), () => options.modules.namedExport ? `" + ${importName}_NAMED___[${JSON.stringify(getValidLocalName(localName, options.modules.exportLocalsConvention))}] + "` : `" + ${importName}.locals[${JSON.stringify(localName)}] + "`);
830
+ code = code.replace(new RegExp(replacementName, "g"), () => options.modules.namedExport ? isTemplateLiteralSupported ? `\${ ${importName}_NAMED___[${JSON.stringify(getValidLocalName(localName, options.modules.exportLocalsConvention))}] }` : `" + ${importName}_NAMED___[${JSON.stringify(getValidLocalName(localName, options.modules.exportLocalsConvention))}] + "` : isTemplateLiteralSupported ? `\${${importName}.locals[${JSON.stringify(localName)}]}` : `" + ${importName}.locals[${JSON.stringify(localName)}] + "`);
829
831
  } else {
830
832
  const {
831
833
  hash,
@@ -834,7 +836,7 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
834
836
  const getUrlOptions = [].concat(hash ? [`hash: ${JSON.stringify(hash)}`] : []).concat(needQuotes ? "needQuotes: true" : []);
835
837
  const preparedOptions = getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(", ")} }` : "";
836
838
  beforeCode += `var ${replacementName} = ___CSS_LOADER_GET_URL_IMPORT___(${importName}${preparedOptions});\n`;
837
- code = code.replace(new RegExp(replacementName, "g"), () => `" + ${replacementName} + "`);
839
+ code = code.replace(new RegExp(replacementName, "g"), () => isTemplateLiteralSupported ? `\${${replacementName}}` : `" + ${replacementName} + "`);
838
840
  }
839
841
  }
840
842
 
@@ -847,10 +849,21 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
847
849
  // 5 - layer
848
850
  return `${beforeCode}// Module\n___CSS_LOADER_EXPORT___.push([module.id, ${code}, ""${sourceMapValue}]);\n`;
849
851
  }
852
+ const SLASH = "\\".charCodeAt(0);
853
+ const BACKTICK = "`".charCodeAt(0);
854
+ const DOLLAR = "$".charCodeAt(0);
855
+ function convertToTemplateLiteral(str) {
856
+ let escapedString = "";
857
+ for (let i = 0; i < str.length; i++) {
858
+ const code = str.charCodeAt(i);
859
+ escapedString += code === SLASH || code === BACKTICK || code === DOLLAR ? `\\${str[i]}` : str[i];
860
+ }
861
+ return `\`${escapedString}\``;
862
+ }
850
863
  function dashesCamelCase(str) {
851
864
  return str.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase());
852
865
  }
853
- function getExportCode(exports, replacements, icssPluginUsed, options) {
866
+ function getExportCode(exports, replacements, icssPluginUsed, options, isTemplateLiteralSupported) {
854
867
  let code = "// Exports\n";
855
868
  if (icssPluginUsed) {
856
869
  let localsCode = "";
@@ -858,12 +871,12 @@ function getExportCode(exports, replacements, icssPluginUsed, options) {
858
871
  const normalizedNames = Array.isArray(names) ? new Set(names) : new Set([names]);
859
872
  for (const name of normalizedNames) {
860
873
  if (options.modules.namedExport) {
861
- localsCode += `export var ${name} = ${JSON.stringify(value)};\n`;
874
+ localsCode += `export var ${name} = ${isTemplateLiteralSupported ? convertToTemplateLiteral(value) : JSON.stringify(value)};\n`;
862
875
  } else {
863
876
  if (localsCode) {
864
877
  localsCode += `,\n`;
865
878
  }
866
- localsCode += `\t${JSON.stringify(name)}: ${JSON.stringify(value)}`;
879
+ localsCode += `\t${JSON.stringify(name)}: ${isTemplateLiteralSupported ? convertToTemplateLiteral(value) : JSON.stringify(value)}`;
867
880
  }
868
881
  }
869
882
  };
@@ -884,14 +897,14 @@ function getExportCode(exports, replacements, icssPluginUsed, options) {
884
897
  } = item;
885
898
  localsCode = localsCode.replace(new RegExp(replacementName, "g"), () => {
886
899
  if (options.modules.namedExport) {
887
- return `" + ${importName}_NAMED___[${JSON.stringify(getValidLocalName(localName, options.modules.exportLocalsConvention))}] + "`;
900
+ return isTemplateLiteralSupported ? `\${${importName}_NAMED___[${JSON.stringify(getValidLocalName(localName, options.modules.exportLocalsConvention))}]}` : `" + ${importName}_NAMED___[${JSON.stringify(getValidLocalName(localName, options.modules.exportLocalsConvention))}] + "`;
888
901
  } else if (options.modules.exportOnlyLocals) {
889
- return `" + ${importName}[${JSON.stringify(localName)}] + "`;
902
+ return isTemplateLiteralSupported ? `\${${importName}[${JSON.stringify(localName)}]}` : `" + ${importName}[${JSON.stringify(localName)}] + "`;
890
903
  }
891
- return `" + ${importName}.locals[${JSON.stringify(localName)}] + "`;
904
+ return isTemplateLiteralSupported ? `\${${importName}.locals[${JSON.stringify(localName)}]}` : `" + ${importName}.locals[${JSON.stringify(localName)}] + "`;
892
905
  });
893
906
  } else {
894
- localsCode = localsCode.replace(new RegExp(replacementName, "g"), () => `" + ${replacementName} + "`);
907
+ localsCode = localsCode.replace(new RegExp(replacementName, "g"), () => isTemplateLiteralSupported ? `\${${replacementName}}` : `" + ${replacementName} + "`);
895
908
  }
896
909
  }
897
910
  if (options.modules.exportOnlyLocals) {
@@ -995,4 +1008,38 @@ function sort(a, b) {
995
1008
  function combineRequests(preRequest, url) {
996
1009
  const idx = url.indexOf("!=!");
997
1010
  return idx !== -1 ? url.slice(0, idx + 3) + preRequest + url.slice(idx + 3) : preRequest + url;
1011
+ }
1012
+ function warningFactory(obj) {
1013
+ let message = "";
1014
+ if (typeof obj.line !== "undefined") {
1015
+ message += `(${obj.line}:${obj.column}) `;
1016
+ }
1017
+ if (typeof obj.plugin !== "undefined") {
1018
+ message += `from "${obj.plugin}" plugin: `;
1019
+ }
1020
+ message += obj.text;
1021
+ if (obj.node) {
1022
+ message += `\n\nCode:\n ${obj.node.toString()}\n`;
1023
+ }
1024
+ const warning = new Error(message);
1025
+ warning.stack = null;
1026
+ return warning;
1027
+ }
1028
+ function syntaxErrorFactory(obj) {
1029
+ let message = "\nSyntaxError\n\n";
1030
+ if (typeof obj.line !== "undefined") {
1031
+ message += `(${obj.line}:${obj.column}) `;
1032
+ }
1033
+ if (typeof obj.plugin !== "undefined") {
1034
+ message += `from "${obj.plugin}" plugin: `;
1035
+ }
1036
+ message += obj.file ? `${obj.file} ` : "<css input> ";
1037
+ message += `${obj.reason}`;
1038
+ const code = obj.showSourceCode();
1039
+ if (code) {
1040
+ message += `\n\n${code}\n`;
1041
+ }
1042
+ const error = new Error(message);
1043
+ error.stack = null;
1044
+ return error;
998
1045
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-loader",
3
- "version": "6.7.4",
3
+ "version": "6.8.0",
4
4
  "description": "css loader module for webpack",
5
5
  "license": "MIT",
6
6
  "repository": "webpack-contrib/css-loader",
@@ -49,7 +49,7 @@
49
49
  "icss-utils": "^5.1.0",
50
50
  "postcss": "^8.4.21",
51
51
  "postcss-modules-extract-imports": "^3.0.0",
52
- "postcss-modules-local-by-default": "^4.0.1",
52
+ "postcss-modules-local-by-default": "^4.0.3",
53
53
  "postcss-modules-scope": "^3.0.0",
54
54
  "postcss-modules-values": "^4.0.0",
55
55
  "postcss-value-parser": "^4.2.0",
@@ -89,7 +89,7 @@
89
89
  "standard-version": "^9.5.0",
90
90
  "strip-ansi": "^6.0.0",
91
91
  "style-loader": "^3.3.2",
92
- "stylus": "^0.56.0",
92
+ "stylus": "^0.59.0",
93
93
  "stylus-loader": "^6.1.0",
94
94
  "url-loader": "^4.1.1",
95
95
  "webpack": "^5.77.0"
@@ -1,35 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- class CssSyntaxError extends Error {
8
- constructor(error) {
9
- super(error);
10
- const {
11
- reason,
12
- line,
13
- column,
14
- file
15
- } = error;
16
- this.name = "CssSyntaxError";
17
-
18
- // Based on https://github.com/postcss/postcss/blob/master/lib/css-syntax-error.es6#L132
19
- // We don't need `plugin` and `file` properties.
20
- this.message = `${this.name}\n\n`;
21
- if (typeof line !== "undefined") {
22
- this.message += `(${line}:${column}) `;
23
- }
24
- this.message += file ? `${file} ` : "<css input> ";
25
- this.message += `${reason}`;
26
- const code = error.showSourceCode();
27
- if (code) {
28
- this.message += `\n\n${code}\n`;
29
- }
30
-
31
- // We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
32
- this.stack = false;
33
- }
34
- }
35
- exports.default = CssSyntaxError;
package/dist/Warning.js DELETED
@@ -1,29 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- class Warning extends Error {
8
- constructor(warning) {
9
- super(warning);
10
- const {
11
- text,
12
- line,
13
- column
14
- } = warning;
15
- this.name = "Warning";
16
-
17
- // Based on https://github.com/postcss/postcss/blob/master/lib/warning.es6#L74
18
- // We don't need `plugin` properties.
19
- this.message = `${this.name}\n\n`;
20
- if (typeof line !== "undefined") {
21
- this.message += `(${line}:${column}) `;
22
- }
23
- this.message += `${text}`;
24
-
25
- // We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
26
- this.stack = false;
27
- }
28
- }
29
- exports.default = Warning;