Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-emotion): upgrade plugin for emotion 10 support #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions examples/using-emotion/.eslintrc.json

This file was deleted.

8 changes: 5 additions & 3 deletions examples/using-emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
"description": "Gatsby example site using using-emotion",
"author": "Tegan Churchill <[email protected]>",
"dependencies": {
"emotion": "^9.2.12",
"emotion-server": "^9.2.12",
"@emotion/core": "^10.0.0",
"@emotion/styled": "^10.0.0",
"emotion": "^10.0.0",
"emotion-server": "^10.0.0",
"gatsby": "^2.0.0",
"gatsby-plugin-emotion": "^2.0.5",
"gatsby-plugin-google-analytics": "^2.0.5",
"gatsby-plugin-offline": "^2.0.5",
"gatsby-plugin-react-helmet": "^3.0.0",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-emotion": "^9.2.12",
"react-helmet": "^5.2.0"
},
"license": "MIT",
"main": "n/a",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve",
"start": "npm run develop"
}
}
18 changes: 9 additions & 9 deletions examples/using-emotion/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { Fragment } from "react"
import Helmet from "react-helmet"
import styled, { css, injectGlobal } from "react-emotion"
// You can import everything from "react-emotion"
import styled from "@emotion/styled"
import { injectGlobal } from "emotion"
import { css } from "@emotion/core"

// Emotion supports different styling options, all of which are supported by gatsby-plugin-emotion out of the box

Expand Down Expand Up @@ -52,11 +53,6 @@ const title = css`
}
`

// Using css with object
const subtitle = css({
color: `#bd93f9`,
})

const IndexPage = () => (
<Fragment>
<Helmet>
Expand All @@ -65,12 +61,16 @@ const IndexPage = () => (
<meta name="referrer" content="origin" />
</Helmet>
<Wrapper>
<h1 className={title}>
<h1 css={title}>
Hello World, this is my first component styled with
{` `}
<a href="https://emotion.sh/">emotion</a>!
</h1>
<p className={subtitle}>
<p
css={css({
color: `#bd93f9`,
})}
>
<a
href="https://www.gatsbyjs.org/packages/gatsby-plugin-emotion/"
// Styling “inline” with css prop
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby-plugin-emotion/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
["babel-preset-gatsby-package", { "browser": true }]
]
}

34 changes: 0 additions & 34 deletions packages/gatsby-plugin-emotion/.npmignore
Original file line number Diff line number Diff line change
@@ -1,34 +0,0 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
82 changes: 0 additions & 82 deletions packages/gatsby-plugin-emotion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,82 +0,0 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

<a name="2.0.6"></a>

## [2.0.6](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.6) (2018-10-29)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.5"></a>

## [2.0.5](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.5) (2018-09-17)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-rc.5"></a>

# [2.0.0-rc.5](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-rc.5) (2018-09-11)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-rc.4"></a>

# [2.0.0-rc.4](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-rc.4) (2018-09-11)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-rc.3"></a>

# [2.0.0-rc.3](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-rc.3) (2018-09-11)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-rc.2"></a>

# [2.0.0-rc.2](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-rc.2) (2018-09-11)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-rc.1"></a>

# [2.0.0-rc.1](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-rc.1) (2018-08-29)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-rc.0"></a>

# [2.0.0-rc.0](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-rc.0) (2018-08-21)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-beta.4"></a>

# [2.0.0-beta.4](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-beta.4) (2018-08-20)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-beta.3"></a>

# [2.0.0-beta.3](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-beta.3) (2018-07-21)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-beta.2"></a>

# [2.0.0-beta.2](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-beta.2) (2018-06-20)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-beta.1"></a>

# [2.0.0-beta.1](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-beta.1) (2018-06-17)

**Note:** Version bump only for package gatsby-plugin-emotion

<a name="2.0.0-beta.0"></a>

# [2.0.0-beta.0](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/[email protected]@2.0.0-beta.0) (2018-06-17)

**Note:** Version bump only for package gatsby-plugin-emotion
28 changes: 0 additions & 28 deletions packages/gatsby-plugin-emotion/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +0,0 @@
# gatsby-plugin-emotion

Provide support for using the css-in-js library
[Emotion](https://github.com/emotion-js/emotion) including server side
rendering.

## Install

```
npm install --save gatsby-plugin-emotion emotion emotion-server react-emotion
```

## How to use

Add the plugin to your `gatsby-config.js`.

```js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-emotion`,
options: {
// Accepts all options defined by `babel-plugin-emotion` plugin.
},
},
],
}
```
1 change: 0 additions & 1 deletion packages/gatsby-plugin-emotion/index.js
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
// noop
13 changes: 7 additions & 6 deletions packages/gatsby-plugin-emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"dependencies": {
"@babel/runtime": "^7.0.0"
"@babel/runtime": "^7.0.0",
"babel-plugin-transform-react-jsx": "^6.24.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
Expand All @@ -24,15 +25,15 @@
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"emotion": "7 || 8 || 9",
"emotion-server": "7 || 8 || 9",
"gatsby": ">2.0.0-alpha"
"@emotion/core": "^10.0.0",
"emotion": "^10.0.0",
"emotion-server": "^10.0.0",
"gatsby": ">2.0.0"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion",
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__"
},
"gitHead": "5bd5aebe066b9875354a81a4b9ed98722731c465"
}
}
3 changes: 3 additions & 0 deletions packages/gatsby-plugin-emotion/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* globals window */
import { hydrate } from "emotion"
import { wrapElement } from "./wrap-element"

exports.onClientEntry = () => {
if (
Expand All @@ -9,3 +10,5 @@ exports.onClientEntry = () => {
hydrate(window.__EMOTION_CRITICAL_CSS_IDS__)
}
}

exports.wrapRootElement = ({ element }) => wrapElement(element)
19 changes: 19 additions & 0 deletions packages/gatsby-plugin-emotion/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const webpack = require(`webpack`)

exports.onCreateBabelConfig = ({ actions }, pluginOptions) => {
actions.setBabelPlugin({
name: `babel-plugin-emotion`,
Expand All @@ -8,4 +10,21 @@ exports.onCreateBabelConfig = ({ actions }, pluginOptions) => {
...(pluginOptions ? pluginOptions : {}),
},
})

actions.setBabelPlugin({
name: `babel-plugin-transform-react-jsx`,
options: {
pragma: `Emotion.jsx`,
},
})
}

exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
plugins: [
new webpack.ProvidePlugin({
Emotion: `@emotion/core`,
}),
],
})
}
6 changes: 5 additions & 1 deletion packages/gatsby-plugin-emotion/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import React from "react"
import { renderToString } from "react-dom/server"
import { extractCritical } from "emotion-server"

import { wrapElement } from "./wrap-element"

exports.replaceRenderer = ({
bodyComponent,
replaceBodyHTMLString,
setHeadComponents,
}) => {
const { html, ids, css } = extractCritical(renderToString(bodyComponent))
const { html, ids, css } = extractCritical(
renderToString(wrapElement(bodyComponent))
)

const criticalStyle = <style dangerouslySetInnerHTML={{ __html: css }} />
const criticalIds = (
Expand Down
7 changes: 7 additions & 0 deletions packages/gatsby-plugin-emotion/src/wrap-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react"
import { cache } from "emotion"
import { CacheProvider } from "@emotion/core"

export const wrapElement = element => (
<CacheProvider value={cache}>{element}</CacheProvider>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary if people are using injectGlobal, keyframes, css, etc. from the emotion package, which seems likely.

)