Skip to content

Add option of adding camelize keys, like css-loader's camelCase #72

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 2 commits into from
May 29, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const genericNames = require('generic-names');
const globToRegex = require('glob-to-regexp');
const identity = require('lodash').identity;
const negate = require('lodash').negate;
const camelCaseFunc = require('lodash').camelCase;
const readFileSync = require('fs').readFileSync;
const relative = require('path').relative;
const resolve = require('path').resolve;
Expand All @@ -27,6 +28,7 @@ module.exports = function setupHook({
preprocessCss = identity,
processCss,
processorOpts,
camelCase,
append = [],
prepend = [],
createImportedName,
Expand Down Expand Up @@ -103,6 +105,11 @@ module.exports = function setupHook({

tokens = lazyResult.root.tokens;

if (camelCase) {
tokens = Object.assign({}, tokens,
...Object.keys(tokens).map(key => ({ [camelCaseFunc(key)]: tokens[key] })))
}

if (!debugMode) {
// updating cache
tokensByFile[filename] = tokens;
Expand Down
1 change: 1 addition & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const rules = {
preprocessCss: 'function',
processCss: 'function',
processorOpts: 'object',
camelCase: 'boolean',
// plugins
append: 'array',
prepend: 'array',
Expand Down
19 changes: 19 additions & 0 deletions test/api/camelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const detachHook = require('../sugar').detachHook;
const dropCache = require('../sugar').dropCache;

suite('api/camelCase', () => {
test('should add camel case keys in token', () => {
const tokens = require('./fixture/bem.css');
assert.deepEqual(tokens, {
blockElementModifier: '_test_api_fixture_bem__block__element--modifier',
'block__element--modifier': '_test_api_fixture_bem__block__element--modifier',
});
});

setup(() => hook({ camelCase: true }));

teardown(() => {
detachHook('.css');
dropCache('./api/fixture/bem.css');
});
});
3 changes: 3 additions & 0 deletions test/api/fixture/bem.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.block__element--modifier {
background: #1e2a35;
}