pretty-lights
Version:
CSS-in-JS with a reliable API
85 lines (68 loc) • 3.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(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; }
var getMatches = function getMatches(regEx, html) {
var ids = {};
var match; // eslint-disable-next-line no-cond-assign
while ((match = regEx.exec(html)) !== null) {
// $FlowFixMe
if (ids[match[1]] === undefined) {
if (match[1].indexOf(' ') > 0) {
console.log('space', match[1]);
match[1].split(' ').forEach(function (id) {
ids[id] = true;
});
} else {
// $FlowFixMe
ids[match[1]] = true;
}
}
}
return ids;
};
var createExtractCritical = function createExtractCritical(cache) {
return function (html) {
// parse out globals from <Global />
var GLOBAL_RGX = new RegExp("data-global-".concat(cache.key, "=\"([a-zA-Z0-9-_ ]+)\""), 'gm');
var GLOBAL_STYLE_TAG = new RegExp("<style data-global-".concat(cache.key, "=\"[^\"]+\"[^>]*></style>"), 'gm'); // parse out animation ids from css
var ANIM_RGX = new RegExp("animation-([a-zA-Z0-9-_]+)", 'gm'); // parse out ids from html
// reconstruct css/rules/cache to pass
var RGX = new RegExp("".concat(cache.key, "-([a-zA-Z0-9-_]+)"), 'gm');
var o = {
html: html,
ids: [],
css: ''
};
var ids = _objectSpread(_objectSpread({}, getMatches(GLOBAL_RGX, html)), getMatches(RGX, html));
o.ids = o.ids.concat( // add global styles to the front
Object.keys(cache.global).map(function (id) {
o.css += cache.inserted[id];
return id;
}), // add styles matching selectors
Object.keys(cache.inserted).filter(function (id) {
if (ids[id] !== undefined && cache.global[id] === undefined && cache.inserted[id] !== true) {
o.css += cache.inserted[id];
return true;
}
return false;
})); // include identified keyframe animations at the top
var animIds = getMatches(ANIM_RGX, o.css);
var animCss = '';
o.ids = o.ids.concat(Object.keys(animIds).map(function (id) {
if (cache.inserted[id]) {
animCss += cache.inserted[id];
}
return id;
}));
o.css = animCss + o.css; // remove global component style placeholders
o.html = o.html.replace(GLOBAL_STYLE_TAG, '');
return o;
};
};
var _default = createExtractCritical;
exports["default"] = _default;