UNPKG

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