UNPKG

3.38 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7
8const DEFAULT_HANDLE_MISSING_STYLENAME_OPTION = 'throw';
9
10const isNamespacedStyleName = styleName => {
11 return styleName.indexOf('.') !== -1;
12};
13
14const getClassNameForNamespacedStyleName = (styleName, styleModuleImportMap, handleMissingStyleNameOption) => {
15 // Note:
16 // Do not use the desctructing syntax with Babel.
17 // Desctructing adds _slicedToArray helper.
18 const styleNameParts = styleName.split('.');
19 const importName = styleNameParts[0];
20 const moduleName = styleNameParts[1];
21 const handleMissingStyleName = handleMissingStyleNameOption || DEFAULT_HANDLE_MISSING_STYLENAME_OPTION;
22
23 if (!moduleName) {
24 if (handleMissingStyleName === 'throw') {
25 throw new Error('Invalid style name: ' + styleName);
26 } else if (handleMissingStyleName === 'warn') {
27 // eslint-disable-next-line no-console
28 console.warn('Invalid style name: ' + styleName);
29 } else {
30 return null;
31 }
32 }
33
34 if (!styleModuleImportMap[importName]) {
35 if (handleMissingStyleName === 'throw') {
36 throw new Error('CSS module import does not exist: ' + importName);
37 } else if (handleMissingStyleName === 'warn') {
38 // eslint-disable-next-line no-console
39 console.warn('CSS module import does not exist: ' + importName);
40 } else {
41 return null;
42 }
43 }
44
45 if (!styleModuleImportMap[importName][moduleName]) {
46 if (handleMissingStyleName === 'throw') {
47 throw new Error('CSS module does not exist: ' + moduleName);
48 } else if (handleMissingStyleName === 'warn') {
49 // eslint-disable-next-line no-console
50 console.warn('CSS module does not exist: ' + moduleName);
51 } else {
52 return null;
53 }
54 }
55
56 return styleModuleImportMap[importName][moduleName];
57};
58
59exports.default = (styleNameValue, styleModuleImportMap, options) => {
60 const styleModuleImportMapKeys = Object.keys(styleModuleImportMap);
61
62 const handleMissingStyleName = options && options.handleMissingStyleName || DEFAULT_HANDLE_MISSING_STYLENAME_OPTION;
63
64 return styleNameValue.split(' ').filter(styleName => {
65 return styleName;
66 }).map(styleName => {
67 if (isNamespacedStyleName(styleName)) {
68 return getClassNameForNamespacedStyleName(styleName, styleModuleImportMap, handleMissingStyleName);
69 }
70
71 if (styleModuleImportMapKeys.length === 0) {
72 throw new Error('Cannot use styleName attribute for style name \'' + styleName + '\' without importing at least one stylesheet.');
73 }
74
75 if (styleModuleImportMapKeys.length > 1) {
76 throw new Error('Cannot use anonymous style name \'' + styleName + '\' with more than one stylesheet import.');
77 }
78
79 const styleModuleMap = styleModuleImportMap[styleModuleImportMapKeys[0]];
80
81 if (!styleModuleMap[styleName]) {
82 if (handleMissingStyleName === 'throw') {
83 throw new Error('Could not resolve the styleName \'' + styleName + '\'.');
84 }
85 if (handleMissingStyleName === 'warn') {
86 // eslint-disable-next-line no-console
87 console.warn('Could not resolve the styleName \'' + styleName + '\'.');
88 }
89 }
90
91 return styleModuleMap[styleName];
92 }).filter(className => {
93 // Remove any styles which could not be found (if handleMissingStyleName === 'ignore')
94 return className;
95 }).join(' ');
96};
97//# sourceMappingURL=getClassName.js.map
\No newline at end of file