Skip to content

Commit 6b7afc9

Browse files
committed
Merge branch 'master' of github.com:gajus/babel-plugin-react-css-modules
2 parents b78a508 + f343ab1 commit 6b7afc9

File tree

9 files changed

+65
-12
lines changed

9 files changed

+65
-12
lines changed

.flowconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
<PROJECT_ROOT>/node_modules/config-chain/test/broken.json
33
<PROJECT_ROOT>/node_modules/conventional-changelog-core/test/fixtures/_malformation.json
44
<PROJECT_ROOT>/node_modules/npmconf/test/fixtures/package.json
5+
6+
[options]
7+
module.ignore_non_literal_requires=true

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ In contrast to [`react-css-modules`](https://github.com/gajus/react-css-modules)
2020
* [Anonymous reference](#anonymous-reference)
2121
* [Named reference](#named-reference)
2222
* [Configuration](#configuration)
23+
* [Configurate syntax loaders](#configurate-syntax-loaders)
2324
* [Installation](#installation)
2425
* [Demo](#demo)
2526
* [Example transpilations](#example-transpilations)
@@ -168,14 +169,33 @@ NODE_ENV=production ./test
168169

169170
|Name|Description|Default|
170171
|---|---|---|
171-
|`generateScopedName`|Refer to [Generating scoped names](https://github.com/css-modules/postcss-modules#generating-scoped-names)|`[path]___[name]__[local]___[hash:base64:5]`|
172172
|`context`|Must match webpack [`context`](https://webpack.github.io/docs/configuration.html#context) configuration. [`css-loader`](https://github.com/webpack/css-loader) inherits `context` values from webpack. Other CSS module implementations might use different context resolution logic.|`process.cwd()`|
173+
|`filetypes`|Configure [postcss syntax loaders](https://github.com/postcss/postcss#syntaxes) like sugerss, LESS and SCSS. ||
174+
|`generateScopedName`|Refer to [Generating scoped names](https://github.com/css-modules/postcss-modules#generating-scoped-names)|`[path]___[name]__[local]___[hash:base64:5]`|
173175

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

176178
> Note:
177179
> The default configuration should work out of the box with the [css-loader](https://github.com/webpack/css-loader).
178180
181+
### Configurate syntax loaders
182+
183+
To add support for different CSS syntaxes (e.g. SCSS), perform the following two steps:
184+
185+
1. Add the [postcss syntax loader](https://github.com/postcss/postcss#syntaxes) as a development dependency:
186+
187+
```bash
188+
npm install postcss-scss --save-dev
189+
```
190+
191+
2. Add a filetype syntax mapping to the Babel plugin configuration
192+
193+
```json
194+
"filetypes": {
195+
".scss": "postcss-scss"
196+
}
197+
```
198+
179199
## Installation
180200

181201
When `babel-plugin-react-css-modules` cannot resolve CSS module at a compile time, it imports a helper function (read [Runtime `styleName` resolution](#runtime-stylename-resolution)). Therefore, you must install `babel-plugin-react-css-modules` as a direct dependency of the project.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"postcss-modules-local-by-default": "^1.1.1",
1515
"postcss-modules-parser": "^1.1.0",
1616
"postcss-modules-scope": "^1.0.2",
17-
"postcss-modules-values": "^1.2.2",
18-
"postcss-scss": "^0.4.0"
17+
"postcss-modules-values": "^1.2.2"
1918
},
2019
"description": "Transforms styleName to className using compile time CSS module resolution.",
2120
"devDependencies": {
@@ -31,7 +30,9 @@
3130
"flow-bin": "^0.37.4",
3231
"husky": "^0.12.0",
3332
"mocha": "^3.2.0",
34-
"semantic-release": "^6.3.5"
33+
"semantic-release": "^6.3.5",
34+
"postcss-less": "^0.15.0",
35+
"postcss-scss": "^0.4.0"
3536
},
3637
"engines": {
3738
"node": ">5.0.0"

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ export default ({
6161
inherits: babelPluginJsxSyntax,
6262
visitor: {
6363
ImportDeclaration (path: Object, stats: Object): void {
64-
if (!path.node.source.value.endsWith('.css') && !path.node.source.value.endsWith('.scss')) {
64+
stats.opts.filetypes = stats.opts.filetypes || {};
65+
66+
const extension = path.node.source.value.lastIndexOf('.') > -1 ? path.node.source.value.substr(path.node.source.value.lastIndexOf('.')) : null;
67+
68+
if (extension !== '.css' && Object.keys(stats.opts.filetypes).indexOf(extension) < 0) {
6569
return;
6670
}
6771

@@ -85,6 +89,7 @@ export default ({
8589

8690
filenameMap[filename].styleModuleImportMap[styleImportName] = requireCssModule(targetResourcePath, {
8791
context: stats.opts.context,
92+
filetypes: stats.opts.filetypes || {},
8893
generateScopedName: stats.opts.generateScopedName
8994
});
9095
},

src/requireCssModule.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ import LocalByDefault from 'postcss-modules-local-by-default';
1414
import Parser from 'postcss-modules-parser';
1515
import Scope from 'postcss-modules-scope';
1616
import Values from 'postcss-modules-values';
17-
import ScssSyntax from 'postcss-scss';
1817
import type {
1918
StyleModuleMapType
2019
} from './types';
2120

22-
const getTokens = (runner, cssSourceFilePath: string): StyleModuleMapType => {
23-
const sourceFilePathIsScss = cssSourceFilePath.endsWith('.scss');
21+
const getTokens = (runner, cssSourceFilePath: string, filetypes): StyleModuleMapType => {
22+
const extension = cssSourceFilePath.substr(cssSourceFilePath.lastIndexOf('.'));
23+
const syntax = filetypes[extension];
2424

2525
const options: Object = {
2626
from: cssSourceFilePath
2727
};
2828

29-
if (sourceFilePathIsScss) {
30-
options.syntax = ScssSyntax;
29+
if (syntax) {
30+
// eslint-disable-next-line import/no-dynamic-require, global-require
31+
options.syntax = require(syntax);
3132
}
3233

3334
const lazyResult = runner
@@ -44,6 +45,7 @@ const getTokens = (runner, cssSourceFilePath: string): StyleModuleMapType => {
4445
};
4546

4647
type OptionsType = {|
48+
filetypes: Object,
4749
generateScopedName?: string,
4850
context?: string
4951
|};
@@ -60,7 +62,7 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap
6062
const fromDirectoryPath = dirname(from);
6163
const toPath = resolve(fromDirectoryPath, to);
6264

63-
return getTokens(runner, toPath);
65+
return getTokens(runner, toPath, options.filetypes);
6466
};
6567

6668
const plugins = [
@@ -77,5 +79,5 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap
7779

7880
runner = postcss(plugins);
7981

80-
return getTokens(runner, cssSourceFilePath);
82+
return getTokens(runner, cssSourceFilePath, options.filetypes);
8183
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './bar.less';
2+
3+
<div styleName="a"></div>;
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.less';
2+
3+
<div className="bar__a"></div>;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../../src",
5+
{
6+
"generateScopedName": "[name]__[local]",
7+
"filetypes": {
8+
".less": "postcss-less"
9+
}
10+
}
11+
]
12+
]
13+
}

0 commit comments

Comments
 (0)