Skip to content
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

Add option to transform classes to camelCase #82

Merged
merged 4 commits into from Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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