All files / lib/adapters fela.js

90.38% Statements 47/52
79.63% Branches 43/54
76.92% Functions 10/13
100% Lines 28/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53    1x     1x   4x   6x   6x   24x     24x   24x 8x     16x     1x           2x   2x 2x 2x   2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x      
"use strict";
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = felaAdapter;
 
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); Eif (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); Eif (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
 
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else Eif (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
 
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
 
function _typeof(obj) { Eif (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
 
function stringifyDeclaration(declaration) {
  var prop = '"' + declaration.property + '":';
 
  if (_typeof(declaration.value) === 'object') {
    return prop + '{' + declaration.value.map(stringifyDeclaration).join(',\n') + '}';
  }
 
  return prop + declaration.value;
}
 
var defaultConfig = {
  reactFela: false,
  dynamicImport: false
};
 
function felaAdapter() {
  var customConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
 
  var _defaultConfig$custom = _objectSpread({}, defaultConfig, {}, customConfig),
      dynamicImport = _defaultConfig$custom.dynamicImport,
      reactFela = _defaultConfig$custom.reactFela;
 
  return function (_ref) {
    var style = _ref.style,
        variantStyleMap = _ref.variantStyleMap,
        className = _ref.className,
        cssFileName = _ref.cssFileName,
        resetClassName = _ref.resetClassName,
        classNameMap = _ref.classNameMap,
        moduleName = _ref.moduleName;
    var hasVariations = Object.keys(classNameMap).length > 1;
    var hasDynamicVariations = Object.keys(variantStyleMap).length > 0;
    var imports = [hasVariations ? 'getClassNameFromVariantMap' : undefined, hasDynamicVariations ? 'getDynamicStyleFromVariantMap' : undefined].filter(Boolean);
    return (!dynamicImport ? "import './" + cssFileName + ".css'\n" : '') + (reactFela ? "import { createComponent } from 'react-fela'\n" : '') + (imports.length > 0 ? 'import { ' + imports.join(', ') + " } from '@elodin/runtime'\n\n" : '\n') + (hasVariations ? 'const variantClassNameMap = ' + JSON.stringify(classNameMap, null, 2) + '\n\n' : '') + (hasDynamicVariations ? 'const variantStyleMap = {\n' + Object.keys(variantStyleMap).map(function (modifier) {
      return "'" + modifier + "'" + ': (props = {}) => ({\n  ' + variantStyleMap[modifier].map(stringifyDeclaration).join(',\n  ') + '\n})';
    }).join(',\n') + '\n}' + '\n\n' : '') + (reactFela ? '' : 'export ') + 'function ' + moduleName + (reactFela ? 'Style' : '') + '(props = {})' + ' {\n  ' + (dynamicImport ? "import('./" + cssFileName + ".css')\n  " : '') + 'return {\n    ' + '_className: ' + (hasVariations ? "'" + resetClassName + " ' + " + "getClassNameFromVariantMap('" + className + "', variantClassNameMap, props)" : "'" + resetClassName + ' ' + className + "'") + ',\n    ' + style.map(stringifyDeclaration).join(',\n    ') + (hasDynamicVariations ? ',\n    ...getDynamicStyleFromVariantMap(variantStyleMap, props)' : '') + '\n  }\n}' + (reactFela ? '\nexport const ' + moduleName + ' = createComponent(' + moduleName + 'Style' + ')' : '');
  };
}