File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,15 @@ export default (cssModulesPath) => {
2626
2727
2828function getCssClassName ( cssModulesPath , cssModuleName ) {
29+ if ( typeof cssModulesPath === 'string' ) {
30+ return getCssClassNameFromPath ( cssModulesPath , cssModuleName ) ;
31+ } else {
32+ return getCssClassNameFromObject ( cssModulesPath , cssModuleName ) ;
33+ }
34+ }
35+
36+
37+ function getCssClassNameFromPath ( cssModulesPath , cssModuleName ) {
2938 if ( fs . lstatSync ( cssModulesPath ) . isDirectory ( ) ) {
3039 let cssModulesDir = cssModulesPath ;
3140 let cssModuleNameParts = cssModuleName . split ( '.' ) ;
@@ -36,14 +45,19 @@ function getCssClassName(cssModulesPath, cssModuleName) {
3645
3746 const cssModules = getCssModules ( path . resolve ( cssModulesPath ) ) ;
3847
48+ return getCssClassNameFromObject ( cssModules , cssModuleName ) ;
49+ }
50+
51+
52+ function getCssClassNameFromObject ( cssModules , cssModuleName ) {
3953 return cssModuleName . trim ( ) . split ( ' ' )
4054 . map ( cssModuleName => {
4155 const cssClassName = _get ( cssModules , cssModuleName ) ;
4256 if ( ! cssClassName ) {
4357 throw getError ( 'CSS module "' + cssModuleName + '" is not found' ) ;
4458 } else if ( typeof cssClassName !== 'string' ) {
4559 throw getError ( 'CSS module "' + cssModuleName + '" is not a string' ) ;
46- }
60+ }
4761 return cssClassName ;
4862 } )
4963 . join ( ' ' ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import cssModules from '..';
55
66const classesPath = path . join ( __dirname , 'classes.json' ) ;
77const classesDir = path . dirname ( classesPath ) ;
8+ const classesObj = require ( classesPath ) ;
89
910
1011describe ( 'posthtml-css-modules' , ( ) => {
@@ -51,6 +52,15 @@ describe('posthtml-css-modules', () => {
5152 } ) ;
5253
5354
55+ it ( 'should inline CSS module from the object' , ( ) => {
56+ return init (
57+ '<div css-module="title"></div>' ,
58+ '<div class="__title __heading"></div>' ,
59+ classesObj
60+ ) ;
61+ } ) ;
62+
63+
5464 it ( 'should throw an error if the file with the CSS modules is not found' , ( ) => {
5565 return init (
5666 '<div></div>' ,
You can’t perform that action at this time.
0 commit comments