UNPKG

@stylable/core

Version:

CSS for Components

166 lines 6.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isCustomValue = exports.getBoxValue = exports.createCustomValue = exports.resolveCustomValues = exports.CustomValueStrategy = exports.stTypes = exports.unbox = exports.box = void 0; const lodash_clonedeepwith_1 = __importDefault(require("lodash.clonedeepwith")); const postcss_value_parser_1 = __importDefault(require("postcss-value-parser")); const stylable_value_parsers_1 = require("./stylable-value-parsers"); function box(type, value) { return { type, value, }; } exports.box = box; const { hasOwnProperty } = Object.prototype; function unbox(boxed) { if (typeof boxed === 'string') { return boxed; } else if (typeof boxed === 'object' && boxed.type && hasOwnProperty.call(boxed, 'value')) { return lodash_clonedeepwith_1.default(boxed.value, unbox); } } exports.unbox = unbox; exports.stTypes = { stArray: createCustomValue({ processArgs: (node, customTypes) => { return exports.CustomValueStrategy.args(node, customTypes); }, createValue: (args) => { return args; }, getValue: (value, index) => value[parseInt(index, 10)], }).register('stArray'), stMap: createCustomValue({ processArgs: (node, customTypes) => { return exports.CustomValueStrategy.named(node, customTypes); }, createValue: (args) => { return args; }, getValue: (value, index) => value[index], }).register('stMap'), }; exports.CustomValueStrategy = { args: (fnNode, customTypes) => { const pathArgs = stylable_value_parsers_1.getFormatterArgs(fnNode); const outputArray = []; for (const arg of pathArgs) { const parsedArg = postcss_value_parser_1.default(arg).nodes[0]; const ct = parsedArg.type === 'function' && parsedArg.value; const resolvedValue = typeof ct === 'string' && customTypes[ct] ? customTypes[ct].evalVarAst(parsedArg, customTypes) : arg; outputArray.push(resolvedValue); } return outputArray; }, named: (fnNode, customTypes) => { const outputMap = {}; const s = stylable_value_parsers_1.getNamedArgs(fnNode); for (const [prop, space, ...valueNodes] of s) { if (space.type !== 'space') { // TODO: error catch throw new Error('Invalid argument'); } let resolvedValue; if (valueNodes.length === 0) { // TODO: error } else if (valueNodes.length === 1) { const valueNode = valueNodes[0]; resolvedValue = valueNode.resolvedValue; if (!resolvedValue) { const ct = customTypes[valueNode.value]; if (valueNode.type === 'function' && ct) { resolvedValue = ct.evalVarAst(valueNode, customTypes); } else { resolvedValue = stylable_value_parsers_1.getStringValue(valueNode); } } } else { resolvedValue = stylable_value_parsers_1.getStringValue(valueNodes); } if (resolvedValue) { outputMap[prop.value] = resolvedValue; } } return outputMap; }, }; function resolveCustomValues(meta, resolver) { const customValues = { ...exports.stTypes }; for (const [symbolName, symbol] of Object.entries(meta.mappedSymbols)) { if (symbol._kind !== 'import') { continue; } const ss = resolver.resolveImport(symbol); if (!ss || ss._kind === 'css') { continue; } if (ss.symbol && isCustomValue(ss.symbol)) { if (customValues[symbolName]) { // TODO: report reserved name.! } else { customValues[symbolName] = ss.symbol.register(symbolName); } } } return customValues; } exports.resolveCustomValues = resolveCustomValues; function createCustomValue({ processArgs, createValue, flattenValue, getValue, }) { return { _kind: 'CustomValue', register(localTypeSymbol) { return { evalVarAst(fnNode, customTypes) { const args = processArgs(fnNode, customTypes); return box(localTypeSymbol, createValue(args)); }, getValue(path, obj, fallbackNode, // TODO: add test customTypes) { if (path.length === 0) { if (flattenValue) { const { delimiter, parts } = flattenValue(obj); return parts .map((v) => getBoxValue([], v, fallbackNode, customTypes)) .join(delimiter); } else { // TODO: add diagnostics return stylable_value_parsers_1.getStringValue([fallbackNode]); } } const value = getValue(obj.value, path[0]); return getBoxValue(path.slice(1), value, fallbackNode, customTypes); }, }; }, }; } exports.createCustomValue = createCustomValue; function getBoxValue(path, value, node, customTypes) { if (typeof value === 'string') { return value; } else if (value && customTypes[value.type]) { return customTypes[value.type].getValue(path, value, node, customTypes); } else { throw new Error('Unknown Type ' + JSON.stringify(value)); // return JSON.stringify(value); } } exports.getBoxValue = getBoxValue; function isCustomValue(symbol) { return symbol._kind === 'CustomValue'; } exports.isCustomValue = isCustomValue; //# sourceMappingURL=custom-values.js.map