forked from gajus/babel-plugin-react-css-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getClassName.js
112 lines (95 loc) · 3.52 KB
/
getClassName.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// @flow
import type {
StyleModuleMapType,
StyleModuleImportMapType,
HandleMissingStyleNameOptionType
} from './types';
const DEFAULT_HANDLE_MISSING_STYLENAME_OPTION = 'throw';
const isNamespacedStyleName = (styleName: string): boolean => {
return styleName.indexOf('.') !== -1;
};
const getClassNameForNamespacedStyleName = (
styleName: string,
styleModuleImportMap: StyleModuleImportMapType,
handleMissingStyleNameOption?: HandleMissingStyleNameOptionType
): ?string => {
// Note:
// Do not use the desctructing syntax with Babel.
// Desctructing adds _slicedToArray helper.
const styleNameParts = styleName.split('.');
const importName = styleNameParts[0];
const moduleName = styleNameParts[1];
const handleMissingStyleName = handleMissingStyleNameOption ||
DEFAULT_HANDLE_MISSING_STYLENAME_OPTION;
if (!moduleName) {
if (handleMissingStyleName === 'throw') {
throw new Error('Invalid style name.');
} else if (handleMissingStyleName === 'warn') {
// eslint-disable-next-line no-console
console.warn('Invalid style name.');
} else {
return null;
}
}
if (!styleModuleImportMap[importName]) {
if (handleMissingStyleName === 'throw') {
throw new Error('CSS module import does not exist.');
} else if (handleMissingStyleName === 'warn') {
// eslint-disable-next-line no-console
console.warn('CSS module import does not exist.');
} else {
return null;
}
}
if (!styleModuleImportMap[importName][moduleName]) {
if (handleMissingStyleName === 'throw') {
throw new Error('CSS module does not exist.');
} else if (handleMissingStyleName === 'warn') {
// eslint-disable-next-line no-console
console.warn('CSS module does not exist.');
} else {
return null;
}
}
return styleModuleImportMap[importName][moduleName];
};
type OptionsType = {|
handleMissingStyleName: HandleMissingStyleNameOptionType
|};
export default (styleNameValue: string, styleModuleImportMap: StyleModuleImportMapType, options?: OptionsType): string => {
const styleModuleImportMapKeys = Object.keys(styleModuleImportMap);
const handleMissingStyleName = options && options.handleMissingStyleName ||
DEFAULT_HANDLE_MISSING_STYLENAME_OPTION;
return styleNameValue
.split(' ')
.filter((styleName) => {
return styleName;
})
.map((styleName) => {
if (isNamespacedStyleName(styleName)) {
return getClassNameForNamespacedStyleName(styleName, styleModuleImportMap, handleMissingStyleName);
}
if (styleModuleImportMapKeys.length === 0) {
throw new Error('Cannot use styleName attribute without importing at least one stylesheet.');
}
if (styleModuleImportMapKeys.length > 1) {
throw new Error('Cannot use anonymous style name with more than one stylesheet import.');
}
const styleModuleMap: StyleModuleMapType = styleModuleImportMap[styleModuleImportMapKeys[0]];
if (!styleModuleMap[styleName]) {
if (handleMissingStyleName === 'throw') {
throw new Error('Could not resolve the styleName \'' + styleName + '\'.');
}
if (handleMissingStyleName === 'warn') {
// eslint-disable-next-line no-console
console.warn('Could not resolve the styleName \'' + styleName + '\'.');
}
}
return styleModuleMap[styleName];
})
.filter((className) => {
// Remove any styles which could not be found (if handleMissingStyleName === 'ignore')
return className;
})
.join(' ');
};