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 91c8e9b

Browse files
Feat/add regexp support for filetype (#240)
* feat: use RegExp as including condition in filetype config * test: add corresponding test cases. * fix: do not use extension-style filrtype as RegExp pattern * feat: modify the priority between RegExp pattern filetype and extension style filetype * style: no continue * style: for comment #2 Co-authored-by: super-cattle <xuxinhang4567#126.com>
1 parent e3636b3 commit 91c8e9b

File tree

17 files changed

+99
-8
lines changed

17 files changed

+99
-8
lines changed

‎src/conditionalClassMerge.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default (
1212
classNameExpression: any,
1313
styleNameExpression: any,
1414
): any => {
15+
// classNameExpression ? (classNameExpression + ' ') : '' + styleNameExpression
1516
return binaryExpression(
1617
'+',
1718
conditionalExpression(

‎src/findMatchedFiletype.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// @flow
2+
3+
export default (sourceFilePath: string, filetypes: $ReadOnlyArray<string>): ?string => {
4+
// Try to match as the RegExp pattern
5+
for (const pattern of filetypes) {
6+
if (!pattern.match(/^\.[a-z0-9A-Z]+?$/)) {
7+
if (sourceFilePath.match(new RegExp(pattern))) {
8+
return pattern;
9+
}
10+
}
11+
}
12+
13+
const extensionDotIndex = sourceFilePath.lastIndexOf('.');
14+
15+
if (extensionDotIndex > -1) {
16+
const extension = sourceFilePath.substr(extensionDotIndex);
17+
const index = filetypes.indexOf(extension);
18+
19+
if (index > -1) {
20+
return filetypes[index];
21+
}
22+
}
23+
24+
return null;
25+
};

‎src/getClassName.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export default (styleNameValue: string, styleModuleImportMap: StyleModuleImportM
120120
return getClassNameFromMultipleImports(styleName, styleModuleImportMap, handleMissingStyleName);
121121
}
122122

123+
// There is only one imported CSS module file.
123124
const styleModuleMap: StyleModuleMapType = styleModuleImportMap[styleModuleImportMapKeys[0]];
124125

125126
if (!styleModuleMap[styleName]) {

‎src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import requireCssModule from './requireCssModule';
1515
import resolveStringLiteral from './resolveStringLiteral';
1616
import replaceJsxExpressionContainer from './replaceJsxExpressionContainer';
1717
import attributeNameExists from './attributeNameExists';
18+
import findMatchedFiletype from './findMatchedFiletype';
1819
import createSpreadMapper from './createSpreadMapper';
1920
import handleSpreadClassName from './handleSpreadClassName';
2021

@@ -130,12 +131,12 @@ export default ({
130131
return filename.match(new RegExp(exclude));
131132
};
132133

134+
// decide whether the import statement should be processed as CSS module
133135
const notForPlugin = (path: *, stats: *) => {
134136
stats.opts.filetypes = stats.opts.filetypes || {};
135137

136-
const extension = path.node.source.value.lastIndexOf('.') > -1 ? path.node.source.value.substr(path.node.source.value.lastIndexOf('.')) : null;
137-
138-
if (extension !== '.css' && Object.keys(stats.opts.filetypes).indexOf(extension) < 0) {
138+
// @HACK
139+
if (path.node.source.value.indexOf('babel-plugin-react-css-modules') === 0) {
139140
return true;
140141
}
141142

@@ -145,7 +146,10 @@ export default ({
145146
return true;
146147
}
147148

148-
return false;
149+
const filetypeKeys = Object.keys(stats.opts.filetypes);
150+
filetypeKeys.push('.css');
151+
152+
return !findMatchedFiletype(filename, filetypeKeys);
149153
};
150154

151155
return {

‎src/requireCssModule.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
GenerateScopedNameConfigurationType,
1919
StyleModuleMapType
2020
} from './types';
21+
import findMatchedFiletype from './findMatchedFiletype';
2122
import optionsDefaults from './schemas/optionsDefaults';
2223

2324
type FiletypeOptionsType = {|
@@ -36,10 +37,9 @@ type OptionsType = {|
3637
|};
3738

3839
const getFiletypeOptions = (cssSourceFilePath: string, filetypes: FiletypesConfigurationType): ?FiletypeOptionsType => {
39-
const extension = cssSourceFilePath.substr(cssSourceFilePath.lastIndexOf('.'));
40-
const filetype = filetypes ? filetypes[extension] : null;
40+
const matchedKey = findMatchedFiletype(cssSourceFilePath, Object.keys(filetypes));
4141

42-
return filetype;
42+
return matchedKey ? filetypes&&filetypes[matchedKey] : null;
4343
};
4444

4545
// eslint-disable-next-line flowtype/no-weak-types

‎src/resolveStringLiteral.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export default (
4141
} else {
4242
throw new Error('Unexpected attribute value:' + destinationAttribute.value);
4343
}
44-
4544
path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(sourceAttribute), 1);
4645
} else {
4746
sourceAttribute.name.name = destinationName;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@color: #f00;
2+
3+
.a {background-color: @color;}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@color: #f00;
2+
3+
.a {background-color: @color;}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './bar.md.less';
2+
3+
<div styleName="a"></div>;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../src",
5+
{
6+
"generateScopedName": "[name]__[local]",
7+
"filetypes": {
8+
"\\.md\\.less$": {
9+
"syntax": "postcss-less"
10+
}
11+
}
12+
}
13+
]
14+
]
15+
}

0 commit comments

Comments
(0)

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