diff --git a/README.md b/README.md index 888f48e..d022e96 100644 --- a/README.md +++ b/README.md @@ -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:). diff --git a/src/index.js b/src/index.js index 4dbedb7..153261d 100644 --- a/src/index.js +++ b/src/index.js @@ -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; diff --git a/src/schemas/optionsSchema.json b/src/schemas/optionsSchema.json index 8b43606..9acd45c 100644 --- a/src/schemas/optionsSchema.json +++ b/src/schemas/optionsSchema.json @@ -18,6 +18,9 @@ }, "webpackHotModuleReloading": { "type": "boolean" + }, + "exclude": { + "type": "string" } }, "type": "object" diff --git a/test/fixtures/react-css-modules/exclude styles from react-css-modules/actual.js b/test/fixtures/react-css-modules/exclude styles from react-css-modules/actual.js new file mode 100644 index 0000000..d194ffc --- /dev/null +++ b/test/fixtures/react-css-modules/exclude styles from react-css-modules/actual.js @@ -0,0 +1,4 @@ +import './bar.css'; +import './not_me.css'; + +
; diff --git a/test/fixtures/react-css-modules/exclude styles from react-css-modules/bar.css b/test/fixtures/react-css-modules/exclude styles from react-css-modules/bar.css new file mode 100644 index 0000000..6b087b6 --- /dev/null +++ b/test/fixtures/react-css-modules/exclude styles from react-css-modules/bar.css @@ -0,0 +1 @@ +.a {} diff --git a/test/fixtures/react-css-modules/exclude styles from react-css-modules/expected.js b/test/fixtures/react-css-modules/exclude styles from react-css-modules/expected.js new file mode 100644 index 0000000..74c2862 --- /dev/null +++ b/test/fixtures/react-css-modules/exclude styles from react-css-modules/expected.js @@ -0,0 +1,4 @@ +import './bar.css'; +import './not_me.css'; + +
; diff --git a/test/fixtures/react-css-modules/exclude styles from react-css-modules/not_me.css b/test/fixtures/react-css-modules/exclude styles from react-css-modules/not_me.css new file mode 100644 index 0000000..89b44e3 --- /dev/null +++ b/test/fixtures/react-css-modules/exclude styles from react-css-modules/not_me.css @@ -0,0 +1 @@ +.other {} diff --git a/test/fixtures/react-css-modules/exclude styles from react-css-modules/options.json b/test/fixtures/react-css-modules/exclude styles from react-css-modules/options.json new file mode 100644 index 0000000..9963217 --- /dev/null +++ b/test/fixtures/react-css-modules/exclude styles from react-css-modules/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "../../../../src", + { + "generateScopedName": "[name]__[local]", + "exclude": "not_me" + } + ] + ] +}