css-loader 4.2.0 → 5.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/CHANGELOG.md CHANGED
@@ -2,6 +2,52 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [5.0.0](https://github.com/webpack-contrib/css-loader/compare/v4.3.0...v5.0.0) (2020-10-13)
6
+
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ * migrate on PostCSS 8
11
+ * runtime doesn't contain source maps code without `sourceMap: true`
12
+ * returned value from the `getLocalIdent` escapes by default, the `exportName` value is always unescaped
13
+ * Auto enable icss modules for all files for which `/\.icss\.\w+$/i` (the `modules.compileType` option is `icss`)
14
+ * `[emoji]` placeholder was deprecated
15
+ * `icss` option was removed (it was deprecated previously)
16
+
17
+ ### Features
18
+
19
+ * allow named exports to have underscores in names ([#1209](https://github.com/webpack-contrib/css-loader/issues/1209)) ([747d62b](https://github.com/webpack-contrib/css-loader/commit/747d62b75a878d8881f4819b96297667dc689b8f))
20
+ * hide warning when you don't need handle `url()`/`@import` ([#1195](https://github.com/webpack-contrib/css-loader/issues/1195)) ([dd52931](https://github.com/webpack-contrib/css-loader/commit/dd52931150ed42f122d9017642437c26cc1b2422))
21
+ * improve error message ([52412f6](https://github.com/webpack-contrib/css-loader/commit/52412f6d5a54745ee37a4a67f038455c26ba5772))
22
+ * reduce runtime ([9f974be](https://github.com/webpack-contrib/css-loader/commit/9f974be81f5942d3afaf783529677bd541952fa3))
23
+ * add fallback if custom getLocalIdent returns `null`/`undefined` ([#1193](https://github.com/webpack-contrib/css-loader/issues/1193)) ([0f95841](https://github.com/webpack-contrib/css-loader/commit/0f9584135e63f9f354043e7f414e0c1aad0edc6e))
24
+
25
+ ## [4.3.0](https://github.com/webpack-contrib/css-loader/compare/v4.2.2...v4.3.0) (2020-09-08)
26
+
27
+
28
+ ### Features
29
+
30
+ * the `importLoaders` can be `string` ([#1178](https://github.com/webpack-contrib/css-loader/issues/1178)) ([ec58a7c](https://github.com/webpack-contrib/css-loader/commit/ec58a7cfda46443e35539d66b86685195fa5db03))
31
+
32
+
33
+ ### Bug Fixes
34
+
35
+ * line breaks in `url` function ([88b8ddc](https://github.com/webpack-contrib/css-loader/commit/88b8ddc1d78a2b6a917ed2dfe2f2a37cf6a84190))
36
+
37
+ ### [4.2.2](https://github.com/webpack-contrib/css-loader/compare/v4.2.1...v4.2.2) (2020-08-24)
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * source maps generation, source from source maps are now relative to `compiler.context` and use `webpack://` protocol ([#1169](https://github.com/webpack-contrib/css-loader/issues/1169)) ([fb5c53d](https://github.com/webpack-contrib/css-loader/commit/fb5c53d80b10ee698762238bb7b122aec8c5048d))
43
+
44
+ ### [4.2.1](https://github.com/webpack-contrib/css-loader/compare/v4.2.0...v4.2.1) (2020-08-06)
45
+
46
+
47
+ ### Bug Fixes
48
+
49
+ * regression with the `exportOnlyLocals` option, now `locals` are not exported under the `locals` name, it was big regression, we apologize for that ([24c0a12](https://github.com/webpack-contrib/css-loader/commit/24c0a122d1396c08326a24f6184f5da09cf52ccc))
50
+
5
51
  ## [4.2.0](https://github.com/webpack-contrib/css-loader/compare/v4.1.1...v4.2.0) (2020-07-31)
6
52
 
7
53
 
package/README.md CHANGED
@@ -530,7 +530,7 @@ module.exports = {
530
530
  auto: true,
531
531
  exportGlobals: true,
532
532
  localIdentName: '[path][name]__[local]--[hash:base64:5]',
533
- context: path.resolve(__dirname, 'src'),
533
+ localIdentContext: path.resolve(__dirname, 'src'),
534
534
  localIdentHashPrefix: 'my-custom-hash',
535
535
  namedExport: true,
536
536
  exportLocalsConvention: 'camelCase',
@@ -780,7 +780,7 @@ module.exports = {
780
780
  loader: 'css-loader',
781
781
  options: {
782
782
  modules: {
783
- context: path.resolve(__dirname, 'context'),
783
+ localIdentContext: path.resolve(__dirname, 'src'),
784
784
  },
785
785
  },
786
786
  },
@@ -848,6 +848,8 @@ Default: `undefined`
848
848
 
849
849
  Allows to specify a function to generate the classname.
850
850
  By default we use built-in function to generate a classname.
851
+ If the custom function returns `null` or `undefined`, we fallback to the
852
+ built-in function to generate the classname.
851
853
 
852
854
  **webpack.config.js**
853
855
 
@@ -951,7 +953,7 @@ module.exports = {
951
953
  };
952
954
  ```
953
955
 
954
- ##### `exportlocalsConvention`
956
+ ##### `exportLocalsConvention`
955
957
 
956
958
  Type: `String`
957
959
  Default: based on the `modules.namedExport` option value, if `true` - `camelCaseOnly`, otherwise `asIs`
@@ -1243,6 +1245,111 @@ module.exports = {
1243
1245
  };
1244
1246
  ```
1245
1247
 
1248
+ ### Separating `Interoperable CSS`-only and `CSS Module` features
1249
+
1250
+ 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 `compileType` 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.
1251
+ Meanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.
1252
+
1253
+ An example case is assumed where a project requires canvas drawing variables to be synchronized with CSS - canvas drawing uses the same color (set by color name in JavaScript) as HTML background (set by class name in CSS).
1254
+
1255
+ **webpack.config.js**
1256
+
1257
+ ```js
1258
+ module.exports = {
1259
+ module: {
1260
+ rules: [
1261
+ // ...
1262
+ // --------
1263
+ // SCSS ALL EXCEPT MODULES
1264
+ {
1265
+ test: /\.scss$/,
1266
+ exclude: /\.module\.scss$/,
1267
+ use: [
1268
+ {
1269
+ loader: 'style-loader'
1270
+ },
1271
+ {
1272
+ loader: 'css-loader',
1273
+ options: {
1274
+ importLoaders: 1,
1275
+ modules: {
1276
+ compileType: 'icss'
1277
+ }
1278
+ }
1279
+ },
1280
+ {
1281
+ loader: 'sass-loader'
1282
+ },
1283
+ ],
1284
+ },
1285
+ // --------
1286
+ // SCSS MODULES
1287
+ {
1288
+ test: /\.module\.scss$/,
1289
+ use: [
1290
+ {
1291
+ loader: 'style-loader'
1292
+ },
1293
+ {
1294
+ loader: 'css-loader',
1295
+ options: {
1296
+ importLoaders: 1,
1297
+ modules: {
1298
+ compileType: 'module'
1299
+ }
1300
+ }
1301
+ },
1302
+ {
1303
+ loader: 'sass-loader'
1304
+ },
1305
+ ],
1306
+ },
1307
+ // --------
1308
+ // ...
1309
+ },
1310
+ };
1311
+ ```
1312
+
1313
+ **variables.scss**
1314
+
1315
+ File treated as `ICSS`-only.
1316
+
1317
+ ```scss
1318
+ $colorBackground: red;
1319
+ :export {
1320
+ colorBackgroundCanvas: $colorBackground;
1321
+ }
1322
+ ```
1323
+
1324
+ **Component.module.scss**
1325
+
1326
+ File treated as `CSS Module`.
1327
+
1328
+ ```scss
1329
+ @import 'variables.scss';
1330
+ .componentClass {
1331
+ background-color: $colorBackground;
1332
+ }
1333
+ ```
1334
+
1335
+ **Component.jsx**
1336
+
1337
+ Using both `CSS Module` functionality as well as SCSS variables directly in JavaScript.
1338
+
1339
+ ```jsx
1340
+ import svars from 'variables.scss';
1341
+ import styles from 'Component.module.scss';
1342
+
1343
+ // Render DOM with CSS modules class name
1344
+ // <div className={styles.componentClass}>
1345
+ // <canvas ref={mountsCanvas}/>
1346
+ // </div>
1347
+
1348
+ // Somewhere in JavaScript canvas drawing code use the variable directly
1349
+ // const ctx = mountsCanvas.current.getContext('2d',{alpha: false});
1350
+ ctx.fillStyle = `${svars.colorBackgroundCanvas}`;
1351
+ ```
1352
+
1246
1353
  ## Contributing
1247
1354
 
1248
1355
  Please take a moment to read our contributing guidelines if you haven't yet done so.
@@ -11,7 +11,8 @@ class CssSyntaxError extends Error {
11
11
  const {
12
12
  reason,
13
13
  line,
14
- column
14
+ column,
15
+ file
15
16
  } = error;
16
17
  this.name = 'CssSyntaxError'; // Based on https://github.com/postcss/postcss/blob/master/lib/css-syntax-error.es6#L132
17
18
  // We don't need `plugin` and `file` properties.
@@ -22,6 +23,7 @@ class CssSyntaxError extends Error {
22
23
  this.message += `(${line}:${column}) `;
23
24
  }
24
25
 
26
+ this.message += file ? `${file} ` : '<css input> ';
25
27
  this.message += `${reason}`;
26
28
  const code = error.showSourceCode();
27
29
 
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ var _postcss = _interopRequireDefault(require("postcss"));
11
11
 
12
12
  var _package = _interopRequireDefault(require("postcss/package.json"));
13
13
 
14
- var _schemaUtils = _interopRequireDefault(require("schema-utils"));
14
+ var _schemaUtils = require("schema-utils");
15
15
 
16
16
  var _semver = require("semver");
17
17
 
@@ -33,7 +33,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
33
33
  */
34
34
  async function loader(content, map, meta) {
35
35
  const rawOptions = (0, _loaderUtils.getOptions)(this);
36
- (0, _schemaUtils.default)(_options.default, rawOptions, {
36
+ (0, _schemaUtils.validate)(_options.default, rawOptions, {
37
37
  name: 'CSS Loader',
38
38
  baseDataPath: 'options'
39
39
  });
@@ -131,15 +131,18 @@ async function loader(content, map, meta) {
131
131
  }
132
132
  }
133
133
 
134
+ const {
135
+ resourcePath
136
+ } = this;
134
137
  let result;
135
138
 
136
139
  try {
137
140
  result = await (0, _postcss.default)(plugins).process(content, {
138
- from: this.resourcePath,
139
- to: this.resourcePath,
141
+ hideNothingWarning: true,
142
+ from: resourcePath,
143
+ to: resourcePath,
140
144
  map: options.sourceMap ? {
141
- // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
142
- prev: map ? (0, _utils.normalizeSourceMap)(map) : null,
145
+ prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
143
146
  inline: false,
144
147
  annotation: false
145
148
  } : false
@@ -165,10 +168,17 @@ async function loader(content, map, meta) {
165
168
  importName: '___CSS_LOADER_API_IMPORT___',
166
169
  url: (0, _loaderUtils.stringifyRequest)(this, require.resolve('./runtime/api'))
167
170
  });
171
+
172
+ if (options.sourceMap) {
173
+ imports.unshift({
174
+ importName: '___CSS_LOADER_API_SOURCEMAP_IMPORT___',
175
+ url: (0, _loaderUtils.stringifyRequest)(this, require.resolve('./runtime/cssWithMappingToString'))
176
+ });
177
+ }
168
178
  }
169
179
 
170
180
  const importCode = (0, _utils.getImportCode)(imports, options);
171
- const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options);
181
+ const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
172
182
  const exportCode = (0, _utils.getExportCode)(exports, replacements, options);
173
183
  callback(null, `${importCode}${moduleCode}${exportCode}`);
174
184
  }
package/dist/options.json CHANGED
@@ -122,10 +122,6 @@
122
122
  }
123
123
  ]
124
124
  },
125
- "icss": {
126
- "description": "Enables/Disables handling the CSS module interoperable import/export format ((https://github.com/webpack-contrib/css-loader#icss)",
127
- "type": "boolean"
128
- },
129
125
  "sourceMap": {
130
126
  "description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
131
127
  "type": "boolean"
@@ -136,6 +132,9 @@
136
132
  {
137
133
  "type": "boolean"
138
134
  },
135
+ {
136
+ "type": "string"
137
+ },
139
138
  {
140
139
  "type": "integer"
141
140
  }
@@ -5,109 +5,114 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _postcss = _interopRequireDefault(require("postcss"));
9
-
10
8
  var _icssUtils = require("icss-utils");
11
9
 
12
10
  var _utils = require("../utils");
13
11
 
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- var _default = _postcss.default.plugin('postcss-icss-parser', options => async css => {
17
- const importReplacements = Object.create(null);
18
- const {
19
- icssImports,
20
- icssExports
21
- } = (0, _icssUtils.extractICSS)(css);
22
- const imports = new Map();
23
- const tasks = []; // eslint-disable-next-line guard-for-in
24
-
25
- for (const url in icssImports) {
26
- const tokens = icssImports[url];
27
-
28
- if (Object.keys(tokens).length === 0) {
29
- // eslint-disable-next-line no-continue
30
- continue;
31
- }
32
-
33
- let normalizedUrl = url;
34
- let prefix = '';
35
- const queryParts = normalizedUrl.split('!');
12
+ const plugin = (options = {}) => {
13
+ return {
14
+ postcssPlugin: 'postcss-icss-parser',
36
15
 
37
- if (queryParts.length > 1) {
38
- normalizedUrl = queryParts.pop();
39
- prefix = queryParts.join('!');
40
- }
41
-
42
- const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), options.rootContext);
43
-
44
- const doResolve = async () => {
16
+ async OnceExit(root) {
17
+ const importReplacements = Object.create(null);
45
18
  const {
46
- resolver,
47
- context
48
- } = options;
49
- const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([normalizedUrl, request])]);
50
- return {
51
- url: resolvedUrl,
52
- prefix,
53
- tokens
54
- };
55
- };
56
-
57
- tasks.push(doResolve());
58
- }
59
-
60
- const results = await Promise.all(tasks);
61
-
62
- for (let index = 0; index <= results.length - 1; index++) {
63
- const {
64
- url,
65
- prefix,
66
- tokens
67
- } = results[index];
68
- const newUrl = prefix ? `${prefix}!${url}` : url;
69
- const importKey = newUrl;
70
- let importName = imports.get(importKey);
71
-
72
- if (!importName) {
73
- importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
74
- imports.set(importKey, importName);
75
- options.imports.push({
76
- importName,
77
- url: options.urlHandler(newUrl),
78
- icss: true,
79
- index
80
- });
81
- options.api.push({
82
- importName,
83
- dedupe: true,
84
- index
85
- });
19
+ icssImports,
20
+ icssExports
21
+ } = (0, _icssUtils.extractICSS)(root);
22
+ const imports = new Map();
23
+ const tasks = []; // eslint-disable-next-line guard-for-in
24
+
25
+ for (const url in icssImports) {
26
+ const tokens = icssImports[url];
27
+
28
+ if (Object.keys(tokens).length === 0) {
29
+ // eslint-disable-next-line no-continue
30
+ continue;
31
+ }
32
+
33
+ let normalizedUrl = url;
34
+ let prefix = '';
35
+ const queryParts = normalizedUrl.split('!');
36
+
37
+ if (queryParts.length > 1) {
38
+ normalizedUrl = queryParts.pop();
39
+ prefix = queryParts.join('!');
40
+ }
41
+
42
+ const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), options.rootContext);
43
+
44
+ const doResolve = async () => {
45
+ const {
46
+ resolver,
47
+ context
48
+ } = options;
49
+ const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([normalizedUrl, request])]);
50
+ return {
51
+ url: resolvedUrl,
52
+ prefix,
53
+ tokens
54
+ };
55
+ };
56
+
57
+ tasks.push(doResolve());
58
+ }
59
+
60
+ const results = await Promise.all(tasks);
61
+
62
+ for (let index = 0; index <= results.length - 1; index++) {
63
+ const {
64
+ url,
65
+ prefix,
66
+ tokens
67
+ } = results[index];
68
+ const newUrl = prefix ? `${prefix}!${url}` : url;
69
+ const importKey = newUrl;
70
+ let importName = imports.get(importKey);
71
+
72
+ if (!importName) {
73
+ importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
74
+ imports.set(importKey, importName);
75
+ options.imports.push({
76
+ importName,
77
+ url: options.urlHandler(newUrl),
78
+ icss: true,
79
+ index
80
+ });
81
+ options.api.push({
82
+ importName,
83
+ dedupe: true,
84
+ index
85
+ });
86
+ }
87
+
88
+ for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
89
+ const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
90
+ const localName = tokens[token];
91
+ importReplacements[token] = replacementName;
92
+ options.replacements.push({
93
+ replacementName,
94
+ importName,
95
+ localName
96
+ });
97
+ }
98
+ }
99
+
100
+ if (Object.keys(importReplacements).length > 0) {
101
+ (0, _icssUtils.replaceSymbols)(root, importReplacements);
102
+ }
103
+
104
+ for (const name of Object.keys(icssExports)) {
105
+ const value = (0, _icssUtils.replaceValueSymbols)(icssExports[name], importReplacements);
106
+ options.exports.push({
107
+ name,
108
+ value
109
+ });
110
+ }
86
111
  }
87
112
 
88
- for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
89
- const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
90
- const localName = tokens[token];
91
- importReplacements[token] = replacementName;
92
- options.replacements.push({
93
- replacementName,
94
- importName,
95
- localName
96
- });
97
- }
98
- }
99
-
100
- if (Object.keys(importReplacements).length > 0) {
101
- (0, _icssUtils.replaceSymbols)(css, importReplacements);
102
- }
103
-
104
- for (const name of Object.keys(icssExports)) {
105
- const value = (0, _icssUtils.replaceValueSymbols)(icssExports[name], importReplacements);
106
- options.exports.push({
107
- name,
108
- value
109
- });
110
- }
111
- });
113
+ };
114
+ };
112
115
 
116
+ plugin.postcss = true;
117
+ var _default = plugin;
113
118
  exports.default = _default;