@@ -6,49 +6,53 @@ import type {
6
6
} from './types' ;
7
7
8
8
const isNamespacedStyleName = ( styleName : string ) : boolean => {
9
- return styleName . includes ( '.' ) ;
9
+ return styleName . indexOf ( '.' ) !== - 1 ;
10
10
} ;
11
11
12
12
const getClassNameForNamespacedStyleName = ( styleName : string , styleModuleImportMap : StyleModuleImportMapType ) : string => {
13
- const [
14
- importName ,
15
- moduleName
16
- ] = styleName . split ( '.' ) ;
13
+ // Note:
14
+ // Do not use the desctructing syntax with Babel.
15
+ // Desctructing adds _slicedToArray helper.
16
+ const styleNameParts = styleName . split ( '.' ) ;
17
+ const importName = styleNameParts [ 0 ] ;
18
+ const moduleName = styleNameParts [ 1 ] ;
17
19
18
20
if ( ! moduleName ) {
19
21
throw new Error ( 'Invalid style name.' ) ;
20
22
}
21
23
22
- if ( ! styleModuleImportMap . hasOwnProperty ( importName ) ) {
24
+ if ( ! styleModuleImportMap [ importName ] ) {
23
25
throw new Error ( 'Import does not exist.' ) ;
24
26
}
25
27
26
- if ( ! styleModuleImportMap [ importName ] . hasOwnProperty ( moduleName ) ) {
28
+ if ( ! styleModuleImportMap [ importName ] [ moduleName ] ) {
27
29
throw new Error ( 'Module does not exist.' ) ;
28
30
}
29
31
30
32
return styleModuleImportMap [ importName ] [ moduleName ] ;
31
33
} ;
32
34
33
35
export default ( styleNameValue : string , styleModuleImportMap : StyleModuleImportMapType ) : string => {
36
+ const styleModuleImportMapKeys = Object . keys ( styleModuleImportMap ) ;
37
+
34
38
return styleNameValue
35
39
. split ( ' ' )
36
40
. map ( ( styleName ) => {
37
41
if ( isNamespacedStyleName ( styleName ) ) {
38
42
return getClassNameForNamespacedStyleName ( styleName , styleModuleImportMap ) ;
39
43
}
40
44
41
- if ( Object . keys ( styleModuleImportMap ) . length === 0 ) {
45
+ if ( styleModuleImportMapKeys . length === 0 ) {
42
46
throw new Error ( 'Cannot use styleName attribute without importing at least one stylesheet.' ) ;
43
47
}
44
48
45
- if ( Object . keys ( styleModuleImportMap ) . length > 1 ) {
49
+ if ( styleModuleImportMapKeys . length > 1 ) {
46
50
throw new Error ( 'Cannot use anonymous style name with more than one stylesheet import.' ) ;
47
51
}
48
52
49
- const styleModuleMap : StyleModuleMapType = styleModuleImportMap [ Object . keys ( styleModuleImportMap ) [ 0 ] ] ;
53
+ const styleModuleMap : StyleModuleMapType = styleModuleImportMap [ styleModuleImportMapKeys [ 0 ] ] ;
50
54
51
- if ( ! styleModuleMap . hasOwnProperty ( styleName ) ) {
55
+ if ( ! styleModuleMap [ styleName ] ) {
52
56
throw new Error ( 'Module cannot be resolved.' ) ;
53
57
}
54
58
0 commit comments