Skip to content

Commit de3a09c

Browse files
feat: auto resolving css-modules import (Issue #994)
1 parent f23954c commit de3a09c

File tree

5 files changed

+67
-27
lines changed

5 files changed

+67
-27
lines changed

README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,17 @@ module.exports = {
541541

542542
##### `mode`
543543

544-
Type: `String`
544+
Type: `String|Function`
545545
Default: `'local'`
546546

547547
Setup `mode` option. You can omit the value when you want `local` mode.
548548

549+
###### `String`
550+
551+
Possible values:
552+
553+
`local`, `global`, `pure`
554+
549555
**webpack.config.js**
550556

551557
```js
@@ -566,6 +572,41 @@ module.exports = {
566572
};
567573
```
568574

575+
###### `Function`
576+
577+
Allows set different values modules.mode based on filename
578+
579+
Possible return values:
580+
581+
`local`, `global`, `pure`
582+
583+
**webpack.config.js**
584+
585+
```js
586+
module.exports = {
587+
module: {
588+
rules: [
589+
{
590+
test: /\.css$/i,
591+
loader: 'css-loader',
592+
options: {
593+
modules: {
594+
// Callback must return "local", "global" or "pure"
595+
mode: (filename) => {
596+
if (/global.css$/i.test(filename)) {
597+
return 'global';
598+
}
599+
600+
return 'local';
601+
},
602+
},
603+
},
604+
},
605+
],
606+
},
607+
};
608+
```
609+
569610
##### `localIdentName`
570611

571612
Type: `String`

test/__snapshots__/modules-option.test.js.snap

+18-19
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ exports[`"modules" option issue #1063 throw error: module 1`] = `
377377
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
378378
exports = ___CSS_LOADER_API_IMPORT___(false);
379379
// Module
380-
exports.push([module.id, \\".className {\\\\n color: green;\\\\n}\\\\n\\\\n:global(.otherClass) {\\\\n color: blue;\\\\n}\\\\n\\", \\"\\"]);
380+
exports.push([module.id, \\".classNameLocalFile {\\\\n color: green;\\\\n}\\\\n\\\\n:global(.otherClassLocalFile) {\\\\n color: blue;\\\\n}\\\\n\\", \\"\\"]);
381381
// Exports
382382
module.exports = exports;
383383
"
@@ -388,26 +388,26 @@ exports[`"modules" option issue #1063 throw error: module 2`] = `
388388
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
389389
exports = ___CSS_LOADER_API_IMPORT___(false);
390390
// Module
391-
exports.push([module.id, \\".className {\\\\n color: green;\\\\n}\\\\n\\\\n:local(.otherClass) {\\\\n color: blue;\\\\n}\\\\n\\", \\"\\"]);
391+
exports.push([module.id, \\".classNameGlobalFile {\\\\n color: black;\\\\n}\\\\n\\\\n:local(.otherClassGlobalFile) {\\\\n color: coral;\\\\n}\\\\n\\", \\"\\"]);
392392
// Exports
393393
module.exports = exports;
394394
"
395395
`;
396396
397397
exports[`"modules" option issue #1063 throw error: result 1`] = `
398-
".className {
398+
".classNameLocalFile {
399399
color: green;
400400
}
401401
402-
:global(.otherClass) {
402+
:global(.otherClassLocalFile) {
403403
color: blue;
404404
}
405-
.className {
406-
color: green;
405+
.classNameGlobalFile {
406+
color: black;
407407
}
408408
409-
:local(.otherClass) {
410-
color: blue;
409+
:local(.otherClassGlobalFile) {
410+
color: coral;
411411
}
412412
"
413413
`;
@@ -421,10 +421,10 @@ exports[`"modules" option issue #1063: module 1`] = `
421421
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
422422
exports = ___CSS_LOADER_API_IMPORT___(false);
423423
// Module
424-
exports.push([module.id, \\"._1iR-jQngOXizCZOLZck-4P {\\\\n color: green;\\\\n}\\\\n\\\\n.otherClass {\\\\n color: blue;\\\\n}\\\\n\\", \\"\\"]);
424+
exports.push([module.id, \\"._2cZDGA9F7Kx1vfQmWcLP51 {\\\\n color: green;\\\\n}\\\\n\\\\n.otherClassLocalFile {\\\\n color: blue;\\\\n}\\\\n\\", \\"\\"]);
425425
// Exports
426426
exports.locals = {
427-
\\"className\\": \\"_1iR-jQngOXizCZOLZck-4P\\"
427+
\\"classNameLocalFile\\": \\"_2cZDGA9F7Kx1vfQmWcLP51\\"
428428
};
429429
module.exports = exports;
430430
"
@@ -435,30 +435,29 @@ exports[`"modules" option issue #1063: module 2`] = `
435435
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
436436
exports = ___CSS_LOADER_API_IMPORT___(false);
437437
// Module
438-
exports.push([module.id, \\"._2WZy6LhVz_KGVbdNCAmL6C {\\\\n color: green;\\\\n}\\\\n\\\\n._7mafm73rmgiKuViZtzNmn {\\\\n color: blue;\\\\n}\\\\n\\", \\"\\"]);
438+
exports.push([module.id, \\".classNameGlobalFile {\\\\n color: black;\\\\n}\\\\n\\\\n._2rcag09JpwrP4_hfyyRmm- {\\\\n color: coral;\\\\n}\\\\n\\", \\"\\"]);
439439
// Exports
440440
exports.locals = {
441-
\\"className\\": \\"_2WZy6LhVz_KGVbdNCAmL6C\\",
442-
\\"otherClass\\": \\"_7mafm73rmgiKuViZtzNmn\\"
441+
\\"otherClassGlobalFile\\": \\"_2rcag09JpwrP4_hfyyRmm-\\"
443442
};
444443
module.exports = exports;
445444
"
446445
`;
447446
448447
exports[`"modules" option issue #1063: result 1`] = `
449-
"._1iR-jQngOXizCZOLZck-4P {
448+
"._2cZDGA9F7Kx1vfQmWcLP51 {
450449
color: green;
451450
}
452451
453-
.otherClass {
452+
.otherClassLocalFile {
454453
color: blue;
455454
}
456-
._2WZy6LhVz_KGVbdNCAmL6C {
457-
color: green;
455+
.classNameGlobalFile {
456+
color: black;
458457
}
459458
460-
._7mafm73rmgiKuViZtzNmn {
461-
color: blue;
459+
._2rcag09JpwrP4_hfyyRmm- {
460+
color: coral;
462461
}
463462
"
464463
`;
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.className {
2-
color: green;
1+
.classNameGlobalFile {
2+
color: black;
33
}
44

5-
:local(.otherClass) {
6-
color: blue;
5+
:local(.otherClassGlobalFile) {
6+
color: coral;
77
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.className {
1+
.classNameLocalFile {
22
color: green;
33
}
44

5-
:global(.otherClass) {
5+
:global(.otherClassLocalFile) {
66
color: blue;
77
}

test/modules-option.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ describe('"modules" option', () => {
570570
const compiler = getCompiler('./modules/issue-1063/issue-1063.js', {
571571
modules: {
572572
mode: (resourcePath) => {
573-
if (resourcePath === 'global.css') {
573+
if (/global.css$/i.test(resourcePath)) {
574574
return 'global';
575575
}
576576

0 commit comments

Comments
 (0)