UNPKG

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