|
| 1 | +import {basename} from 'path'; |
| 2 | + |
| 3 | +import {camelCase} from 'change-case'; |
| 4 | + |
| 5 | +const COMPONENT_REGEX = /^[A-Z]\w+$/; |
| 6 | +const SUBCOMPONENT_VARIATION_SELECTOR = /^\w+-\w+$/; |
| 7 | +const NESTED_COMPONENT_PATH_REGEX = /.*\/components\/(.*)\/components/; |
| 8 | + |
| 9 | +export const generateScopedName = function generateScopedName({ |
| 10 | + includeHash = false, |
| 11 | +} = {}) { |
| 12 | + return (name, filename) => { |
| 13 | + const cleanedFilename = filename.replace(/\?.*$/, ''); |
| 14 | + const componentName = basename(cleanedFilename, '.module.scss'); |
| 15 | + const nestedComponentMatch = |
| 16 | + NESTED_COMPONENT_PATH_REGEX.exec(cleanedFilename); |
| 17 | + |
| 18 | + const polarisComponentName = |
| 19 | + nestedComponentMatch && nestedComponentMatch.length > 1 |
| 20 | + ? `${polarisClassName(nestedComponentMatch[1])}-${componentName}` |
| 21 | + : polarisClassName(componentName); |
| 22 | + |
| 23 | + let className; |
| 24 | + |
| 25 | + if (isComponent(name)) { |
| 26 | + className = |
| 27 | + componentName === name |
| 28 | + ? polarisComponentName |
| 29 | + : subcomponentClassName(polarisComponentName, name); |
| 30 | + } else if (SUBCOMPONENT_VARIATION_SELECTOR.test(name)) { |
| 31 | + const [subcomponent, variation] = name.split('-'); |
| 32 | + const subcomponentName = subcomponentClassName( |
| 33 | + polarisComponentName, |
| 34 | + subcomponent, |
| 35 | + ); |
| 36 | + className = variationClassName(subcomponentName, camelCase(variation)); |
| 37 | + } else { |
| 38 | + className = variationClassName(polarisComponentName, camelCase(name)); |
| 39 | + } |
| 40 | + |
| 41 | + const suffix = includeHash |
| 42 | + ? `_${stringHash(name).toString(36).substr(0, 5)}` |
| 43 | + : ''; |
| 44 | + |
| 45 | + return className + suffix; |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +function isComponent(className) { |
| 50 | + return COMPONENT_REGEX.test(className); |
| 51 | +} |
| 52 | + |
| 53 | +function polarisClassName(className) { |
| 54 | + return `Polaris-${className}`; |
| 55 | +} |
| 56 | + |
| 57 | +function subcomponentClassName(component, subcomponent) { |
| 58 | + return `${component}__${subcomponent}`; |
| 59 | +} |
| 60 | + |
| 61 | +function variationClassName(component, variation) { |
| 62 | + return `${component}--${variation}`; |
| 63 | +} |
| 64 | + |
| 65 | +// Taken from the string-hash package |
| 66 | +function stringHash(str) { |
| 67 | + let hash = 5381; |
| 68 | + let i = str.length; |
| 69 | + |
| 70 | + while (i) { |
| 71 | + hash = (hash * 33) ^ str.charCodeAt(--i); |
| 72 | + } |
| 73 | + |
| 74 | + /* JavaScript does bitwise operations (like XOR, above) on 32-bit signed |
| 75 | + * integers. Since we want the results to be always positive, convert the |
| 76 | + * signed int to an unsigned by doing an unsigned bitshift. */ |
| 77 | + return hash >>> 0; |
| 78 | +} |
0 commit comments