Skip to content

Commit

Permalink
Add option to transform classes to camelCase (#82)
Browse files Browse the repository at this point in the history
* Add option to transform classes to camelCase
  • Loading branch information
igor-ribeiro authored and madyankin committed Jul 20, 2018
1 parent 38a8bab commit 95a02b8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -170,6 +170,25 @@ postcss([
]);
```

### Camel cased classes

If you need, you can pass the options `{ camelCase: true }` to transform classes:

CSS:
```css
.post-title {
color: red;
}
```

JSON:
```json
{
"post-title": "._post-title_116zl_1",
"postTitle": "._post-title_116zl_1"
}
```


## Integration with templates
The plugin only transforms CSS classes to CSS modules.
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"css-modules-loader-core": "^1.1.0",
"generic-names": "^1.0.2",
"lodash.camelcase": "^4.3.0",
"postcss": "^6.0.1",
"string-hash": "^1.1.1"
},
Expand Down
10 changes: 10 additions & 0 deletions src/index.js
@@ -1,4 +1,5 @@
import postcss from 'postcss';
import camelCase from 'lodash.camelcase';
import Parser from 'css-modules-loader-core/lib/parser';
import FileSystemLoader from 'css-modules-loader-core/lib/file-system-loader';
import genericNames from 'generic-names';
Expand Down Expand Up @@ -77,6 +78,15 @@ module.exports = postcss.plugin(PLUGIN_NAME, (opts = {}) => {
const out = loader.finalSource;
if (out) css.prepend(out);

if (opts.camelCase === true) {
Object.keys(parser.exportTokens).forEach((token) => {
const camelCaseToken = camelCase(token);

parser.exportTokens[camelCaseToken] = parser.exportTokens[token];
});
}


// getJSON may return a promise
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
};
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/in/camelCase.css
@@ -0,0 +1,3 @@
.camel-case {
background: red;
}
3 changes: 3 additions & 0 deletions test/fixtures/out/camelCase.css
@@ -0,0 +1,3 @@
._classes_camel-case {
background: red;
}
19 changes: 19 additions & 0 deletions test/test.js
Expand Up @@ -98,6 +98,25 @@ test('processes globalModulePaths option', async (t) => {
t.is(result.css, out);
});

test('processes camelCase option', async (t) => {
const sourceFile = path.join(fixturesPath, 'in', 'camelCase.css');
const source = fs.readFileSync(sourceFile).toString();
const jsonFile = path.join(fixturesPath, 'in', 'camelCase.css.json');

if (fs.existsSync(jsonFile)) fs.unlinkSync(jsonFile);

await postcss([plugin({ generateScopedName, camelCase: true })])
.process(source, { from: sourceFile });

const json = fs.readFileSync(jsonFile).toString();
fs.unlinkSync(jsonFile);

t.deepEqual(JSON.parse(json), {
camelCase: '_camelCase_camel-case',
'camel-case': '_camelCase_camel-case',
});
});


test('different instances have different generateScopedName functions', async (t) => {
const one = plugin({
Expand Down

0 comments on commit 95a02b8

Please sign in to comment.