Skip to content

allow passing css modules object to cssModulesPath #10

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 1 commit into from
Sep 3, 2017
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
16 changes: 15 additions & 1 deletion lib/cssModules.es6
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ export default (cssModulesPath) => {


function getCssClassName(cssModulesPath, cssModuleName) {
if (typeof cssModulesPath === 'string') {
return getCssClassNameFromPath(cssModulesPath, cssModuleName);
} else {
return getCssClassNameFromObject(cssModulesPath, cssModuleName);
}
}


function getCssClassNameFromPath(cssModulesPath, cssModuleName) {
if (fs.lstatSync(cssModulesPath).isDirectory()) {
let cssModulesDir = cssModulesPath;
let cssModuleNameParts = cssModuleName.split('.');
Expand All @@ -36,14 +45,19 @@ function getCssClassName(cssModulesPath, cssModuleName) {

const cssModules = getCssModules(path.resolve(cssModulesPath));

return getCssClassNameFromObject(cssModules, cssModuleName);
}


function getCssClassNameFromObject(cssModules, cssModuleName) {
return cssModuleName.trim().split(' ')
.map(cssModuleName => {
const cssClassName = _get(cssModules, cssModuleName);
if (! cssClassName) {
throw getError('CSS module "' + cssModuleName + '" is not found');
} else if (typeof cssClassName !== 'string') {
throw getError('CSS module "' + cssModuleName + '" is not a string');
}
}
return cssClassName;
})
.join(' ');
Expand Down
10 changes: 10 additions & 0 deletions test/cssModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cssModules from '..';

const classesPath = path.join(__dirname, 'classes.json');
const classesDir = path.dirname(classesPath);
const classesObj = require(classesPath);


describe('posthtml-css-modules', () => {
Expand Down Expand Up @@ -51,6 +52,15 @@ describe('posthtml-css-modules', () => {
});


it('should inline CSS module from the object', () => {
return init(
'<div css-module="title"></div>',
'<div class="__title __heading"></div>',
classesObj
);
});


it('should throw an error if the file with the CSS modules is not found', () => {
return init(
'<div></div>',
Expand Down