Skip to content

Added exclude option to filter file based on a RegExp pattern #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ NODE_ENV=production ./test
|`filetypes`|Configure [postcss syntax loaders](https://github.com/postcss/postcss#syntaxes) like sugerss, LESS and SCSS. ||
|`webpackHotModuleReloading`|Enables hot reloading of CSS in webpack|`false`|
|`generateScopedName`|Refer to [Generating scoped names](https://github.com/css-modules/postcss-modules#generating-scoped-names)|`[path]___[name]__[local]___[hash:base64:5]`|
|`exclude`| a RegExp that will exclude otherwise included files e.g., to exclude all styles from node_modules `exclude: 'node_modules'`|

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

Expand Down
36 changes: 28 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,42 @@ export default ({
firstNonImportDeclarationNode.insertBefore(hotAcceptStatement);
};

const getTargetResourcePath = (path: Object, stats:Object) => {
const targetFileDirectoryPath = dirname(stats.file.opts.filename);

if (path.node.source.value.startsWith('.')) {
return resolve(targetFileDirectoryPath, path.node.source.value);
}

return require.resolve(path.node.source.value);
};

const notForPlugin = (path: Object, stats: Object) => {
stats.opts.filetypes = stats.opts.filetypes || {};

const extension = path.node.source.value.lastIndexOf('.') > -1 ? path.node.source.value.substr(path.node.source.value.lastIndexOf('.')) : null;

if (extension !== '.css' && Object.keys(stats.opts.filetypes).indexOf(extension) < 0) {
return true;
}

if (stats.opts.exclude && getTargetResourcePath(path, stats).match(new RegExp(stats.opts.exclude))) {
return true;
}

return false;
};

return {
inherits: babelPluginJsxSyntax,
visitor: {
ImportDeclaration (path: Object, stats: Object): void {
stats.opts.filetypes = stats.opts.filetypes || {};

const extension = path.node.source.value.lastIndexOf('.') > -1 ? path.node.source.value.substr(path.node.source.value.lastIndexOf('.')) : null;

if (extension !== '.css' && Object.keys(stats.opts.filetypes).indexOf(extension) < 0) {
if (notForPlugin(path, stats)) {
return;
}

const filename = stats.file.opts.filename;
const targetFileDirectoryPath = dirname(stats.file.opts.filename);

const targetResourcePath = path.node.source.value.startsWith('.') ? resolve(targetFileDirectoryPath, path.node.source.value) : require.resolve(path.node.source.value);
const targetResourcePath = getTargetResourcePath(path, stats);

let styleImportName: string;

Expand Down
3 changes: 3 additions & 0 deletions src/schemas/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
},
"webpackHotModuleReloading": {
"type": "boolean"
},
"exclude": {
"type": "string"
}
},
"type": "object"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './bar.css';
import './not_me.css';

<div styleName="a"></div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './bar.css';
import './not_me.css';

<div className="bar__a"></div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.other {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"../../../../src",
{
"generateScopedName": "[name]__[local]",
"exclude": "not_me"
}
]
]
}