Skip to content

Commit 43c03eb

Browse files
author
Osama Jandali
committed
annotate injectGlobal
- annotate injectGlobal - tests now include all cases (css, keyframes, injectGlobal)
1 parent f149841 commit 43c03eb

File tree

6 files changed

+143
-3
lines changed

6 files changed

+143
-3
lines changed

src/visitors/uglifyPure.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import annotateAsPure from "@babel/helper-annotate-as-pure"
22

33
import { useUglifyPure } from '../utils/options'
4-
import { isStyled, isHelper } from '../utils/detectors'
4+
import { isStyled, isHelper, isInjectGlobalHelper } from '../utils/detectors'
55

66
export default (path, state) => {
77
if(useUglifyPure(state)){
88
if(isStyled(path.node,state) ||
99
isStyled(path.node.callee,state) ||
10-
isHelper(path.node.callee,state)
10+
isHelper(path.node.callee,state) ||
11+
isInjectGlobalHelper(path.node.callee,state)
1112
){
1213
if(path.parent.type == 'VariableDeclarator' ||
13-
path.parent.type == 'TaggedTemplateExpression'
14+
path.parent.type == 'TaggedTemplateExpression' ||
15+
path.parent.type =='ExpressionStatement'
1416
){
1517
annotateAsPure(path)
1618
}

test/__snapshots__/index.test.js.snap

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@ const WithAttrsWrapped = styled(Inner).attrs({ some: \'value\' }).withConfig({
7979
})\`\`;"
8080
`;
8181
82+
exports[`fixtures should annotate css with uglify pure comments 1`] = `
83+
"const Simple = /*#__PURE__*/styled.div.withConfig({
84+
displayName: \"before__Simple\"
85+
})([[\"{width:100%;}\"]]);
86+
87+
const Nested = /*#__PURE__*/styled.div.withConfig({
88+
displayName: \"before__Nested\"
89+
})([[\"{width:100%;}\"], [\":hover{color:papayawhip;}\"], [\" > div{background:white;}\"]]);
90+
91+
const Interpolations = /*#__PURE__*/styled.div.withConfig({
92+
displayName: \"before__Interpolations\"
93+
})([[\"{width:\", props => props.width, \";}\"]]);
94+
95+
const NestedAndInterpolations = /*#__PURE__*/styled.div.withConfig({
96+
displayName: \"before__NestedAndInterpolations\"
97+
})([[\"{width:\", props => props.width, \";}\"], [\":hover{color:\", props => props.color, \";}\"]]);
98+
99+
const SelectorInterpolation = /*#__PURE__*/styled.div.withConfig({
100+
displayName: \"before__SelectorInterpolation\"
101+
})([[\"{width:\", props => props.width, \";}\"], [\" \", props => props.selector, \"{color:papayawhip;}\"]]);
102+
103+
const RulesetInterpolationA = /*#__PURE__*/styled.div.withConfig({
104+
displayName: \"before__RulesetInterpolationA\"
105+
})([[\"{width:\", props => props.width, \";\", props => props.ruleset, \";}\"], [\":hover{color:papayawhip;}\"]]);
106+
107+
const RulesetInterpolationB = /*#__PURE__*/styled.div.withConfig({
108+
displayName: \"before__RulesetInterpolationB\"
109+
})([[\"{\", props => props.ruleset, \";width:\", props => props.width, \";}\"], [\":hover{color:papayawhip;}\"]]);
110+
111+
const Prefixes = /*#__PURE__*/styled.div.withConfig({
112+
displayName: \"before__Prefixes\"
113+
})([[\"{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;}\"]]);
114+
115+
const DoubleInterpolation = /*#__PURE__*/styled.div.withConfig({
116+
displayName: \"before__DoubleInterpolation\"
117+
})([[\"{margin:\", props => props.vert, \" \", props => props.hori, \";}\"]]);"
118+
`;
119+
120+
exports[`fixtures should annotate inject global with uglify pure comments 1`] = `"/*#__PURE__*/injectGlobal([[\"\", glob, \"\\n/*! preserve comment */html,body{margin:100000px;padding:\", test, \";}@media (max-width:999px){html,body{margin:0;}}.root{width:100%;}\"]]);"`;
121+
82122
exports[`fixtures should annotate keyframes with uglify pure comments 1`] = `"const Animation = /*#__PURE__*/keyframes([[\"@-webkit-keyframes \"], [\"{0%{opacity:0;}100%{opacity:1;}}@keyframes \"], [\"{0%{opacity:0;}100%{opacity:1;}}\"]]);"`;
83123
84124
exports[`fixtures should annotate styled calls with uglify pure comments 1`] = `
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"plugins": [
3+
["../../../src", {
4+
"preprocess": true,
5+
"ssr": false,
6+
"uglifyPure":true
7+
}]
8+
]
9+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const Simple = styled.div`width: 100%;`;
2+
3+
const Nested = styled.div`
4+
width: 100%;
5+
6+
&:hover {
7+
color: papayawhip;
8+
}
9+
10+
> div {
11+
background: white;
12+
}
13+
`;
14+
15+
const Interpolations = styled.div`
16+
width: ${props => props.width};
17+
`;
18+
19+
const NestedAndInterpolations = styled.div`
20+
width: ${props => props.width};
21+
22+
&:hover {
23+
color: ${props => props.color};
24+
}
25+
`;
26+
27+
const SelectorInterpolation = styled.div`
28+
width: ${props => props.width};
29+
30+
${props => props.selector} {
31+
color: papayawhip;
32+
}
33+
`;
34+
35+
const RulesetInterpolationA = styled.div`
36+
width: ${props => props.width};
37+
${props => props.ruleset}
38+
39+
&:hover {
40+
color: papayawhip;
41+
}
42+
`;
43+
44+
const RulesetInterpolationB = styled.div`
45+
${props => props.ruleset}
46+
width: ${props => props.width};
47+
48+
&:hover {
49+
color: papayawhip;
50+
}
51+
`;
52+
53+
54+
const Prefixes = styled.div`
55+
display: flex;
56+
align-items: center;
57+
`;
58+
59+
const DoubleInterpolation = styled.div`
60+
margin: ${props => props.vert} ${props => props.hori};
61+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"plugins": [
3+
["../../../src", {
4+
"preprocess": true,
5+
"ssr": true
6+
}]
7+
]
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
injectGlobal`
2+
${glob}
3+
4+
// comment
5+
/* comment */
6+
/*! preserve comment */
7+
8+
html, body {
9+
margin: 100000px;
10+
padding: ${test};
11+
12+
@media (max-width: 999px) {
13+
margin: 0;
14+
}
15+
}
16+
17+
.root {
18+
width: 100%;
19+
}
20+
`;

0 commit comments

Comments
 (0)