Skip to content

Commit f149841

Browse files
author
Osama Jandali
committed
Pure comments specific for styled typed only
- Move the function to separate file. - Check for isStyled and isHelper to only annotate them and not to mess up with normal calls - Add new two fixtures to test the new function
1 parent e1a00fb commit f149841

File tree

27 files changed

+125
-28
lines changed

27 files changed

+125
-28
lines changed

src/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import annotateAsPure from "@babel/helper-annotate-as-pure"
2-
1+
import uglifyPure from './visitors/uglifyPure'
32
import minify from './visitors/minify'
43
import displayNameAndId from './visitors/displayNameAndId'
54
import templateLiterals from './visitors/templateLiterals'
65
import assignStyledRequired from './visitors/assignStyledRequired'
76
import { noParserImportDeclaration, noParserRequireCallExpression } from './visitors/noParserImport'
8-
import { useUglifyPure } from './utils/options'
7+
98

109
export default function({ types: t }) {
1110
return {
@@ -14,11 +13,7 @@ export default function({ types: t }) {
1413
noParserImportDeclaration(path, state)
1514
},
1615
CallExpression(path, state) {
17-
if(useUglifyPure(state)){
18-
if(path.parent.type == 'VariableDeclarator' || path.parent.type == 'TaggedTemplateExpression'){
19-
annotateAsPure(path.node)
20-
}
21-
}
16+
uglifyPure(path, state)
2217
noParserRequireCallExpression(path, state)
2318
},
2419
TaggedTemplateExpression(path, state) {

src/utils/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +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', false)
11+
export const useUglifyPure = (state) => getOption(state, 'uglifyPure')

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ const WithAttrsWrapped = styled(Inner).attrs({ some: \'value\' }).withConfig({
7979
})\`\`;"
8080
`;
8181
82+
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;}}\"]]);"`;
83+
84+
exports[`fixtures should annotate styled calls with uglify pure comments 1`] = `
85+
"const Test = /*#__PURE__*/styled.div.withConfig({
86+
displayName: \'Test\'
87+
})\`width:100%;\`;
88+
const Test2 = /*#__PURE__*/styled(\'div\').withConfig({
89+
displayName: \'Test2\'
90+
})\`\`;
91+
const Test3 = true ? /*#__PURE__*/styled.div.withConfig({
92+
displayName: \'Test3\'
93+
})\`\` : /*#__PURE__*/styled.div.withConfig({
94+
displayName: \'Test3\'
95+
})\`\`;
96+
const styles = { One: /*#__PURE__*/styled.div.withConfig({
97+
displayName: \'One\'
98+
})\`\` };
99+
let Component;
100+
Component = /*#__PURE__*/styled.div.withConfig({
101+
displayName: \'Component\'
102+
})\`\`;
103+
const WrappedComponent = /*#__PURE__*/styled(Inner).withConfig({
104+
displayName: \'WrappedComponent\'
105+
})\`\`;
106+
const notStyled = /*#__PURE__*/styled.div(\'\'); // not transpiled by styled components but should add pure comment
107+
const normalFunc = add(5, 3);"
108+
`;
109+
82110
exports[`fixtures should minify css in helpers 1`] = `
83111
"import { css, keyframes } from \'styled-components\';
84112

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
}

test/fixtures/05-use-file-name/.babelrc

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

test/fixtures/06-work-with-hoisted-default-as-import/.babelrc

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

0 commit comments

Comments
 (0)