UNPKG

react-scoped-styles

Version:
47 lines (46 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceConditionals = exports.findClosingParenthesisIdx = void 0; const findClosingParenthesisIdx = (str) => { let stack = null; for (let i = 0; i < str.length; i++) { const char = str[i]; if (char === '(') { if (stack === null) stack = 0; stack += 1; } else if (char === ')') { if (stack !== null) stack -= 1; } if (stack === 0) return i; } return 0; }; exports.findClosingParenthesisIdx = findClosingParenthesisIdx; // const dynKeyRegex = /(?<=\[).+(?=\]\:)/g; // const constKeyRegex = /['"`]?([\w-${}]+)["'`]?(?=:)/g; // const defaultClassRegex = /["'`](.+)["'`](?=[,)])/g; const allStringsRegex = /["'`](.+?)["'`]/g; const replaceConditionals = (content, includeHash) => { if (!/classes\(/ig.test(content)) { return content; } const regexp = /classes\(/ig; let match; let replacedContent = content; while ((match = regexp.exec(replacedContent)) !== null) { const startIdx = match.index; const closingIdx = (0, exports.findClosingParenthesisIdx)(replacedContent.slice(startIdx)) + startIdx + 1; const pre = replacedContent.slice(0, startIdx); const post = replacedContent.slice(closingIdx); const classesExpr = replacedContent .slice(startIdx, closingIdx) .replace(allStringsRegex, (_, className) => `'${includeHash(className)}'`); replacedContent = pre + classesExpr + post; } return replacedContent; }; exports.replaceConditionals = replaceConditionals;