-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getClassName.js
155 lines (130 loc) · 5.49 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// @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 handleErrorPreserve = (msg: string, handleMissingStyleName: HandleMissingStyleNameOptionType, styleName: string, removeUnknownClassName: Boolean) => {
let ret = handleError(msg, handleMissingStyleName);
if (!removeUnknownClassName) {
// eslint-disable-next-line no-console
console.log('preserve className', styleName);
ret = styleName;
}
return ret;
}
const getClassNameForNamespacedStyleName = (
styleName: string,
styleModuleImportMap: StyleModuleImportMapType,
handleMissingStyleNameOption?: HandleMissingStyleNameOptionType,
removeUnknownClassName?: Boolean
): ?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 handleErrorPreserve('Invalid style name: ' + styleName, handleMissingStyleName, styleName, removeUnknownClassName);
}
if (!styleModuleImportMap[importName]) {
return handleErrorPreserve('CSS module import does not exist: ' + importName, handleMissingStyleName, styleName, removeUnknownClassName);
}
if (!styleModuleImportMap[importName][moduleName]) {
return handleErrorPreserve('CSS module does not exist: ' + moduleName, handleMissingStyleName, styleName, removeUnknownClassName);
}
return styleModuleImportMap[importName][moduleName];
};
const getClassNameFromMultipleImports = (
styleName: string,
styleModuleImportMap: StyleModuleImportMapType,
handleMissingStyleNameOption?: HandleMissingStyleNameOptionType,
removeUnknownClassName?: Boolean
): ?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 handleErrorPreserve('Could not resolve the styleName \'' + styleName + '\'.', handleMissingStyleName, styleName, removeUnknownClassName);
}
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,
removeUnknownClassName,
} = options || {};
if (!styleNameValue) {
return '';
}
return styleNameValue
.split(' ')
.filter((styleName) => {
return styleName;
})
.map((styleName) => {
if (isNamespacedStyleName(styleName)) {
return getClassNameForNamespacedStyleName(styleName, styleModuleImportMap, handleMissingStyleName, removeUnknownClassName);
}
// console.log('styleModuleImportMapKeys', styleModuleImportMapKeys)
if (styleModuleImportMapKeys.length === 0) {
const { include, exclude } = options
const errMsg = 'Cannot use styleName attribute for style name \'' + styleName +
'\' without importing at least one stylesheet.';
if (include || exclude) {
return console.warn(errMsg);
}
throw new Error(errMsg);
}
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, removeUnknownClassName);
}
const styleModuleMap: StyleModuleMapType = styleModuleImportMap[styleModuleImportMapKeys[0]];
if (!styleModuleMap[styleName]) {
return handleErrorPreserve('Could not resolve the styleName \'' + styleName + '\'.', handleMissingStyleName, styleName, removeUnknownClassName);
}
return styleModuleMap[styleName];
})
.filter((className) => {
// Remove any styles which could not be found (if handleMissingStyleName === 'ignore')
return className;
})
.join(' ');
};