Skip to content

Commit 6975b52

Browse files
authored
Merge pull request #156 from styled-components/undo-v4-requirement
only rewrite the styled-components import if >= v4
2 parents 14039d8 + f8da22c commit 6975b52

File tree

9 files changed

+243
-61
lines changed

9 files changed

+243
-61
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ Then add it to your babel configuration:
2323
}
2424
```
2525

26-
## styled-components v4 beta
27-
28-
If you are using the styled-components v4 beta, you'll also need to use the beta version of this plugin:
29-
30-
```
31-
npm install --save-dev babel-plugin-styled-components@beta
32-
```
33-
34-
Older versions should technically work, but some of the bundle size reduction benefits are only realized with the newer code!
35-
3626
## Documentation
3727

3828
**The documentation for this plugin lives on [the styled-components website](https://www.styled-components.com/docs/tooling#babel-plugin)!**

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
"babel-preset-env": "^1.2.2",
2121
"jest": "^17.0.3",
2222
"prettier": "^1.12.1",
23-
"rimraf": "^2.6.2"
23+
"rimraf": "^2.6.2",
24+
"styled-components": "^3.4.5"
2425
},
2526
"dependencies": {
26-
"@babel/helper-annotate-as-pure": "^7.0.0-beta.37",
27-
"lodash": "^4.17.10"
27+
"lodash": "^4.17.10",
28+
"semver": "^5.5.1"
2829
},
2930
"peerDependencies": {
30-
"styled-components": ">= 4.0"
31+
"styled-components": ">= 2"
3132
},
3233
"scripts": {
3334
"clean": "rimraf lib",
@@ -50,4 +51,4 @@
5051
"<rootDir>/test/whitespaceTrimmingSerializer.js"
5152
]
5253
}
53-
}
54+
}

src/utils/options.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import semver from 'semver'
2+
import styledPkg from 'styled-components/package.json'
3+
14
function getOption({ opts }, name, defaultValue = true) {
25
return opts[name] === undefined || opts[name] === null
36
? defaultValue
@@ -10,3 +13,10 @@ export const useFileName = state => getOption(state, 'fileName')
1013
export const useMinify = state => getOption(state, 'minify')
1114
export const useTranspileTemplateLiterals = state =>
1215
getOption(state, 'transpileTemplateLiterals')
16+
17+
/**
18+
* When using the babel plugin, we desugar styled.div to styled('div'), which means we can
19+
* then use a lighter-weight version of s-c (v4+) since those element names don't need to be kept around
20+
* ahead of time.
21+
*/
22+
export const useNoTags = () => semver.satisfies(styledPkg.version, '>= 4')
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { useNoTags } from '../utils/options'
2+
13
/**
2-
* When using the Babel plugin, we desugar styled.div to styled('div'), which means we can
3-
* then use a lighter-weight version of s-c since those element names don't need to be kept around
4+
* When using the babel plugin, we desugar styled.div to styled('div'), which means we can
5+
* then use a lighter-weight version of s-c (v4+) since those element names don't need to be kept around
46
* ahead of time.
57
*/
68
export default t => path => {
7-
if (path.node.source.value === 'styled-components') {
9+
if (useNoTags() && path.node.source.value === 'styled-components') {
810
path.node.source = t.stringLiteral('styled-components/no-tags')
911
}
1012
}

test/__snapshots__/index.test.js.snap

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const TestCallExpression = styled_default.default(Test).withConfig({
103103
`;
104104
105105
exports[`fixtures should minify css in helpers 1`] = `
106-
"import { css, keyframes } from \'styled-components/no-tags\';
106+
"import { css, keyframes } from \'styled-components\';
107107
108108
const key = keyframes\`to{transform:rotate(360deg);}\`;
109109
@@ -119,31 +119,31 @@ var _templateObject = _taggedTemplateLiteral([\'width:100%;\'], [\'width:100%;\'
119119
_templateObject4 = _taggedTemplateLiteral([\'color:red;\'], [\'color:red;\']),
120120
_templateObject5 = _taggedTemplateLiteral([\'&:hover{color:blue;}\'], [\'&:hover{color:blue;}\']);
121121
122-
var _noTags = require(\'styled-components/no-tags\');
122+
var _styledComponents = require(\'styled-components\');
123123
124-
var _noTags2 = _interopRequireDefault(_noTags);
124+
var _styledComponents2 = _interopRequireDefault(_styledComponents);
125125
126126
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
127127
128128
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
129129
130-
var Simple = (0, _noTags2.default)(\'div\')(_templateObject);
130+
var Simple = (0, _styledComponents2.default)(\'div\')(_templateObject);
131131
132-
var Interpolation = (0, _noTags2.default)(\'div\')(_templateObject2, function (props) {
132+
var Interpolation = (0, _styledComponents2.default)(\'div\')(_templateObject2, function (props) {
133133
return props.text;
134134
});
135135
136-
var SpecialCharacters = (0, _noTags2.default)(\'div\')(_templateObject3, function (props) {
136+
var SpecialCharacters = (0, _styledComponents2.default)(\'div\')(_templateObject3, function (props) {
137137
return props.text;
138138
});
139139
140-
var Comment = (0, _noTags2.default)(\'div\')(_templateObject4);
140+
var Comment = (0, _styledComponents2.default)(\'div\')(_templateObject4);
141141
142-
var Parens = (0, _noTags2.default)(\'div\')(_templateObject5);"
142+
var Parens = (0, _styledComponents2.default)(\'div\')(_templateObject5);"
143143
`;
144144
145145
exports[`fixtures should minify css to use without transpilation 1`] = `
146-
"import styled from \'styled-components/no-tags\';
146+
"import styled from \'styled-components\';
147147
148148
const Simple = styled(\'div\')\`width:100%;\`;
149149
@@ -198,13 +198,13 @@ const Test7 = styled(\'div\').withConfig({
198198
`;
199199
200200
exports[`fixtures should not use private api if not required 1`] = `
201-
"import styled from \'styled-components/no-tags\';
201+
"import styled from \'styled-components\';
202202
203203
const Test = styled(\'div\')\`width:100%;\`;"
204204
`;
205205
206206
exports[`fixtures should track the imported variable 1`] = `
207-
"import s from \"styled-components/no-tags\";
207+
"import s from \"styled-components\";
208208
209209
const Test = s(\"div\").withConfig({
210210
displayName: \"Test\",
@@ -254,47 +254,47 @@ const TestCallExpression = styled_default.default(Test).withConfig({
254254
exports[`fixtures should transpile template literals with config 1`] = `
255255
"\'use strict\';
256256
257-
var _noTags = require(\'styled-components/no-tags\');
257+
var _styledComponents = require(\'styled-components\');
258258
259-
var _noTags2 = _interopRequireDefault(_noTags);
259+
var _styledComponents2 = _interopRequireDefault(_styledComponents);
260260
261261
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
262262
263-
var Named = (0, _noTags2.default)(\'div\').withConfig({
263+
var Named = (0, _styledComponents2.default)(\'div\').withConfig({
264264
displayName: \'transpile-template-literals-with-config__Named\'
265265
})([\'\\n width: 100%;\\n\']);
266266
267-
var NamedWithInterpolation = (0, _noTags2.default)(\'div\').withConfig({
267+
var NamedWithInterpolation = (0, _styledComponents2.default)(\'div\').withConfig({
268268
displayName: \'transpile-template-literals-with-config__NamedWithInterpolation\'
269269
})([\'\\n color: \', \';\\n\'], function (color) {
270270
return props.color;
271271
});
272272
273-
var Wrapped = (0, _noTags2.default)(Inner).withConfig({
273+
var Wrapped = (0, _styledComponents2.default)(Inner).withConfig({
274274
displayName: \'transpile-template-literals-with-config__Wrapped\'
275275
})([\'color: red;\']);"
276276
`;
277277
278278
exports[`fixtures should transpile template literals without config 1`] = `
279279
"\'use strict\';
280280
281-
var _noTags = require(\'styled-components/no-tags\');
281+
var _styledComponents = require(\'styled-components\');
282282
283-
var _noTags2 = _interopRequireDefault(_noTags);
283+
var _styledComponents2 = _interopRequireDefault(_styledComponents);
284284
285285
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
286286
287-
var Named = (0, _noTags2.default)(\'div\')([\'\\n width: 100%;\\n\']);
287+
var Named = (0, _styledComponents2.default)(\'div\')([\'\\n width: 100%;\\n\']);
288288
289-
var NamedWithInterpolation = (0, _noTags2.default)(\'div\')([\'\\n color: \', \';\\n\'], function (color) {
289+
var NamedWithInterpolation = (0, _styledComponents2.default)(\'div\')([\'\\n color: \', \';\\n\'], function (color) {
290290
return props.color;
291291
});
292292
293-
var Wrapped = (0, _noTags2.default)(Inner)([\'color: red;\']);"
293+
var Wrapped = (0, _styledComponents2.default)(Inner)([\'color: red;\']);"
294294
`;
295295
296296
exports[`fixtures should use file name 1`] = `
297-
"import styled from \"styled-components/no-tags\";
297+
"import styled from \"styled-components\";
298298
299299
const Test = styled(\"div\").withConfig({
300300
displayName: \"use-file-name__Test\",
@@ -319,5 +319,5 @@ exports[`fixtures should work with hoisted default as import 1`] = `
319319
displayName: \'work-with-hoisted-default-as-import__Test\',
320320
componentId: \'sc-1te5tgi-0\'
321321
})\`width:100%;\`;
322-
import { default as s, css } from \'styled-components/no-tags\';"
322+
import { default as s, css } from \'styled-components\';"
323323
`;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
exports[`test should swap out the import if styled-components v4+ is detected 1`] = `
2+
"import styled from \'styled-components/no-tags\';
3+
4+
const Named = styled(\'div\').withConfig({
5+
displayName: \'unknown__Named\',
6+
componentId: \'sc-2vipsm-0\'
7+
})([\'width:100%;\']);
8+
9+
const NamedWithInterpolation = styled(\'div\').withConfig({
10+
displayName: \'unknown__NamedWithInterpolation\',
11+
componentId: \'sc-2vipsm-1\'
12+
})([\'color:\', \';\'], props => props.color);
13+
14+
const Wrapped = styled(Inner).withConfig({
15+
displayName: \'unknown__Wrapped\',
16+
componentId: \'sc-2vipsm-2\'
17+
})([\'color:red;\']);"
18+
`;

test/no-tags/example.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import styled from 'styled-components'
2+
3+
const Named = styled.div`
4+
width: 100%;
5+
`
6+
7+
const NamedWithInterpolation = styled.div`
8+
color: ${props => props.color};
9+
`
10+
11+
const Wrapped = styled(Inner)`
12+
color: red;
13+
`

test/no-tags/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { transform } from 'babel-core'
2+
import * as fs from 'fs'
3+
4+
jest.mock('styled-components/package.json', () => ({ version: '4.0.0' }))
5+
6+
it('should swap out the import if styled-components v4+ is detected', () => {
7+
const fixture = transform(
8+
fs.readFileSync(__dirname + '/example.js', 'utf8'),
9+
{ plugins: ['./src'] }
10+
).code
11+
expect(fixture).toMatchSnapshot()
12+
})

0 commit comments

Comments
 (0)