Skip to content

Commit cb3e1bb

Browse files
committed
Add minification in helpers css, keyframes, and createGlobalStyle
1 parent d6e1217 commit cb3e1bb

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

src/__tests__/baselines/minification/minify-css-in-helpers.ts.baseline

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,8 @@ TypeScript after transform:
3838
declare const keyframes: any;
3939
declare const css: any;
4040
declare const theColor: any;
41-
const key = keyframes \`
42-
to {
43-
transform: rotate(360deg);
44-
}
45-
\`;
46-
const color = css \`
47-
color: \${theColor};
48-
\`;
41+
const key = keyframes \`to{transform:rotate(360deg);}\`;
42+
const color = css \`color:\${theColor};\`;
4943
export {};
5044

5145

src/createTransformer.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { minifyTemplate } from './minify';
1919
* styled.tag
2020
* Component.extend
2121
* styled(Component)
22+
* styled('tag')
2223
* styledFunction.attrs(attributes)
2324
*/
2425
function isStyledFunction(node: ts.Node, identifiers: CustomStyledIdentifiers): boolean {
@@ -84,6 +85,30 @@ function isStyledAttrs(node: ts.Node, identifiers: CustomStyledIdentifiers) {
8485
&& isStyledFunction((node as ts.PropertyAccessExpression).expression, identifiers);
8586
}
8687

88+
function isStyledKeyframesIdentifier(name: string, { keyframes = ['keyframes'] }: CustomStyledIdentifiers) {
89+
return keyframes.indexOf(name) >= 0;
90+
}
91+
92+
function isStyledCssIdentifier(name: string, { css = ['css'] }: CustomStyledIdentifiers) {
93+
return css.indexOf(name) >= 0;
94+
}
95+
96+
function isStyledCreateGlobalStyleIdentifier(name: string, { createGlobalStyle = ['createGlobalStyle'] }: CustomStyledIdentifiers) {
97+
return createGlobalStyle.indexOf(name) >= 0;
98+
}
99+
100+
function isMinifyableStyledFunction(node: ts.Node, identifiers: CustomStyledIdentifiers) {
101+
return isStyledFunction(node, identifiers)
102+
|| (
103+
isIdentifier(node)
104+
&& (
105+
isStyledKeyframesIdentifier(node.text, identifiers)
106+
|| isStyledCssIdentifier(node.text, identifiers)
107+
|| isStyledCreateGlobalStyleIdentifier(node.text, identifiers)
108+
)
109+
);
110+
}
111+
87112
function defaultGetDisplayName(filename: string, bindingName: string | undefined): string | undefined {
88113
return bindingName;
89114
}
@@ -135,7 +160,7 @@ export function createTransformer({
135160
if (
136161
minify
137162
&& isTaggedTemplateExpression(node)
138-
&& isStyledFunction(node.tag, identifiers)
163+
&& isMinifyableStyledFunction(node.tag, identifiers)
139164
) {
140165
const minifiedTemplate = minifyTemplate(node.template);
141166
if (minifiedTemplate && minifiedTemplate !== node.template) {

src/models/Options.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,25 @@ export interface CustomStyledIdentifiers {
4848
* @defaultValue `['attrs']`
4949
*/
5050
attrs?: string[];
51+
52+
/**
53+
* Identifiers of `keyframes` function.
54+
*
55+
* @defaultValue `['keyframes']`
56+
*/
57+
keyframes?: string[];
58+
59+
/**
60+
* Identifiers of `css` function.
61+
*
62+
* @defaultValue `['css']`
63+
*/
64+
css?: string[];
65+
66+
/**
67+
* Identifiers of `createGlobalStyle` function.
68+
*
69+
* @defaultValue `['createGlobalStyle']`
70+
*/
71+
createGlobalStyle?: string[];
5172
}

0 commit comments

Comments
 (0)