-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
getClassName.js
137 lines (113 loc) · 4.59 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// @flow
import type {
StyleModuleMapType,
StyleModuleImportMapType,
HandleMissingStyleNameOptionType,
GetClassNameOptionsType
} from './types';
import optionsDefaults from './schemas/optionsDefaults';
const isNamespacedStyleName = (styleName: string): boolean => {
return styleName.indexOf('.') !== -1;
};
const handleError = (message: string, handleMissingStyleName: HandleMissingStyleNameOptionType): null => {
if (handleMissingStyleName === 'throw') {
throw new Error(message);
} else if (handleMissingStyleName === 'warn') {
// eslint-disable-next-line no-console
console.warn(message);
}
return null;
};
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 ||
optionsDefaults.handleMissingStyleName;
if (!moduleName) {
return handleError('Invalid style name: ' + styleName, handleMissingStyleName);
}
if (!styleModuleImportMap[importName]) {
return handleError('CSS module import does not exist: ' + importName, handleMissingStyleName);
}
if (!styleModuleImportMap[importName][moduleName]) {
return handleError('CSS module does not exist: ' + moduleName, handleMissingStyleName);
}
return styleModuleImportMap[importName][moduleName];
};
const getClassNameFromMultipleImports = (
styleName: string,
styleModuleImportMap: StyleModuleImportMapType,
handleMissingStyleNameOption?: HandleMissingStyleNameOptionType
): ?string => {
const handleMissingStyleName = handleMissingStyleNameOption ||
optionsDefaults.handleMissingStyleName;
const importKeysWithMatches = Object.keys(styleModuleImportMap)
.map((importKey) => {
return styleModuleImportMap[importKey][styleName] && importKey;
})
.filter((importKey) => {
return importKey;
});
if (importKeysWithMatches.length > 1) {
throw new Error('Cannot resolve styleName "' + styleName + '" because it is present in multiple imports:' +
'\n\n\t' + importKeysWithMatches.join('\n\t') +
'\n\nYou can resolve this by using a named import, e.g:' +
'\n\n\timport foo from "' + importKeysWithMatches[0] + '";' +
'\n\t<div styleName="foo.' + styleName + '" />' +
'\n\n');
}
if (importKeysWithMatches.length === 0) {
return handleError('Could not resolve the styleName \'' + styleName + '\'.', handleMissingStyleName);
}
return styleModuleImportMap[importKeysWithMatches[0]][styleName];
};
export default (styleNameValue: string, styleModuleImportMap: StyleModuleImportMapType, options?: GetClassNameOptionsType): string => {
const styleModuleImportMapKeys = Object.keys(styleModuleImportMap);
const {
handleMissingStyleName = optionsDefaults.handleMissingStyleName,
autoResolveMultipleImports = optionsDefaults.autoResolveMultipleImports
} = options || {};
if (!styleNameValue) {
return '';
}
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 for style name \'' + styleName +
'\' without importing at least one stylesheet.');
}
if (styleModuleImportMapKeys.length > 1) {
if (!autoResolveMultipleImports) {
throw new Error('Cannot use anonymous style name \'' + styleName +
'\' with more than one stylesheet import without setting \'autoResolveMultipleImports\' to true.');
}
return getClassNameFromMultipleImports(styleName, styleModuleImportMap, handleMissingStyleName);
}
// There is only one imported CSS module file.
const styleModuleMap: StyleModuleMapType = styleModuleImportMap[styleModuleImportMapKeys[0]];
if (!styleModuleMap[styleName]) {
return handleError('Could not resolve the styleName \'' + styleName + '\'.', handleMissingStyleName);
}
return styleModuleMap[styleName];
})
.filter((className) => {
// Remove any styles which could not be found (if handleMissingStyleName === 'ignore')
return className;
})
.join(' ');
};