css-loader 4.2.1 → 4.2.2
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 +7 -0
- package/README.md +106 -1
- package/dist/index.js +7 -5
- package/dist/utils.js +80 -21
- package/package.json +10 -8
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
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
|
+
### [4.2.2](https://github.com/webpack-contrib/css-loader/compare/v4.2.1...v4.2.2) (2020-08-24)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* 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))
|
|
11
|
+
|
|
5
12
|
### [4.2.1](https://github.com/webpack-contrib/css-loader/compare/v4.2.0...v4.2.1) (2020-08-06)
|
|
6
13
|
|
|
7
14
|
|
package/README.md
CHANGED
|
@@ -951,7 +951,7 @@ module.exports = {
|
|
|
951
951
|
};
|
|
952
952
|
```
|
|
953
953
|
|
|
954
|
-
##### `
|
|
954
|
+
##### `exportLocalsConvention`
|
|
955
955
|
|
|
956
956
|
Type: `String`
|
|
957
957
|
Default: based on the `modules.namedExport` option value, if `true` - `camelCaseOnly`, otherwise `asIs`
|
|
@@ -1243,6 +1243,111 @@ module.exports = {
|
|
|
1243
1243
|
};
|
|
1244
1244
|
```
|
|
1245
1245
|
|
|
1246
|
+
### Separating `Interoperable CSS`-only and `CSS Module` features
|
|
1247
|
+
|
|
1248
|
+
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.
|
|
1249
|
+
Meanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.
|
|
1250
|
+
|
|
1251
|
+
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).
|
|
1252
|
+
|
|
1253
|
+
**webpack.config.js**
|
|
1254
|
+
|
|
1255
|
+
```js
|
|
1256
|
+
module.exports = {
|
|
1257
|
+
module: {
|
|
1258
|
+
rules: [
|
|
1259
|
+
// ...
|
|
1260
|
+
// --------
|
|
1261
|
+
// SCSS ALL EXCEPT MODULES
|
|
1262
|
+
{
|
|
1263
|
+
test: /\.scss$/,
|
|
1264
|
+
exclude: /\.module\.scss$/,
|
|
1265
|
+
use: [
|
|
1266
|
+
{
|
|
1267
|
+
loader: 'style-loader'
|
|
1268
|
+
},
|
|
1269
|
+
{
|
|
1270
|
+
loader: 'css-loader',
|
|
1271
|
+
options: {
|
|
1272
|
+
importLoaders: 1,
|
|
1273
|
+
modules: {
|
|
1274
|
+
compileType: 'icss'
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
},
|
|
1278
|
+
{
|
|
1279
|
+
loader: 'sass-loader'
|
|
1280
|
+
},
|
|
1281
|
+
],
|
|
1282
|
+
},
|
|
1283
|
+
// --------
|
|
1284
|
+
// SCSS MODULES
|
|
1285
|
+
{
|
|
1286
|
+
test: /\.module\.scss$/,
|
|
1287
|
+
use: [
|
|
1288
|
+
{
|
|
1289
|
+
loader: 'style-loader'
|
|
1290
|
+
},
|
|
1291
|
+
{
|
|
1292
|
+
loader: 'css-loader',
|
|
1293
|
+
options: {
|
|
1294
|
+
importLoaders: 1,
|
|
1295
|
+
modules: {
|
|
1296
|
+
compileType: 'module'
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
},
|
|
1300
|
+
{
|
|
1301
|
+
loader: 'sass-loader'
|
|
1302
|
+
},
|
|
1303
|
+
],
|
|
1304
|
+
},
|
|
1305
|
+
// --------
|
|
1306
|
+
// ...
|
|
1307
|
+
},
|
|
1308
|
+
};
|
|
1309
|
+
```
|
|
1310
|
+
|
|
1311
|
+
**variables.scss**
|
|
1312
|
+
|
|
1313
|
+
File treated as `ICSS`-only.
|
|
1314
|
+
|
|
1315
|
+
```scss
|
|
1316
|
+
$colorBackground: red;
|
|
1317
|
+
:export {
|
|
1318
|
+
colorBackgroundCanvas: $colorBackground;
|
|
1319
|
+
}
|
|
1320
|
+
```
|
|
1321
|
+
|
|
1322
|
+
**Component.module.scss**
|
|
1323
|
+
|
|
1324
|
+
File treated as `CSS Module`.
|
|
1325
|
+
|
|
1326
|
+
```scss
|
|
1327
|
+
@import 'variables.scss';
|
|
1328
|
+
.componentClass {
|
|
1329
|
+
background-color: $colorBackground;
|
|
1330
|
+
}
|
|
1331
|
+
```
|
|
1332
|
+
|
|
1333
|
+
**Component.jsx**
|
|
1334
|
+
|
|
1335
|
+
Using both `CSS Module` functionality as well as SCSS variables directly in JavaScript.
|
|
1336
|
+
|
|
1337
|
+
```jsx
|
|
1338
|
+
import svars from 'variables.scss';
|
|
1339
|
+
import styles from 'Component.module.scss';
|
|
1340
|
+
|
|
1341
|
+
// Render DOM with CSS modules class name
|
|
1342
|
+
// <div className={styles.componentClass}>
|
|
1343
|
+
// <canvas ref={mountsCanvas}/>
|
|
1344
|
+
// </div>
|
|
1345
|
+
|
|
1346
|
+
// Somewhere in JavaScript canvas drawing code use the variable directly
|
|
1347
|
+
// const ctx = mountsCanvas.current.getContext('2d',{alpha: false});
|
|
1348
|
+
ctx.fillStyle = `${svars.colorBackgroundCanvas}`;
|
|
1349
|
+
```
|
|
1350
|
+
|
|
1246
1351
|
## Contributing
|
|
1247
1352
|
|
|
1248
1353
|
Please take a moment to read our contributing guidelines if you haven't yet done so.
|
package/dist/index.js
CHANGED
|
@@ -131,15 +131,17 @@ 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:
|
|
139
|
-
to:
|
|
141
|
+
from: resourcePath,
|
|
142
|
+
to: resourcePath,
|
|
140
143
|
map: options.sourceMap ? {
|
|
141
|
-
|
|
142
|
-
prev: map ? (0, _utils.normalizeSourceMap)(map) : null,
|
|
144
|
+
prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
|
|
143
145
|
inline: false,
|
|
144
146
|
annotation: false
|
|
145
147
|
} : false
|
|
@@ -168,7 +170,7 @@ async function loader(content, map, meta) {
|
|
|
168
170
|
}
|
|
169
171
|
|
|
170
172
|
const importCode = (0, _utils.getImportCode)(imports, options);
|
|
171
|
-
const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options);
|
|
173
|
+
const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
|
|
172
174
|
const exportCode = (0, _utils.getExportCode)(exports, replacements, options);
|
|
173
175
|
callback(null, `${importCode}${moduleCode}${exportCode}`);
|
|
174
176
|
}
|
package/dist/utils.js
CHANGED
|
@@ -28,8 +28,6 @@ var _path = _interopRequireDefault(require("path"));
|
|
|
28
28
|
|
|
29
29
|
var _loaderUtils = require("loader-utils");
|
|
30
30
|
|
|
31
|
-
var _normalizePath = _interopRequireDefault(require("normalize-path"));
|
|
32
|
-
|
|
33
31
|
var _cssesc = _interopRequireDefault(require("cssesc"));
|
|
34
32
|
|
|
35
33
|
var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-values"));
|
|
@@ -66,6 +64,10 @@ function unescape(str) {
|
|
|
66
64
|
String.fromCharCode(high >> 10 | 0xd800, high & 0x3ff | 0xdc00);
|
|
67
65
|
/* eslint-enable line-comment-position */
|
|
68
66
|
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function normalizePath(file) {
|
|
70
|
+
return _path.default.sep === '\\' ? file.replace(/\\/g, '/') : file;
|
|
69
71
|
} // eslint-disable-next-line no-control-regex
|
|
70
72
|
|
|
71
73
|
|
|
@@ -81,7 +83,7 @@ function defaultGetLocalIdent(loaderContext, localIdentName, localName, options)
|
|
|
81
83
|
const {
|
|
82
84
|
resourcePath
|
|
83
85
|
} = loaderContext;
|
|
84
|
-
const request = (
|
|
86
|
+
const request = normalizePath(_path.default.relative(context, resourcePath)); // eslint-disable-next-line no-param-reassign
|
|
85
87
|
|
|
86
88
|
options.content = `${hashPrefix + request}\x00${unescape(localName)}`; // Using `[path]` placeholder outputs `/` we need escape their
|
|
87
89
|
// Also directories can contains invalid characters for css we need escape their too
|
|
@@ -290,26 +292,57 @@ function getModulesPlugins(options, loaderContext) {
|
|
|
290
292
|
return plugins;
|
|
291
293
|
}
|
|
292
294
|
|
|
293
|
-
|
|
295
|
+
const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
|
|
296
|
+
const ABSOLUTE_SCHEME = /^[a-z0-9+\-.]+:/i;
|
|
297
|
+
|
|
298
|
+
function getURLType(source) {
|
|
299
|
+
if (source[0] === '/') {
|
|
300
|
+
if (source[1] === '/') {
|
|
301
|
+
return 'scheme-relative';
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return 'path-absolute';
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (IS_NATIVE_WIN32_PATH.test(source)) {
|
|
308
|
+
return 'path-absolute';
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return ABSOLUTE_SCHEME.test(source) ? 'absolute' : 'path-relative';
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function normalizeSourceMap(map, resourcePath) {
|
|
294
315
|
let newMap = map; // Some loader emit source map as string
|
|
295
316
|
// Strip any JSON XSSI avoidance prefix from the string (as documented in the source maps specification), and then parse the string as JSON.
|
|
296
317
|
|
|
297
318
|
if (typeof newMap === 'string') {
|
|
298
319
|
newMap = JSON.parse(newMap);
|
|
299
|
-
} // Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
|
|
300
|
-
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (newMap.file) {
|
|
304
|
-
newMap.file = (0, _normalizePath.default)(newMap.file);
|
|
305
320
|
}
|
|
306
321
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
322
|
+
delete newMap.file;
|
|
323
|
+
const {
|
|
324
|
+
sourceRoot
|
|
325
|
+
} = newMap;
|
|
326
|
+
delete newMap.sourceRoot;
|
|
310
327
|
|
|
311
328
|
if (newMap.sources) {
|
|
312
|
-
|
|
329
|
+
// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
|
|
330
|
+
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
|
|
331
|
+
newMap.sources = newMap.sources.map(source => {
|
|
332
|
+
// Non-standard syntax from `postcss`
|
|
333
|
+
if (source.indexOf('<') === 0) {
|
|
334
|
+
return source;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const sourceType = getURLType(source); // Do no touch `scheme-relative` and `absolute` URLs
|
|
338
|
+
|
|
339
|
+
if (sourceType === 'path-relative' || sourceType === 'path-absolute') {
|
|
340
|
+
const absoluteSource = sourceType === 'path-relative' && sourceRoot ? _path.default.resolve(sourceRoot, normalizePath(source)) : normalizePath(source);
|
|
341
|
+
return _path.default.relative(_path.default.dirname(resourcePath), absoluteSource);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return source;
|
|
345
|
+
});
|
|
313
346
|
}
|
|
314
347
|
|
|
315
348
|
return newMap;
|
|
@@ -360,17 +393,43 @@ function getImportCode(imports, options) {
|
|
|
360
393
|
return code ? `// Imports\n${code}` : '';
|
|
361
394
|
}
|
|
362
395
|
|
|
363
|
-
function
|
|
396
|
+
function normalizeSourceMapForRuntime(map, loaderContext) {
|
|
397
|
+
const resultMap = map ? map.toJSON() : null;
|
|
398
|
+
|
|
399
|
+
if (resultMap) {
|
|
400
|
+
delete resultMap.file;
|
|
401
|
+
resultMap.sourceRoot = '';
|
|
402
|
+
resultMap.sources = resultMap.sources.map(source => {
|
|
403
|
+
// Non-standard syntax from `postcss`
|
|
404
|
+
if (source.indexOf('<') === 0) {
|
|
405
|
+
return source;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const sourceType = getURLType(source);
|
|
409
|
+
|
|
410
|
+
if (sourceType !== 'path-relative') {
|
|
411
|
+
return source;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const resourceDirname = _path.default.dirname(loaderContext.resourcePath);
|
|
415
|
+
|
|
416
|
+
const absoluteSource = _path.default.resolve(resourceDirname, source);
|
|
417
|
+
|
|
418
|
+
const contextifyPath = normalizePath(_path.default.relative(loaderContext.rootContext, absoluteSource));
|
|
419
|
+
return `webpack://${contextifyPath}`;
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return JSON.stringify(resultMap);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function getModuleCode(result, api, replacements, options, loaderContext) {
|
|
364
427
|
if (options.modules.exportOnlyLocals === true) {
|
|
365
428
|
return '';
|
|
366
429
|
}
|
|
367
430
|
|
|
368
|
-
const {
|
|
369
|
-
|
|
370
|
-
map
|
|
371
|
-
} = result;
|
|
372
|
-
const sourceMapValue = options.sourceMap && map ? `,${map}` : '';
|
|
373
|
-
let code = JSON.stringify(css);
|
|
431
|
+
const sourceMapValue = options.sourceMap ? `,${normalizeSourceMapForRuntime(result.map, loaderContext)}` : '';
|
|
432
|
+
let code = JSON.stringify(result.css);
|
|
374
433
|
let beforeCode = `var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(${options.sourceMap});\n`;
|
|
375
434
|
|
|
376
435
|
for (const item of api) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "css-loader",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.2",
|
|
4
4
|
"description": "css loader module for webpack",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "webpack-contrib/css-loader",
|
|
@@ -47,7 +47,6 @@
|
|
|
47
47
|
"cssesc": "^3.0.0",
|
|
48
48
|
"icss-utils": "^4.1.1",
|
|
49
49
|
"loader-utils": "^2.0.0",
|
|
50
|
-
"normalize-path": "^3.0.0",
|
|
51
50
|
"postcss": "^7.0.32",
|
|
52
51
|
"postcss-modules-extract-imports": "^2.0.0",
|
|
53
52
|
"postcss-modules-local-by-default": "^3.0.3",
|
|
@@ -59,10 +58,10 @@
|
|
|
59
58
|
},
|
|
60
59
|
"devDependencies": {
|
|
61
60
|
"@babel/cli": "^7.10.5",
|
|
62
|
-
"@babel/core": "^7.
|
|
61
|
+
"@babel/core": "^7.11.4",
|
|
63
62
|
"@babel/preset-env": "^7.10.4",
|
|
64
|
-
"@commitlint/cli": "^
|
|
65
|
-
"@commitlint/config-conventional": "^
|
|
63
|
+
"@commitlint/cli": "^10.0.0",
|
|
64
|
+
"@commitlint/config-conventional": "^10.0.0",
|
|
66
65
|
"@webpack-contrib/defaults": "^6.3.0",
|
|
67
66
|
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
|
|
68
67
|
"babel-jest": "^26.1.0",
|
|
@@ -76,20 +75,23 @@
|
|
|
76
75
|
"file-loader": "^6.0.0",
|
|
77
76
|
"husky": "^4.2.5",
|
|
78
77
|
"jest": "^26.1.0",
|
|
78
|
+
"less-loader": "^6.2.0",
|
|
79
79
|
"lint-staged": "^10.2.11",
|
|
80
80
|
"memfs": "^3.2.0",
|
|
81
|
-
"mini-css-extract-plugin": "^0.
|
|
81
|
+
"mini-css-extract-plugin": "^0.10.0",
|
|
82
82
|
"npm-run-all": "^4.1.5",
|
|
83
83
|
"postcss-loader": "^3.0.0",
|
|
84
84
|
"postcss-preset-env": "^6.7.0",
|
|
85
85
|
"prettier": "^2.0.5",
|
|
86
86
|
"sass": "^1.26.10",
|
|
87
87
|
"sass-loader": "^9.0.2",
|
|
88
|
-
"standard-version": "^
|
|
88
|
+
"standard-version": "^9.0.0",
|
|
89
89
|
"strip-ansi": "^6.0.0",
|
|
90
90
|
"style-loader": "^1.2.1",
|
|
91
|
+
"stylus": "^0.54.8",
|
|
92
|
+
"stylus-loader": "^3.0.2",
|
|
91
93
|
"url-loader": "^4.1.0",
|
|
92
|
-
"webpack": "^4.44.
|
|
94
|
+
"webpack": "^4.44.1"
|
|
93
95
|
},
|
|
94
96
|
"keywords": [
|
|
95
97
|
"webpack",
|