Skip to content

Commit 711086f

Browse files
CurtisHumphreygajus
authored andcommitted
feat: add exclude option to filter file based on a RegExp pattern (#61)
1 parent 861d31c commit 711086f

File tree

8 files changed

+53
-8
lines changed

8 files changed

+53
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ NODE_ENV=production ./test
176176
|`filetypes`|Configure [postcss syntax loaders](https://github.com/postcss/postcss#syntaxes) like sugerss, LESS and SCSS. ||
177177
|`webpackHotModuleReloading`|Enables hot reloading of CSS in webpack|`false`|
178178
|`generateScopedName`|Refer to [Generating scoped names](https://github.com/css-modules/postcss-modules#generating-scoped-names)|`[path]___[name]__[local]___[hash:base64:5]`|
179+
|`exclude`| a RegExp that will exclude otherwise included files e.g., to exclude all styles from node_modules `exclude: 'node_modules'`|
179180

180181
Missing a configuration? [Raise an issue](https://github.com/gajus/babel-plugin-react-css-modules/issues/new?title=New%20configuration:).
181182

src/index.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,42 @@ export default ({
100100
firstNonImportDeclarationNode.insertBefore(hotAcceptStatement);
101101
};
102102

103+
const getTargetResourcePath = (path: Object, stats:Object) => {
104+
const targetFileDirectoryPath = dirname(stats.file.opts.filename);
105+
106+
if (path.node.source.value.startsWith('.')) {
107+
return resolve(targetFileDirectoryPath, path.node.source.value);
108+
}
109+
110+
return require.resolve(path.node.source.value);
111+
};
112+
113+
const notForPlugin = (path: Object, stats: Object) => {
114+
stats.opts.filetypes = stats.opts.filetypes || {};
115+
116+
const extension = path.node.source.value.lastIndexOf('.') > -1 ? path.node.source.value.substr(path.node.source.value.lastIndexOf('.')) : null;
117+
118+
if (extension !== '.css' && Object.keys(stats.opts.filetypes).indexOf(extension) < 0) {
119+
return true;
120+
}
121+
122+
if (stats.opts.exclude && getTargetResourcePath(path, stats).match(new RegExp(stats.opts.exclude))) {
123+
return true;
124+
}
125+
126+
return false;
127+
};
128+
103129
return {
104130
inherits: babelPluginJsxSyntax,
105131
visitor: {
106132
ImportDeclaration (path: Object, stats: Object): void {
107-
stats.opts.filetypes = stats.opts.filetypes || {};
108-
109-
const extension = path.node.source.value.lastIndexOf('.') > -1 ? path.node.source.value.substr(path.node.source.value.lastIndexOf('.')) : null;
110-
111-
if (extension !== '.css' && Object.keys(stats.opts.filetypes).indexOf(extension) < 0) {
133+
if (notForPlugin(path, stats)) {
112134
return;
113135
}
114136

115137
const filename = stats.file.opts.filename;
116-
const targetFileDirectoryPath = dirname(stats.file.opts.filename);
117-
118-
const targetResourcePath = path.node.source.value.startsWith('.') ? resolve(targetFileDirectoryPath, path.node.source.value) : require.resolve(path.node.source.value);
138+
const targetResourcePath = getTargetResourcePath(path, stats);
119139

120140
let styleImportName: string;
121141

src/schemas/optionsSchema.json

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
},
1919
"webpackHotModuleReloading": {
2020
"type": "boolean"
21+
},
22+
"exclude": {
23+
"type": "string"
2124
}
2225
},
2326
"type": "object"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './bar.css';
2+
import './not_me.css';
3+
4+
<div styleName="a"></div>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.a {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './bar.css';
2+
import './not_me.css';
3+
4+
<div className="bar__a"></div>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.other {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../src",
5+
{
6+
"generateScopedName": "[name]__[local]",
7+
"exclude": "not_me"
8+
}
9+
]
10+
]
11+
}

0 commit comments

Comments
 (0)