Repository URL to install this package:
|
Version:
1.2.18 ▾
|
"use strict";
// transpiled export default
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.preprocessHelper = exports.convertOutputToBabelTypes = exports.cssWithPlaceholdersToArr = exports.assembleAndInterleavePlaceholders = void 0;
const t = global._interopRequireWildcard(require('@babel/types')); // const _stylis = global._interopRequireDefault(require('stylis'))
const _stylis = global._interopRequireDefault(require("../../../view-container/styles/styleh"));
const _placeholderUtils = require("./placeholderUtils");
/**
* Assembles CSS partials and replaces interpolations with placeholders
*/
const stylis = new _stylis.default({
global: false,
cascade: true,
keyframe: false,
prefix: true,
compress: false,
semicolon: true
});
/**
* Splits the css into an array with interleaved interpolation nodes
*/
function assembleAndInterleavePlaceholders(cssArr) {
let css = cssArr[0];
for (let i = 1; i < cssArr.length; i++) {
const interpolationIndex = i - 1;
const placeholder = (0, _placeholderUtils.makePlaceholder)(interpolationIndex); // Append a semicolon to all mixins (not selectors, not rule)
const cssPartial = cssArr[i];
const separator = (0, _placeholderUtils.isUnendedMixin)(cssPartial) ? ';' : '';
css += placeholder + separator + cssPartial;
}
return css;
}
/**
* Convert CSS strings back to babel string literals
* and turn arrays back into babel array expressions
*/
function cssWithPlaceholdersToArr(css, interpolationNodes) {
const placeholderSplit = (0, _placeholderUtils.splitByPlaceholders)(css);
const res = [];
for (let i = 0; i < placeholderSplit.length; i++) {
const str = placeholderSplit[i];
const isInterpolation = i % 2 !== 0;
if (isInterpolation) {
const interpolationIndex = parseInt(str, 10);
res.push(interpolationNodes[interpolationIndex]);
} else {
res.push(str);
}
}
return res;
}
/*
* Flattens and splits CSS into an array where classname should be injected, and maps these
* partials to an array with interleaves interpolation nodes.
* Example:
* [
* [ ':hover { color: blue; background:', props => props.background, '; }' ]
* ]
*/
function convertOutputToBabelTypes(arrOfCSSArr) {
return t.arrayExpression(arrOfCSSArr.map(cssArr => {
return t.arrayExpression(cssArr.map(x => {
return typeof x === 'string' ? t.stringLiteral(x) : x;
}));
}));
}
exports.convertOutputToBabelTypes = convertOutputToBabelTypes;
function identity(x) {
return x;
} // eslint-disable-next-line
function preprocessHelper(cssArr, interpolationNodes) {
const transformFlattened = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : identity;
const stylisNamespace = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
const fixGlobals = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; // Test whether the input is using reserved strings
const someAreReserved = cssArr.some(function (x) {
return (0, _placeholderUtils.containsPlaceholders)(x) || x.includes(_placeholderUtils.temporaryClassname);
});
if (someAreReserved) {
throw new TypeError("CSS Input can't contain Styled Components placeholders of the format: __PLACEHOLDER_1__ or __TEMPORARY_CLASSNAME__.");
} // Flatten CSS using stylis
const css = transformFlattened(assembleAndInterleavePlaceholders(cssArr));
let flattenedCSS = stylis(stylisNamespace, css, false, false).trim();
if (fixGlobals && flattenedCSS.startsWith('{')) {
flattenedCSS = (0, _placeholderUtils.fixGlobalPlaceholders)(flattenedCSS);
}
const classnameSplit = flattenedCSS.split(_placeholderUtils.temporaryClassname).filter(x => x !== '').map(str => cssWithPlaceholdersToArr(str, interpolationNodes));
return classnameSplit;
}
exports.assembleAndInterleavePlaceholders = assembleAndInterleavePlaceholders;
exports.cssWithPlaceholdersToArr = cssWithPlaceholdersToArr;
exports.preprocessHelper = preprocessHelper;