Skip to content

Commit 7024de4

Browse files
authored
Merge pull request #162 from styled-components/adjust-version-detector
looser version detection method
2 parents 20b92f5 + 4d91ba6 commit 7024de4

File tree

7 files changed

+85
-26
lines changed

7 files changed

+85
-26
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
"styled-components": "^3.4.5"
2727
},
2828
"dependencies": {
29-
"lodash": "^4.17.10",
30-
"semver": "^5.5.1"
29+
"lodash": "^4.17.10"
3130
},
3231
"resolutions": {
3332
"babel-core": "7.0.0-bridge.0"
@@ -56,4 +55,4 @@
5655
"<rootDir>/test/whitespaceTrimmingSerializer.js"
5756
]
5857
}
59-
}
58+
}

src/utils/options.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import semver from 'semver'
2-
import styledPkg from 'styled-components/package.json'
3-
41
function getOption({ opts }, name, defaultValue = true) {
52
return opts[name] === undefined || opts[name] === null
63
? defaultValue
@@ -19,4 +16,8 @@ export const useTranspileTemplateLiterals = state =>
1916
* then use a lighter-weight version of s-c (v4+) since those element names don't need to be kept around
2017
* ahead of time.
2118
*/
22-
export const useNoTags = () => semver.satisfies(styledPkg.version, '>= 4')
19+
export const useNoTags = () =>
20+
parseInt(
21+
require('styled-components/package.json').version.split('.')[0],
22+
10
23+
) >= 4
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`should swap out the import if styled-components v4+ is detected 1`] = `
4-
"\\"use strict\\";
5-
6-
var _styledComponents = _interopRequireDefault(require(\\"styled-components\\"));
3+
exports[`should not swap out the import if styled-components v3 is detected 1`] = `
4+
"import styled from 'styled-components';
5+
const Named = styled(\\"div\\").withConfig({
6+
displayName: \\"example__Named\\",
7+
componentId: \\"ghf2ss-0\\"
8+
})([\\"width:100%;\\"]);
9+
const NamedWithInterpolation = styled(\\"div\\").withConfig({
10+
displayName: \\"example__NamedWithInterpolation\\",
11+
componentId: \\"ghf2ss-1\\"
12+
})([\\"color:\\", \\";\\"], props => props.color);
13+
const Wrapped = styled(Inner).withConfig({
14+
displayName: \\"example__Wrapped\\",
15+
componentId: \\"ghf2ss-2\\"
16+
})([\\"color:red;\\"]);"
17+
`;
718

8-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19+
exports[`should swap out the import if styled-components v4 beta is detected 1`] = `
20+
"import styled from \\"styled-components/no-tags\\";
21+
const Named = styled(\\"div\\").withConfig({
22+
displayName: \\"example__Named\\",
23+
componentId: \\"ghf2ss-0\\"
24+
})([\\"width:100%;\\"]);
25+
const NamedWithInterpolation = styled(\\"div\\").withConfig({
26+
displayName: \\"example__NamedWithInterpolation\\",
27+
componentId: \\"ghf2ss-1\\"
28+
})([\\"color:\\", \\";\\"], props => props.color);
29+
const Wrapped = styled(Inner).withConfig({
30+
displayName: \\"example__Wrapped\\",
31+
componentId: \\"ghf2ss-2\\"
32+
})([\\"color:red;\\"]);"
33+
`;
934

10-
var Named = _styledComponents.default.div\`
11-
width: 100%;
12-
\`;
13-
var NamedWithInterpolation = _styledComponents.default.div\`
14-
color: \${function (props) {
15-
return props.color;
16-
}};
17-
\`;
18-
var Wrapped = (0, _styledComponents.default)(Inner)\`
19-
color: red;
20-
\`;"
35+
exports[`should swap out the import if styled-components v4+ is detected 1`] = `
36+
"import styled from \\"styled-components/no-tags\\";
37+
const Named = styled(\\"div\\").withConfig({
38+
displayName: \\"example__Named\\",
39+
componentId: \\"ghf2ss-0\\"
40+
})([\\"width:100%;\\"]);
41+
const NamedWithInterpolation = styled(\\"div\\").withConfig({
42+
displayName: \\"example__NamedWithInterpolation\\",
43+
componentId: \\"ghf2ss-1\\"
44+
})([\\"color:\\", \\";\\"], props => props.color);
45+
const Wrapped = styled(Inner).withConfig({
46+
displayName: \\"example__Wrapped\\",
47+
componentId: \\"ghf2ss-2\\"
48+
})([\\"color:red;\\"]);"
2149
`;

test/no-tags/example/.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": [
3+
[
4+
"../../../src"
5+
]
6+
]
7+
}
File renamed without changes.

test/no-tags/index.test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import { transformFileSync } from '@babel/core'
22
import * as path from 'path'
33

4-
jest.mock('styled-components/package.json', () => ({ version: '4.0.0' }))
4+
let mockVersion
5+
6+
jest.mock('styled-components/package.json', () => ({ version: mockVersion }))
7+
8+
beforeEach(() => jest.resetModules())
9+
10+
it('should not swap out the import if styled-components v3 is detected', () => {
11+
mockVersion = '3.0.0'
12+
13+
const fixturePath = path.join(__dirname + '/example/index.js')
14+
const fixture = transformFileSync(fixturePath).code
15+
16+
expect(fixture).toMatchSnapshot()
17+
})
518

619
it('should swap out the import if styled-components v4+ is detected', () => {
7-
const fixturePath = path.join(__dirname + '/example.js')
20+
mockVersion = '4.0.0'
21+
22+
const fixturePath = path.join(__dirname + '/example/index.js')
23+
const fixture = transformFileSync(fixturePath).code
24+
25+
expect(fixture).toMatchSnapshot()
26+
})
27+
28+
it('should swap out the import if styled-components v4 beta is detected', () => {
29+
mockVersion = '4.0.0-beta.0'
30+
31+
const fixturePath = path.join(__dirname + '/example/index.js')
832
const fixture = transformFileSync(fixturePath).code
933

1034
expect(fixture).toMatchSnapshot()

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3596,7 +3596,7 @@ sax@^1.2.4:
35963596
version "5.3.0"
35973597
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
35983598

3599-
semver@^5.4.1, semver@^5.5.0, semver@^5.5.1:
3599+
semver@^5.4.1, semver@^5.5.0:
36003600
version "5.5.1"
36013601
resolved "https://registry.npmjs.org/semver/-/semver-5.5.1.tgz#7dfdd8814bdb7cabc7be0fb1d734cfb66c940477"
36023602

0 commit comments

Comments
 (0)