Skip to content

Commit 79eba06

Browse files
authored
Merge pull request #117 from osamajandali/add-uglify-pure
Add Uglify Pure Comments
2 parents b211beb + 391c366 commit 79eba06

File tree

31 files changed

+236
-20
lines changed

31 files changed

+236
-20
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"displayName"
3535
],
3636
"dependencies": {
37+
"@babel/helper-annotate-as-pure": "^7.0.0-beta.37",
3738
"babel-types": "^6.26.0",
3839
"stylis": "^3.0.0"
3940
},

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import uglifyPure from './visitors/uglifyPure'
12
import minify from './visitors/minify'
23
import displayNameAndId from './visitors/displayNameAndId'
34
import templateLiterals from './visitors/templateLiterals'
45
import assignStyledRequired from './visitors/assignStyledRequired'
56
import { noParserImportDeclaration, noParserRequireCallExpression } from './visitors/noParserImport'
67

8+
79
export default function({ types: t }) {
810
return {
911
visitor: {
1012
ImportDeclaration(path, state) {
1113
noParserImportDeclaration(path, state)
1214
},
1315
CallExpression(path, state) {
16+
uglifyPure(path, state)
1417
noParserRequireCallExpression(path, state)
1518
},
1619
TaggedTemplateExpression(path, state) {

src/utils/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export const useFileName = (state) =>getOption(state, 'fileName')
88
export const useMinify = (state) => getOption(state, 'minify')
99
export const useCSSPreprocessor = (state) => getOption(state, 'preprocess', false) // EXPERIMENTAL
1010
export const useTranspileTemplateLiterals = (state) => getOption(state, 'transpileTemplateLiterals')
11+
export const useUglifyPure = (state) => getOption(state, 'uglifyPure')

src/visitors/templateLiterals/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
useCSSPreprocessor,
3-
useTranspileTemplateLiterals
3+
useTranspileTemplateLiterals,
4+
useUglifyPure
45
} from '../../utils/options'
56

67
import preprocess from './preprocess'

src/visitors/uglifyPure.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import annotateAsPure from "@babel/helper-annotate-as-pure"
2+
3+
import { useUglifyPure } from '../utils/options'
4+
import { isStyled, isHelper } from '../utils/detectors'
5+
6+
export default (path, state) => {
7+
if(useUglifyPure(state)){
8+
if(isStyled(path.node,state) ||
9+
isStyled(path.node.callee,state) ||
10+
isHelper(path.node.callee,state)
11+
){
12+
if(path.parent.type == 'VariableDeclarator' ||
13+
path.parent.type == 'TaggedTemplateExpression'
14+
){
15+
annotateAsPure(path)
16+
}
17+
}
18+
}
19+
}

test/__snapshots__/index.test.js.snap

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,72 @@ 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 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;}}\"]]);"`;
121+
122+
exports[`fixtures should annotate styled calls with uglify pure comments 1`] = `
123+
"const Test = /*#__PURE__*/styled.div.withConfig({
124+
displayName: \'Test\'
125+
})\`width:100%;\`;
126+
const Test2 = /*#__PURE__*/styled(\'div\').withConfig({
127+
displayName: \'Test2\'
128+
})\`\`;
129+
const Test3 = true ? /*#__PURE__*/styled.div.withConfig({
130+
displayName: \'Test3\'
131+
})\`\` : /*#__PURE__*/styled.div.withConfig({
132+
displayName: \'Test3\'
133+
})\`\`;
134+
const styles = { One: /*#__PURE__*/styled.div.withConfig({
135+
displayName: \'One\'
136+
})\`\` };
137+
let Component;
138+
Component = /*#__PURE__*/styled.div.withConfig({
139+
displayName: \'Component\'
140+
})\`\`;
141+
const WrappedComponent = /*#__PURE__*/styled(Inner).withConfig({
142+
displayName: \'WrappedComponent\'
143+
})\`\`;
144+
const notStyled = /*#__PURE__*/styled.div(\'\'); // not transpiled by styled components but should add pure comment
145+
const normalFunc = add(5, 3);"
146+
`;
147+
82148
exports[`fixtures should minify css in helpers 1`] = `
83149
"import { css, keyframes } from \'styled-components\';
84150

test/fixtures/01-add-display-names/.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"ssr": false,
55
"fileName": false,
66
"transpileTemplateLiterals": false,
7-
"preprocess": false
7+
"preprocess": false,
8+
"uglifyPure":false
89
}]
910
]
1011
}

test/fixtures/02-add-identifier/.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"displayName": false,
55
"fileName": false,
66
"transpileTemplateLiterals": false,
7-
"ssr": true
7+
"ssr": true,
8+
"uglifyPure":false
89
}]
910
]
1011
}

test/fixtures/03-add-identifier-and-display-name/.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
["../../../src", {
44
"fileName": false,
55
"transpileTemplateLiterals": false,
6-
"ssr": true
6+
"ssr": true,
7+
"uglifyPure":false
78
}]
89
]
910
}

test/fixtures/04-track-the-imported-variable/.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
["../../../src", {
44
"fileName": false,
55
"transpileTemplateLiterals": false,
6-
"ssr": true
6+
"ssr": true,
7+
"uglifyPure":false
78
}]
89
]
910
}

0 commit comments

Comments
 (0)