Skip to content

Commit dccf651

Browse files
authored
Add support for the Styled Components Babel plugin (#4722)
1 parent dc79b61 commit dccf651

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

examples/using-styled-components/package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
"version": "1.0.0",
66
"author": "Kyle Mathews <[email protected]>",
77
"dependencies": {
8-
"gatsby": "latest",
9-
"gatsby-link": "latest",
10-
"gatsby-plugin-google-analytics": "latest",
11-
"gatsby-plugin-offline": "latest",
12-
"gatsby-plugin-styled-components": "latest",
13-
"lodash": "^4.16.4",
14-
"react-typography": "^0.15.0",
15-
"slash": "^1.0.0",
16-
"styled-components": "^2.2.3",
17-
"typography": "^0.15.8",
18-
"typography-breakpoint-constants": "^0.14.0"
8+
"babel-plugin-styled-components": "^1.5.1",
9+
"gatsby": "1.9.242",
10+
"gatsby-link": "1.6.39",
11+
"gatsby-plugin-google-analytics": "1.0.25",
12+
"gatsby-plugin-offline": "1.0.15",
13+
"gatsby-plugin-styled-components": "2.0.10",
14+
"lodash": "^4.17.5",
15+
"react-typography": "^0.16.13",
16+
"slash": "^2.0.0",
17+
"styled-components": "^3.2.3",
18+
"typography": "^0.16.6",
19+
"typography-breakpoint-constants": "^0.15.10"
1920
},
2021
"keywords": [
2122
"gatsby"

packages/gatsby-plugin-styled-components/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ built-in server-side rendering support.
66

77
## Install
88

9-
`npm install --save gatsby-plugin-styled-components styled-components`
9+
`npm install --save gatsby-plugin-styled-components styled-components babel-plugin-styled-components`
1010

1111
## How to use
1212

packages/gatsby-plugin-styled-components/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"main": "index.js",
2323
"peerDependencies": {
2424
"gatsby": "^1.0.0",
25-
"styled-components": ">= 2.0.0"
25+
"styled-components": ">= 2.0.0",
26+
"babel-plugin-styled-components": ">1.5.0"
2627
},
2728
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-styled-components",
2829
"scripts": {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Add Babel plugin
2+
let babelPluginExists = false
3+
try {
4+
require.resolve(`babel-plugin-styled-components`)
5+
babelPluginExists = true
6+
} catch (e) {
7+
// Ignore
8+
}
9+
10+
exports.modifyBabelrc = ({ babelrc, stage }) => {
11+
if (babelPluginExists) {
12+
if (stage === `build-html`) {
13+
return {
14+
...babelrc,
15+
plugins: babelrc.plugins.concat([
16+
[
17+
`babel-plugin-styled-components`,
18+
{
19+
ssr: true,
20+
},
21+
],
22+
]),
23+
}
24+
}
25+
26+
return {
27+
...babelrc,
28+
plugins: babelrc.plugins.concat([`babel-plugin-styled-components`]),
29+
}
30+
}
31+
32+
return babelrc
33+
}

packages/gatsby/src/utils/babel-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ module.exports = async function babelConfig(program, stage) {
179179

180180
const normalizedConfig = normalizeConfig(babelrc, directory)
181181
let modifiedConfig = await apiRunnerNode(`modifyBabelrc`, {
182+
stage,
182183
babelrc: normalizedConfig,
183184
})
184185
if (modifiedConfig.length > 0) {

0 commit comments

Comments
 (0)