Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fcb91f

Browse files
AlbertLuciantogajus
authored andcommitted
feat: do not apply plug-in if file does not contain styleName attribute (#230)
* feat: add skipAbsentStyleName * test: add test case for skipAbsentStyleName * feat: refactor skipAbsentStyleName traversal * fix: readd require.resolve for resource path * feat: update options naming * docs: update for skip option * refactor: traverse JSXAttribute instead of element
1 parent d27306c commit 9fcb91f

File tree

8 files changed

+67
-1
lines changed

8 files changed

+67
-1
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Configure the options for the plugin within your `.babelrc` as follows:
198198
|`webpackHotModuleReloading`|`boolean`|Enables hot reloading of CSS in webpack|`false`|
199199
|`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"`|
200200
|`attributeNames`|`?AttributeNameMapType`|Refer to [Custom Attribute Mapping](#custom-attribute-mapping)|`{"styleName": "className"}`|
201+
|`skip`|`boolean`|Whether to apply plugin if no matching `attributeNames` found in the file|`false`|
201202

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

‎src/attributeNameExists.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @flow
2+
3+
import optionsDefaults from './schemas/optionsDefaults';
4+
5+
const attributeNameExists = (programPath: *, stats: *): boolean => {
6+
let exists = false;
7+
8+
let attributeNames = optionsDefaults.attributeNames;
9+
10+
if (stats.opts && stats.opts.attributeNames) {
11+
attributeNames = Object.assign({}, attributeNames, stats.opts.attributeNames);
12+
}
13+
14+
programPath.traverse({
15+
JSXAttribute (attrPath: *) {
16+
if (exists) {
17+
return;
18+
}
19+
20+
const attribute = attrPath.node;
21+
22+
if (typeof attribute.name !== 'undefined' && typeof attributeNames[attribute.name.name] === 'string') {
23+
exists = true;
24+
}
25+
}
26+
});
27+
28+
return exists;
29+
};
30+
31+
export default attributeNameExists;

‎src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import createObjectExpression from './createObjectExpression';
1414
import requireCssModule from './requireCssModule';
1515
import resolveStringLiteral from './resolveStringLiteral';
1616
import replaceJsxExpressionContainer from './replaceJsxExpressionContainer';
17+
import attributeNameExists from './attributeNameExists';
1718

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

35+
let skip = false;
36+
3437
const setupFileForRuntimeResolution = (path, filename) => {
3538
const programPath = path.findParent((parentPath) => {
3639
return parentPath.isProgram();
@@ -147,7 +150,7 @@ export default ({
147150
inherits: babelPluginJsxSyntax,
148151
visitor: {
149152
ImportDeclaration (path: *, stats: *): void {
150-
if (notForPlugin(path, stats)) {
153+
if (skip||notForPlugin(path, stats)) {
151154
return;
152155
}
153156

@@ -183,6 +186,10 @@ export default ({
183186
}
184187
},
185188
JSXElement (path: *, stats: *): void {
189+
if (skip) {
190+
return;
191+
}
192+
186193
const filename = stats.file.opts.filename;
187194

188195
if (stats.opts.exclude && isFilenameExcluded(filename, stats.opts.exclude)) {
@@ -250,6 +257,10 @@ export default ({
250257
filenameMap[filename] = {
251258
styleModuleImportMap: {}
252259
};
260+
261+
if (stats.opts.skip && !attributeNameExists(path, stats)) {
262+
skip = true;
263+
}
253264
}
254265
}
255266
};

‎src/schemas/optionsSchema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
}
7070
},
7171
"type": "object"
72+
},
73+
"skip": {
74+
"type": "boolean"
7275
}
7376
},
7477
"type": "object"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.a {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'bar.css';
2+
3+
<div className="any" />;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../src",
5+
{
6+
"generateScopedName": "[name]__[local]",
7+
"skip": true
8+
}
9+
]
10+
]
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
require("bar.css");
4+
5+
<div className="any" />;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /