Skip to content

feat: do not apply plug-in if file does not contain styleName attribute #230

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 7 commits into from
Feb 21, 2019
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 @@ -198,6 +198,7 @@ Configure the options for the plugin within your `.babelrc` as follows:
|`webpackHotModuleReloading`|`boolean`|Enables hot reloading of CSS in webpack|`false`|
|`handleMissingStyleName`|`"throw"`, `"warn"`, `"ignore"`|Determines what should be done for undefined CSS modules (using a `styleName` for which there is no CSS module defined). Setting this option to `"ignore"` is equivalent to setting `errorWhenNotFound: false` in [react-css-modules](https://github.com/gajus/react-css-modules#errorwhennotfound). |`"throw"`|
|`attributeNames`|`?AttributeNameMapType`|Refer to [Custom Attribute Mapping](#custom-attribute-mapping)|`{"styleName": "className"}`|
|`skip`|`boolean`|Whether to apply plugin if no matching `attributeNames` found in the file|`false`|

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

Expand Down
31 changes: 31 additions & 0 deletions src/attributeNameExists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @flow

import optionsDefaults from './schemas/optionsDefaults';

const attributeNameExists = (programPath: *, stats: *): boolean => {
let exists = false;

let attributeNames = optionsDefaults.attributeNames;

if (stats.opts && stats.opts.attributeNames) {
attributeNames = Object.assign({}, attributeNames, stats.opts.attributeNames);
}

programPath.traverse({
JSXAttribute (attrPath: *) {
if (exists) {
return;
}

const attribute = attrPath.node;

if (typeof attribute.name !== 'undefined' && typeof attributeNames[attribute.name.name] === 'string') {
exists = true;
}
}
});

return exists;
};

export default attributeNameExists;
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import createObjectExpression from './createObjectExpression';
import requireCssModule from './requireCssModule';
import resolveStringLiteral from './resolveStringLiteral';
import replaceJsxExpressionContainer from './replaceJsxExpressionContainer';
import attributeNameExists from './attributeNameExists';

const ajv = new Ajv({
// eslint-disable-next-line id-match
Expand All @@ -31,6 +32,8 @@ export default ({
}) => {
const filenameMap = {};

let skip = false;

const setupFileForRuntimeResolution = (path, filename) => {
const programPath = path.findParent((parentPath) => {
return parentPath.isProgram();
Expand Down Expand Up @@ -147,7 +150,7 @@ export default ({
inherits: babelPluginJsxSyntax,
visitor: {
ImportDeclaration (path: *, stats: *): void {
if (notForPlugin(path, stats)) {
if (skip || notForPlugin(path, stats)) {
return;
}

Expand Down Expand Up @@ -183,6 +186,10 @@ export default ({
}
},
JSXElement (path: *, stats: *): void {
if (skip) {
return;
}

const filename = stats.file.opts.filename;

if (stats.opts.exclude && isFilenameExcluded(filename, stats.opts.exclude)) {
Expand Down Expand Up @@ -250,6 +257,10 @@ export default ({
filenameMap[filename] = {
styleModuleImportMap: {}
};

if (stats.opts.skip && !attributeNameExists(path, stats)) {
skip = true;
}
}
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/schemas/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
}
},
"type": "object"
},
"skip": {
"type": "boolean"
}
},
"type": "object"
Expand Down
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,3 @@
import 'bar.css';

<div className="any" />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"../../../../src",
{
"generateScopedName": "[name]__[local]",
"skip": true
}
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

require("bar.css");

<div className="any" />;