Skip to content

Feat/add regexp support for filetype #240

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
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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 src/conditionalClassMerge.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default (
classNameExpression: any,
styleNameExpression: any,
): any => {
// classNameExpression ? (classNameExpression + ' ') : '' + styleNameExpression
return binaryExpression(
'+',
conditionalExpression(
Expand Down
25 changes: 25 additions & 0 deletions src/findMatchedFiletype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @flow

export default (sourceFilePath: string, filetypes: $ReadOnlyArray<string>): ?string => {
// Try to match as the RegExp pattern
for (const pattern of filetypes) {
if (!pattern.match(/^\.[a-z0-9A-Z]+?$/)) {
if (sourceFilePath.match(new RegExp(pattern))) {
return pattern;
}
}
}

const extensionDotIndex = sourceFilePath.lastIndexOf('.');

if (extensionDotIndex > -1) {
const extension = sourceFilePath.substr(extensionDotIndex);
const index = filetypes.indexOf(extension);

if (index > -1) {
return filetypes[index];
}
}

return null;
};
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import requireCssModule from './requireCssModule';
import resolveStringLiteral from './resolveStringLiteral';
import replaceJsxExpressionContainer from './replaceJsxExpressionContainer';
import attributeNameExists from './attributeNameExists';
import findMatchedFiletype from './findMatchedFiletype';

const ajv = new Ajv({
// eslint-disable-next-line id-match
Expand Down Expand Up @@ -131,9 +132,8 @@ export default ({
const notForPlugin = (path: *, stats: *) => {
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) {
// @HACK
if (path.node.source.value.indexOf('babel-plugin-react-css-modules') === 0) {
return true;
}

Expand All @@ -143,6 +143,13 @@ export default ({
return true;
}

const filetypeKeys = Object.keys(stats.opts.filetypes);

filetypeKeys.push('.css');
if (!findMatchedFiletype(filename, filetypeKeys)) {
return true;
}

return false;
};

Expand Down
6 changes: 3 additions & 3 deletions src/requireCssModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
GenerateScopedNameConfigurationType,
StyleModuleMapType
} from './types';
import findMatchedFiletype from './findMatchedFiletype';
import optionsDefaults from './schemas/optionsDefaults';

type FiletypeOptionsType = {|
Expand All @@ -36,10 +37,9 @@ type OptionsType = {|
|};

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

return filetype;
return matchedKey ? filetypes && filetypes[matchedKey] : null;
};

// eslint-disable-next-line flowtype/no-weak-types
Expand Down
1 change: 0 additions & 1 deletion src/resolveStringLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default (
} else {
throw new Error('Unexpected attribute value:' + destinationAttribute.value);
}

path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(sourceAttribute), 1);
} else {
sourceAttribute.name.name = destinationName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@color: #f00;

.a {background-color: @color;}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@color: #f00;

.a {background-color: @color;}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './bar.md.less';

<div styleName="a"></div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"plugins": [
[
"../../../../src",
{
"generateScopedName": "[name]__[local]",
"filetypes": {
"\\.md\\.less$": {
"syntax": "postcss-less"
}
}
}
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

require("./bar.md.less");

<div className="bar-md__a"></div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a {background-color: #f00;}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a {background-color: #f00;}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import barMd from './bar.md.css';
import base from './styles/base.css';

<div styleName="barMd.a base.b"></div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"plugins": [
[
"../../../../src",
{
"generateScopedName": "[name]__[local]",
"filetypes": {
"\\.md\\.less$": {
"syntax": "postcss-less"
},
"styles/.*?\\.css$": {}
}
}
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var _barMd = _interopRequireDefault(require("./bar.md.css"));

var _base = _interopRequireDefault(require("./styles/base.css"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

<div className="bar-md__a base__b"></div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.b {background-color: #0f0;}