UNPKG

6.55 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../src/getClassName.js"],"names":["DEFAULT_HANDLE_MISSING_STYLENAME_OPTION","isNamespacedStyleName","styleName","indexOf","getClassNameForNamespacedStyleName","styleModuleImportMap","handleMissingStyleNameOption","styleNameParts","split","importName","moduleName","handleMissingStyleName","Error","console","warn","styleNameValue","options","styleModuleImportMapKeys","Object","keys","filter","map","length","styleModuleMap","className","join"],"mappings":";;;;;;;AAQA,MAAMA,0CAA0C,OAAhD;;AAEA,MAAMC,wBAAyBC,SAAD,IAAgC;AAC5D,SAAOA,UAAUC,OAAV,CAAkB,GAAlB,MAA2B,CAAC,CAAnC;AACD,CAFD;;AAIA,MAAMC,qCAAqC,CACzCF,SADyC,EAEzCG,oBAFyC,EAGzCC,4BAHyC,KAI7B;AACZ;AACA;AACA;AACA,QAAMC,iBAAiBL,UAAUM,KAAV,CAAgB,GAAhB,CAAvB;AACA,QAAMC,aAAaF,eAAe,CAAf,CAAnB;AACA,QAAMG,aAAaH,eAAe,CAAf,CAAnB;AACA,QAAMI,yBAAyBL,gCAC7BN,uCADF;;AAGA,MAAI,CAACU,UAAL,EAAiB;AACf,QAAIC,2BAA2B,OAA/B,EAAwC;AACtC,YAAM,IAAIC,KAAJ,CAAU,yBAAyBV,SAAnC,CAAN;AACD,KAFD,MAEO,IAAIS,2BAA2B,MAA/B,EAAuC;AAC5C;AACAE,cAAQC,IAAR,CAAa,yBAAyBZ,SAAtC;AACD,KAHM,MAGA;AACL,aAAO,IAAP;AACD;AACF;;AAED,MAAI,CAACG,qBAAqBI,UAArB,CAAL,EAAuC;AACrC,QAAIE,2BAA2B,OAA/B,EAAwC;AACtC,YAAM,IAAIC,KAAJ,CAAU,uCAAuCH,UAAjD,CAAN;AACD,KAFD,MAEO,IAAIE,2BAA2B,MAA/B,EAAuC;AAC5C;AACAE,cAAQC,IAAR,CAAa,uCAAuCL,UAApD;AACD,KAHM,MAGA;AACL,aAAO,IAAP;AACD;AACF;;AA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KAA7D,CAAN;AACD;AACD,UAAIS,2BAA2B,MAA/B,EAAuC;AACrC;AACAE,gBAAQC,IAAR,CAAa,uCAAuCZ,SAAvC,GAAmD,KAAhE;AACD;AACF;;AAED,WAAOqB,eAAerB,SAAf,CAAP;AACD,GAjCI,EAkCJkB,MAlCI,CAkCII,SAAD,IAAe;AACrB;AACA,WAAOA,SAAP;AACD,GArCI,EAsCJC,IAtCI,CAsCC,GAtCD,CAAP;AAuCD,C","file":"getClassName.js","sourcesContent":["// @flow\n\nimport type {\n StyleModuleMapType,\n StyleModuleImportMapType,\n HandleMissingStyleNameOptionType\n} from './types';\n\nconst DEFAULT_HANDLE_MISSING_STYLENAME_OPTION = 'throw';\n\nconst isNamespacedStyleName = (styleName: string): boolean => {\n return styleName.indexOf('.') !== -1;\n};\n\nconst getClassNameForNamespacedStyleName = (\n styleName: string,\n styleModuleImportMap: StyleModuleImportMapType,\n handleMissingStyleNameOption?: HandleMissingStyleNameOptionType\n): ?string => {\n // Note:\n // Do not use the desctructing syntax with Babel.\n // Desctructing adds _slicedToArray helper.\n const styleNameParts = styleName.split('.');\n const importName = styleNameParts[0];\n const moduleName = styleNameParts[1];\n const handleMissingStyleName = handleMissingStyleNameOption ||\n DEFAULT_HANDLE_MISSING_STYLENAME_OPTION;\n\n if (!moduleName) {\n if (handleMissingStyleName === 'throw') {\n throw new Error('Invalid style name: ' + styleName);\n } else if (handleMissingStyleName === 'warn') {\n // eslint-disable-next-line no-console\n console.warn('Invalid style name: ' + styleName);\n } else {\n return null;\n }\n }\n\n if (!styleModuleImportMap[importName]) {\n if (handleMissingStyleName === 'throw') {\n throw new Error('CSS module import does not exist: ' + importName);\n } else if (handleMissingStyleName === 'warn') {\n // eslint-disable-next-line no-console\n console.warn('CSS module import does not exist: ' + importName);\n } else {\n return null;\n }\n }\n\n if (!styleModuleImportMap[importName][moduleName]) {\n if (handleMissingStyleName === 'throw') {\n throw new Error('CSS module does not exist: ' + moduleName);\n } else if (handleMissingStyleName === 'warn') {\n // eslint-disable-next-line no-console\n console.warn('CSS module does not exist: ' + moduleName);\n } else {\n return null;\n }\n }\n\n return styleModuleImportMap[importName][moduleName];\n};\n\ntype OptionsType = {|\n handleMissingStyleName: HandleMissingStyleNameOptionType\n|};\n\nexport default (styleNameValue: string, styleModuleImportMap: StyleModuleImportMapType, options?: OptionsType): string => {\n const styleModuleImportMapKeys = Object.keys(styleModuleImportMap);\n\n const handleMissingStyleName = options && options.handleMissingStyleName ||\n DEFAULT_HANDLE_MISSING_STYLENAME_OPTION;\n\n return styleNameValue\n .split(' ')\n .filter((styleName) => {\n return styleName;\n })\n .map((styleName) => {\n if (isNamespacedStyleName(styleName)) {\n return getClassNameForNamespacedStyleName(styleName, styleModuleImportMap, handleMissingStyleName);\n }\n\n if (styleModuleImportMapKeys.length === 0) {\n throw new Error('Cannot use styleName attribute for style name \\'' + styleName +\n '\\' without importing at least one stylesheet.');\n }\n\n if (styleModuleImportMapKeys.length > 1) {\n throw new Error('Cannot use anonymous style name \\'' + styleName +\n '\\' with more than one stylesheet import.');\n }\n\n const styleModuleMap: StyleModuleMapType = styleModuleImportMap[styleModuleImportMapKeys[0]];\n\n if (!styleModuleMap[styleName]) {\n if (handleMissingStyleName === 'throw') {\n throw new Error('Could not resolve the styleName \\'' + styleName + '\\'.');\n }\n if (handleMissingStyleName === 'warn') {\n // eslint-disable-next-line no-console\n console.warn('Could not resolve the styleName \\'' + styleName + '\\'.');\n }\n }\n\n return styleModuleMap[styleName];\n })\n .filter((className) => {\n // Remove any styles which could not be found (if handleMissingStyleName === 'ignore')\n return className;\n })\n .join(' ');\n};\n"]}
\No newline at end of file