Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions src/stylis-rtl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cssjanus from 'cssjanus';
import cssjanus from "cssjanus";
import {
COMMENT,
compile,
Expand All @@ -7,26 +7,22 @@ import {
RULESET,
serialize,
strlen,
Middleware,
KEYFRAMES,
MEDIA,
SUPPORTS,
} from 'stylis';
Element,
} from "stylis";

type MiddlewareParams = Parameters<Middleware>;

function stringifyPreserveComments(
element: MiddlewareParams[0],
index: MiddlewareParams[1],
children: MiddlewareParams[2]
): string {
function stringifyPreserveComments(element: Element): string {
switch (element.type) {
case IMPORT:
case DECLARATION:
case COMMENT:
return (element.return = element.return || element.value);
case RULESET: {
element.value = Array.isArray(element.props) ? element.props.join(',') : element.props;
element.value = Array.isArray(element.props)
? element.props.join(",")
: element.props;

if (Array.isArray(element.children)) {
element.children.forEach((x) => {
Expand All @@ -36,32 +32,35 @@ function stringifyPreserveComments(
}
}

const serializedChildren = serialize(Array.prototype.concat(element.children), stringifyPreserveComments);
const serializedChildren = serialize(
Array.prototype.concat(element.children),
stringifyPreserveComments
);

return strlen(serializedChildren) ? (element.return = element.value + '{' + serializedChildren + '}') : '';
return strlen(serializedChildren)
? (element.return = element.value + "{" + serializedChildren + "}")
: "";
}

function stylisRTLPlugin(
element: MiddlewareParams[0],
index: MiddlewareParams[1],
children: MiddlewareParams[2],
callback: MiddlewareParams[3]
): string | void {
function stylisRTLPlugin(element: Element): string | void {
if (
element.type === KEYFRAMES ||
element.type === SUPPORTS ||
(element.type === RULESET && (!element.parent || element.parent.type === MEDIA || element.parent.type === RULESET))
(element.type === RULESET &&
(!element.parent ||
element.parent.type === MEDIA ||
element.parent.type === RULESET))
) {
const stringified = cssjanus.transform(stringifyPreserveComments(element, index, children));
const stringified = cssjanus.transform(stringifyPreserveComments(element));

element.children = stringified ? compile(stringified)[0].children : [];

element.return = '';
element.return = "";
}
}

// stable identifier that will not be dropped by minification unless the whole module
// is unused
Object.defineProperty(stylisRTLPlugin, 'name', { value: 'stylisRTLPlugin' });
Object.defineProperty(stylisRTLPlugin, "name", { value: "stylisRTLPlugin" });

export default stylisRTLPlugin;