css-loader 6.7.3 → 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/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
  [![node][node]][node-url]
12
12
  [![tests][tests]][tests-url]
13
13
  [![coverage][cover]][cover-url]
14
- [![chat][chat]][chat-url]
14
+ [![discussion][discussion]][discussion-url]
15
15
  [![size][size]][size-url]
16
16
 
17
17
  # css-loader
@@ -22,7 +22,7 @@ The `css-loader` interprets `@import` and `url()` like `import/require()` and wi
22
22
 
23
23
  > **Warning**
24
24
  >
25
- > To use css-loader, webpack@5 is required
25
+ > To use the latest version of css-loader, webpack@5 is required
26
26
 
27
27
  To begin, you'll need to install `css-loader`:
28
28
 
@@ -184,9 +184,17 @@ Type:
184
184
  <!-- use other name to prettify since import is reserved keyword -->
185
185
 
186
186
  ```ts
187
- type import =
187
+ type importFn =
188
188
  | boolean
189
- | { filter: (url: string, media: string, resourcePath: string) => boolean };
189
+ | {
190
+ filter: (
191
+ url: string,
192
+ media: string,
193
+ resourcePath: string,
194
+ supports?: string,
195
+ layer?: string
196
+ ) => boolean;
197
+ };
190
198
  ```
191
199
 
192
200
  Default: `true`
@@ -693,11 +701,11 @@ Type:
693
701
 
694
702
  ```ts
695
703
  type mode =
696
- | "local"
697
- | "global"
698
- | "pure"
699
- | "icss"
700
- | ((resourcePath) => "local" | "global" | "pure" | "icss"))`
704
+ | "local"
705
+ | "global"
706
+ | "pure"
707
+ | "icss"
708
+ | ((resourcePath: string) => "local" | "global" | "pure" | "icss");
701
709
  ```
702
710
 
703
711
  Default: `'local'`
@@ -1696,6 +1704,8 @@ module.exports = {
1696
1704
  module: {
1697
1705
  rules: [
1698
1706
  {
1707
+ // If you enable `experiments.css` or `experiments.futureDefaults`, please uncomment line below
1708
+ // type: "javascript/auto",
1699
1709
  test: /\.(sa|sc|c)ss$/i,
1700
1710
  use: [
1701
1711
  devMode ? "style-loader" : MiniCssExtractPlugin.loader,
@@ -2026,7 +2036,7 @@ Please take a moment to read our contributing guidelines if you haven't yet done
2026
2036
  [tests-url]: https://github.com/webpack-contrib/css-loader/actions
2027
2037
  [cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
2028
2038
  [cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
2029
- [chat]: https://badges.gitter.im/webpack/webpack.svg
2030
- [chat-url]: https://gitter.im/webpack/webpack
2039
+ [discussion]: https://img.shields.io/github/discussions/webpack/webpack
2040
+ [discussion-url]: https://github.com/webpack/webpack/discussions
2031
2041
  [size]: https://packagephobia.now.sh/badge?p=css-loader
2032
2042
  [size-url]: https://packagephobia.now.sh/result?p=css-loader
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");
@@ -20,8 +18,12 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
20
18
 
21
19
  async function loader(content, map, meta) {
22
20
  const rawOptions = this.getOptions(_options.default);
23
- const plugins = [];
24
21
  const callback = this.async();
22
+ if (this._compiler && this._compiler.options && this._compiler.options.experiments && this._compiler.options.experiments.css && this._module && this._module.type === "css") {
23
+ this.emitWarning(new Error('You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `css-loader` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `css-loader` in your webpack config (now css-loader does nothing).'));
24
+ callback(null, content, map, meta);
25
+ return;
26
+ }
25
27
  let options;
26
28
  try {
27
29
  options = (0, _utils.normalizeOptions)(rawOptions, this);
@@ -29,6 +31,7 @@ async function loader(content, map, meta) {
29
31
  callback(error);
30
32
  return;
31
33
  }
34
+ const plugins = [];
32
35
  const replacements = [];
33
36
  const exports = [];
34
37
  if ((0, _utils.shouldUseModulesPlugins)(options)) {
@@ -120,11 +123,11 @@ async function loader(content, map, meta) {
120
123
  if (error.file) {
121
124
  this.addDependency(error.file);
122
125
  }
123
- callback(error.name === "CssSyntaxError" ? new _CssSyntaxError.default(error) : error);
126
+ callback(error.name === "CssSyntaxError" ? (0, _utils.syntaxErrorFactory)(error) : error);
124
127
  return;
125
128
  }
126
129
  for (const warning of result.warnings()) {
127
- this.emitWarning(new _Warning.default(warning));
130
+ this.emitWarning((0, _utils.warningFactory)(warning));
128
131
  }
129
132
  const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
130
133
  const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
@@ -146,14 +149,28 @@ async function loader(content, map, meta) {
146
149
  });
147
150
  }
148
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
+ }
149
166
  const importCode = (0, _utils.getImportCode)(imports, options);
150
167
  let moduleCode;
151
168
  try {
152
- moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
169
+ moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, isTemplateLiteralSupported, this);
153
170
  } catch (error) {
154
171
  callback(error);
155
172
  return;
156
173
  }
157
- const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options);
174
+ const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options, isTemplateLiteralSupported);
158
175
  callback(null, `${importCode}${moduleCode}${exportCode}`);
159
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.3",
3
+ "version": "6.8.0",
4
4
  "description": "css loader module for webpack",
5
5
  "license": "MIT",
6
6
  "repository": "webpack-contrib/css-loader",
@@ -28,6 +28,9 @@
28
28
  "lint:js": "eslint --cache .",
29
29
  "lint:spelling": "cspell \"**/*.*\"",
30
30
  "lint": "npm-run-all -l -p \"lint:**\"",
31
+ "fix:js": "npm run lint:js -- --fix",
32
+ "fix:prettier": "npm run lint:prettier -- --write",
33
+ "fix": "npm-run-all -l fix:js fix:prettier",
31
34
  "test:only": "cross-env NODE_ENV=test jest",
32
35
  "test:watch": "npm run test:only -- --watch",
33
36
  "test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
@@ -44,30 +47,30 @@
44
47
  },
45
48
  "dependencies": {
46
49
  "icss-utils": "^5.1.0",
47
- "postcss": "^8.4.19",
50
+ "postcss": "^8.4.21",
48
51
  "postcss-modules-extract-imports": "^3.0.0",
49
- "postcss-modules-local-by-default": "^4.0.0",
52
+ "postcss-modules-local-by-default": "^4.0.3",
50
53
  "postcss-modules-scope": "^3.0.0",
51
54
  "postcss-modules-values": "^4.0.0",
52
55
  "postcss-value-parser": "^4.2.0",
53
56
  "semver": "^7.3.8"
54
57
  },
55
58
  "devDependencies": {
56
- "@babel/cli": "^7.19.3",
57
- "@babel/core": "^7.20.5",
58
- "@babel/preset-env": "^7.20.2",
59
+ "@babel/cli": "^7.21.0",
60
+ "@babel/core": "^7.21.4",
61
+ "@babel/preset-env": "^7.21.4",
59
62
  "@commitlint/cli": "^16.3.0",
60
63
  "@commitlint/config-conventional": "^16.2.4",
61
64
  "@webpack-contrib/eslint-config-webpack": "^3.0.0",
62
65
  "babel-jest": "^28.1.3",
63
66
  "cross-env": "^7.0.3",
64
- "cspell": "^6.15.1",
67
+ "cspell": "^6.31.1",
65
68
  "del": "^6.1.1",
66
69
  "del-cli": "^4.0.1",
67
- "es-check": "^7.0.1",
68
- "eslint": "^8.28.0",
69
- "eslint-config-prettier": "^8.5.0",
70
- "eslint-plugin-import": "^2.26.0",
70
+ "es-check": "^7.1.0",
71
+ "eslint": "^8.37.0",
72
+ "eslint-config-prettier": "^8.8.0",
73
+ "eslint-plugin-import": "^2.27.5",
71
74
  "file-loader": "^6.2.0",
72
75
  "husky": "^7.0.1",
73
76
  "jest": "^28.1.3",
@@ -75,21 +78,21 @@
75
78
  "less": "^4.1.3",
76
79
  "less-loader": "^10.0.1",
77
80
  "lint-staged": "^12.5.0",
78
- "memfs": "^3.4.12",
79
- "mini-css-extract-plugin": "^2.7.1",
81
+ "memfs": "^3.4.13",
82
+ "mini-css-extract-plugin": "^2.7.5",
80
83
  "npm-run-all": "^4.1.5",
81
84
  "postcss-loader": "^6.2.1",
82
85
  "postcss-preset-env": "^7.8.3",
83
- "prettier": "^2.8.0",
84
- "sass": "^1.56.1",
86
+ "prettier": "^2.8.7",
87
+ "sass": "^1.60.0",
85
88
  "sass-loader": "^12.6.0",
86
89
  "standard-version": "^9.5.0",
87
90
  "strip-ansi": "^6.0.0",
88
- "style-loader": "^3.1.0",
89
- "stylus": "^0.56.0",
91
+ "style-loader": "^3.3.2",
92
+ "stylus": "^0.59.0",
90
93
  "stylus-loader": "^6.1.0",
91
94
  "url-loader": "^4.1.1",
92
- "webpack": "^5.75.0"
95
+ "webpack": "^5.77.0"
93
96
  },
94
97
  "keywords": [
95
98
  "webpack",
@@ -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;