UNPKG

4.34 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 {
73 handleMissingStyleName = _optionsDefaults.default.handleMissingStyleName,
74 autoResolveMultipleImports = _optionsDefaults.default.autoResolveMultipleImports
75 } = options || {};
76
77 if (!styleNameValue) {
78 return '';
79 }
80
81 return styleNameValue.split(' ').filter(styleName => {
82 return styleName;
83 }).map(styleName => {
84 if (isNamespacedStyleName(styleName)) {
85 return getClassNameForNamespacedStyleName(styleName, styleModuleImportMap, handleMissingStyleName);
86 }
87
88 if (styleModuleImportMapKeys.length === 0) {
89 throw new Error('Cannot use styleName attribute for style name \'' + styleName + '\' without importing at least one stylesheet.');
90 }
91
92 if (styleModuleImportMapKeys.length > 1) {
93 if (!autoResolveMultipleImports) {
94 throw new Error('Cannot use anonymous style name \'' + styleName + '\' with more than one stylesheet import without setting \'autoResolveMultipleImports\' to true.');
95 }
96
97 return getClassNameFromMultipleImports(styleName, styleModuleImportMap, handleMissingStyleName);
98 }
99
100 const styleModuleMap = styleModuleImportMap[styleModuleImportMapKeys[0]];
101
102 if (!styleModuleMap[styleName]) {
103 return handleError('Could not resolve the styleName \'' + styleName + '\'.', handleMissingStyleName);
104 }
105
106 return styleModuleMap[styleName];
107 }).filter(className => {
108 // Remove any styles which could not be found (if handleMissingStyleName === 'ignore')
109 return className;
110 }).join(' ');
111};
112
113exports.default = _default;
114//# sourceMappingURL=getClassName.js.map
\No newline at end of file