Skip to content

Commit d641c4d

Browse files
feat: pass the resourceQuery and resourceFragment to the auto and mode callback (#1569)
1 parent 3924679 commit d641c4d

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

README.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,19 @@ module.exports = {
605605
Type:
606606

607607
```ts
608-
type auto = boolean | regExp | ((resourcePath: string) => boolean);
608+
type auto =
609+
| boolean
610+
| regExp
611+
| ((
612+
resourcePath: string,
613+
resourceQuery: string,
614+
resourceFragment: string
615+
) => boolean);
609616
```
610617

611618
Default: `undefined`
612619

613-
Allows auto enable CSS modules/ICSS based on filename when `modules` option is object.
620+
Allows auto enable CSS modules/ICSS based on the filename, query or fragment when `modules` option is object.
614621

615622
Possible values:
616623

@@ -673,7 +680,7 @@ module.exports = {
673680

674681
###### `function`
675682

676-
Enable CSS modules for files based on the filename satisfying your filter function check.
683+
Enable CSS modules for files based on the filename, query or fragment satisfying your filter function check.
677684

678685
**webpack.config.js**
679686

@@ -686,7 +693,9 @@ module.exports = {
686693
loader: "css-loader",
687694
options: {
688695
modules: {
689-
auto: (resourcePath) => resourcePath.endsWith(".custom-module.css"),
696+
auto: (resourcePath, resourceQuery, resourceFragment) => {
697+
return resourcePath.endsWith(".custom-module.css");
698+
},
690699
},
691700
},
692701
},
@@ -705,7 +714,11 @@ type mode =
705714
| "global"
706715
| "pure"
707716
| "icss"
708-
| ((resourcePath: string) => "local" | "global" | "pure" | "icss");
717+
| ((
718+
resourcePath: string,
719+
resourceQuery: string,
720+
resourceFragment: string
721+
) => "local" | "global" | "pure" | "icss");
709722
```
710723

711724
Default: `'local'`
@@ -745,7 +758,7 @@ module.exports = {
745758

746759
###### `function`
747760

748-
Allows set different values for the `mode` option based on a filename
761+
Allows set different values for the `mode` option based on the filename, query or fragment.
749762

750763
Possible return values - `local`, `global`, `pure` and `icss`.
751764

@@ -761,7 +774,7 @@ module.exports = {
761774
options: {
762775
modules: {
763776
// Callback must return "local", "global", or "pure" values
764-
mode: (resourcePath) => {
777+
mode: (resourcePath, resourceQuery, resourceFragment) => {
765778
if (/pure.css$/i.test(resourcePath)) {
766779
return "pure";
767780
}

src/utils.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -646,15 +646,24 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
646646
return false;
647647
}
648648
} else if (typeof modulesOptions.auto === "function") {
649-
const isModule = modulesOptions.auto(resourcePath);
649+
const { resourceQuery, resourceFragment } = loaderContext;
650+
const isModule = modulesOptions.auto(
651+
resourcePath,
652+
resourceQuery,
653+
resourceFragment
654+
);
650655

651656
if (!isModule) {
652657
return false;
653658
}
654659
}
655660

656661
if (typeof modulesOptions.mode === "function") {
657-
modulesOptions.mode = modulesOptions.mode(loaderContext.resourcePath);
662+
modulesOptions.mode = modulesOptions.mode(
663+
loaderContext.resourcePath,
664+
loaderContext.resourceQuery,
665+
loaderContext.resourceFragment
666+
);
658667
}
659668

660669
if (needNamedExport) {

test/modules-option.test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,11 @@ describe('"modules" option', () => {
834834
it("issue #1063", async () => {
835835
const compiler = getCompiler("./modules/issue-1063/issue-1063.js", {
836836
modules: {
837-
mode: (resourcePath) => {
837+
mode: (resourcePath, resourceQuery, resourceFragment) => {
838+
expect(resourcePath).toBeDefined();
839+
expect(resourceQuery).toBeDefined();
840+
expect(resourceFragment).toBeDefined();
841+
838842
if (/pure.css$/i.test(resourcePath)) {
839843
return "pure";
840844
}
@@ -1269,7 +1273,13 @@ describe('"modules" option', () => {
12691273
it('should work with a modules.auto Function that returns "true"', async () => {
12701274
const compiler = getCompiler("./modules/mode/modules.js", {
12711275
modules: {
1272-
auto: (relativePath) => relativePath.endsWith("module.css"),
1276+
auto: (resourcePath, resourceQuery, resourceFragment) => {
1277+
expect(resourcePath).toBeDefined();
1278+
expect(resourceQuery).toBeDefined();
1279+
expect(resourceFragment).toBeDefined();
1280+
1281+
return resourcePath.endsWith("module.css");
1282+
},
12731283
},
12741284
});
12751285
const stats = await compile(compiler);

0 commit comments

Comments
 (0)