Skip to content

Commit f343ab1

Browse files
jcdekoninggajus
authored andcommitted
Feature: having postcss syntax configurable (#10)
* Make syntax parser configurable * Make syntax parser configurable * fix: handle multiple file import with dynamic resolution (fixes #7 #11 #13 #14 #17) (#12) * fix: added escape hatch when testing attribute that do not have a name property (#11) * fix: object expression generated on dynamic resolution now use quote to escape properties' name (#14) * fix: getClassName now works as expected when multiple file use runtime resolution * fix: getClassName now works as expected when multiple file use runtime resolution * fix: generate className when using runtime resolution (#18) (#19) * fix: added escape hatch when testing attribute that do not have a name property (#11) * fix: object expression generated on dynamic resolution now use quote to escape properties' name (#14) * fix: getClassName now works as expected when multiple file use runtime resolution * fix: getClassName now works as expected when multiple file use runtime resolution * fix: className attribute is now correctly generated when using runtime resolution (#18) * feat: improve error messages (#21) * Make syntax parser configurable * Merged upstream * Updated the configuration section of the readme file * docs: improve wording * fix: add a missing comma BREAKING CHANGE: If your installation is using CSS, now you need to manually install scss-loader and configure it. https://github.com/gajus/babel-plugin-react-css-modules#configurate-syntax-loaders
1 parent acbb2e8 commit f343ab1

File tree

9 files changed

+65
-12
lines changed

9 files changed

+65
-12
lines changed

.flowconfig

+3
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

+21-1
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
* [Example transpilations](#example-transpilations)
2526
* [Anonymous `styleName` resolution](#anonymous-stylename-resolution)
@@ -167,14 +168,33 @@ NODE_ENV=production ./test
167168

168169
|Name|Description|Default|
169170
|---|---|---|
170-
|`generateScopedName`|Refer to [Generating scoped names](https://github.com/css-modules/postcss-modules#generating-scoped-names)|`[path]___[name]__[local]___[hash:base64:5]`|
171171
|`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()`|
172+
|`filetypes`|Configure [postcss syntax loaders](https://github.com/postcss/postcss#syntaxes) like sugerss, LESS and SCSS. ||
173+
|`generateScopedName`|Refer to [Generating scoped names](https://github.com/css-modules/postcss-modules#generating-scoped-names)|`[path]___[name]__[local]___[hash:base64:5]`|
172174

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

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

180200
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

+4-3
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

+6-1
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

@@ -84,6 +88,7 @@ export default ({
8488
}
8589

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

src/requireCssModule.js

+9-7
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
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './bar.less';
2+
3+
<div styleName="a"></div>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@color: #f00;
2+
3+
.a {background-color: @color;}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './bar.less';
2+
3+
<div className="bar__a"></div>;
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)