Skip to content

Commit b0bf485

Browse files
committed
chore: support custom autoModules
1 parent 71593d9 commit b0bf485

9 files changed

Lines changed: 146 additions & 9 deletions

File tree

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,50 @@ Enable CSS modules or set options for `postcss-modules`.
144144

145145
### autoModules
146146

147-
Type: `boolean`<br>
147+
Type: `boolean` `function` `RegExp`<br>
148148
Default: `true`
149149

150-
Automatically enable CSS modules for `.module.css` `.module.sss` `.module.scss` `.module.sass` `.module.styl` `.module.stylus` `.module.less` files.
150+
Allows auto enable CSS modules based on filename.
151+
152+
Possible values:
153+
154+
* `true` - enable CSS modules for `.module.css` `.module.sss` `.module.scss` `.module.sass` `.module.styl` `.module.stylus` `.module.less` files.
155+
* `false` - disables CSS Modules.
156+
* `RegExp` - enable CSS modules for all files matching /RegExp/i.test(filename) regexp.
157+
* `function` - enable CSS Modules for files based on the filename satisfying your filter function check.
158+
159+
#### `boolean`
160+
161+
Possible values:
162+
163+
* `true` - enable CSS modules for `.module.css` `.module.sss` `.module.scss` `.module.sass` `.module.styl` `.module.stylus` `.module.less` files.
164+
* `false` - disables CSS Modules.
165+
166+
```js
167+
postcss({
168+
autoModules: true,
169+
})
170+
```
171+
172+
#### `RegExp`
173+
174+
Enable CSS modules for all files matching /RegExp/i.test(filename) regexp.
175+
176+
```js
177+
postcss({
178+
autoModules: /\.custom-module\.\w+$/i,
179+
})
180+
```
181+
182+
#### `function`
183+
184+
Enable CSS modules for files based on the filename satisfying your filter function check.
185+
186+
```js
187+
postcss({
188+
autoModules: (file) => file.endsWith(".custom-module.css"),
189+
})
190+
```
151191

152192
### namedExports
153193

src/postcss-loader.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ function isModuleFile(file) {
5252
return /\.module\.[a-z]{2,6}$/.test(file)
5353
}
5454

55+
function isAutoModule(autoModules, file) {
56+
if (autoModules === false) {
57+
return false
58+
}
59+
60+
if (autoModules === true || autoModules === undefined) {
61+
return isModuleFile(file)
62+
}
63+
64+
if (typeof autoModules === 'function') {
65+
return autoModules(file)
66+
}
67+
68+
if (autoModules instanceof RegExp) {
69+
return autoModules.test(file)
70+
}
71+
72+
return false
73+
}
74+
5575
/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */
5676
export default {
5777
name: 'postcss',
@@ -72,9 +92,8 @@ export default {
7292
const shouldInject = options.inject
7393

7494
const modulesExported = {}
75-
const autoModules = options.autoModules !== false && options.onlyModules !== true
76-
const isAutoModule = autoModules && isModuleFile(this.id)
77-
const supportModules = autoModules ? isAutoModule : options.modules
95+
const onlyModules = options.onlyModules !== true
96+
const supportModules = onlyModules ? isAutoModule(options.autoModules, this.id) : options.modules
7897
if (supportModules) {
7998
plugins.unshift(
8099
require('postcss-modules')({

test/__snapshots__/index.test.js.snap

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,54 @@ console.log(
712712
"
713713
`;
714714

715+
exports[`modules custom-auto-modules: js code 1`] = `
716+
"'use strict';
717+
718+
function styleInject(css, ref) {
719+
if ( ref === void 0 ) ref = {};
720+
var insertAt = ref.insertAt;
721+
722+
if (!css || typeof document === 'undefined') { return; }
723+
724+
var head = document.head || document.getElementsByTagName('head')[0];
725+
var style = document.createElement('style');
726+
style.type = 'text/css';
727+
728+
if (insertAt === 'top') {
729+
if (head.firstChild) {
730+
head.insertBefore(style, head.firstChild);
731+
} else {
732+
head.appendChild(style);
733+
}
734+
} else {
735+
head.appendChild(style);
736+
}
737+
738+
if (style.styleSheet) {
739+
style.styleSheet.cssText = css;
740+
} else {
741+
style.appendChild(document.createTextNode(css));
742+
}
743+
}
744+
745+
var css_248z$2 = \\".a-test_foo {\\\\n color: red;\\\\n}\\\\n\\";
746+
var a = {\\"foo\\":\\"a-test_foo\\"};
747+
styleInject(css_248z$2);
748+
749+
var css_248z$1 = \\".b {\\\\n color: red; }\\\\n\\";
750+
styleInject(css_248z$1);
751+
752+
var css_248z = \\".c {\\\\n color: red;\\\\n}\\\\n\\";
753+
styleInject(css_248z);
754+
755+
console.log(
756+
a,
757+
css_248z$1,
758+
css_248z
759+
);
760+
"
761+
`;
762+
715763
exports[`modules extract: css code 1`] = `
716764
".style_foo {
717765
color: red;
@@ -796,11 +844,10 @@ function styleInject(css, ref) {
796844
}
797845
}
798846
799-
var css_248z = \\".style_foo {\\\\n color: red;\\\\n}\\\\n\\";
800-
var style = {\\"foo\\":\\"style_foo\\"};
847+
var css_248z = \\".foo {\\\\n color: red;\\\\n}\\\\n\\";
801848
styleInject(css_248z);
802849
803-
console.log(style.foo);
850+
console.log(css_248z.foo);
804851
"
805852
`;
806853

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.b {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.c {
2+
color: red;
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import a from './a.test.css'
2+
import b from './b.test.scss'
3+
import c from './c.test.less'
4+
5+
console.log(
6+
a,
7+
b,
8+
c
9+
)

test/index.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,19 @@ snapshotMany('modules', [
259259
{
260260
title: 'auto-modules',
261261
input: 'auto-modules/index.js'
262+
},
263+
{
264+
title: 'custom-auto-modules',
265+
input: 'custom-auto-modules/index.js',
266+
options: {
267+
autoModules: filename => {
268+
if (/\.test\.[cs|le]s+/.test(filename)) {
269+
return true
270+
}
271+
272+
return false
273+
}
274+
}
262275
}
263276
])
264277

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type PostCSSPluginConf = {
2020
modules?: boolean | Record<string, any>;
2121
extensions?: string[];
2222
plugins?: any[];
23-
autoModules?: boolean;
23+
autoModules?: boolean | RegExp | ((file: string) => boolean);
2424
namedExports?: boolean | ((id: string) => string);
2525
minimize?: boolean | any;
2626
parser?: string | FunctionType;

0 commit comments

Comments
 (0)