jss-compose
Version:
JSS plugin for classes composition
210 lines (180 loc) • 5.96 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["jssCompose"] = factory();
else
root["jssCompose"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = jssCompose;
var _warning = __webpack_require__(1);
var _warning2 = _interopRequireDefault(_warning);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Set selector.
*
* @param {Object} original rule
* @param {String} className class string
* @return {Boolean} flag, indicating function was successfull or not
*/
function registerClass(rule, className) {
// Skip falsy values
if (!className) return true;
// Support array of class names `{composes: ['foo', 'bar']}`
if (Array.isArray(className)) {
for (var index = 0; index < className.length; index++) {
var isSetted = registerClass(rule, className[index]);
if (!isSetted) return false;
}
return true;
}
// Support space separated class names `{composes: 'foo bar'}`
if (className.indexOf(' ') > -1) {
return registerClass(rule, className.split(' '));
}
var parent = rule.options.parent;
// It is a ref to a local rule.
if (className[0] === '$') {
var refRule = parent.getRule(className.substr(1));
if (!refRule) {
(0, _warning2.default)(false, '[JSS] Referenced rule is not defined. \r\n%s', rule);
return false;
}
if (refRule === rule) {
(0, _warning2.default)(false, '[JSS] Cyclic composition detected. \r\n%s', rule);
return false;
}
parent.classes[rule.key] += ' ' + parent.classes[refRule.key];
return true;
}
rule.options.parent.classes[rule.key] += ' ' + className;
return true;
}
/**
* Convert compose property to additional class, remove property from original styles.
*
* @param {Rule} rule
* @api public
*/
function jssCompose() {
function onProcessStyle(style, rule) {
if (!style.composes) return style;
registerClass(rule, style.composes);
// Remove composes property to prevent infinite loop.
delete style.composes;
return style;
}
return { onProcessStyle: onProcessStyle };
}
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
/**
* Copyright 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
/**
* Similar to invariant but only logs a warning if the condition is not met.
* This can be used to log issues in development environments in critical
* paths. Removing the logging code for production environments will keep the
* same logic and follow the same code paths.
*/
var warning = function() {};
if (true) {
warning = function(condition, format, args) {
var len = arguments.length;
args = new Array(len > 2 ? len - 2 : 0);
for (var key = 2; key < len; key++) {
args[key - 2] = arguments[key];
}
if (format === undefined) {
throw new Error(
'`warning(condition, format, ...args)` requires a warning ' +
'message argument'
);
}
if (format.length < 10 || (/^[s\W]*$/).test(format)) {
throw new Error(
'The warning format should be able to uniquely identify this ' +
'warning. Please, use a more descriptive format than: ' + format
);
}
if (!condition) {
var argIndex = 0;
var message = 'Warning: ' +
format.replace(/%s/g, function() {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
try {
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch(x) {}
}
};
}
module.exports = warning;
/***/ }
/******/ ])
});
;
//# sourceMappingURL=jss-compose.js.map