From 83e87688112c19a3ef4b788aebe34a163e17e9e0 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Wed, 28 Nov 2018 12:35:17 -0600 Subject: [PATCH 1/3] feat(gatsby-plugin-emotion): upgrade plugin for emotion 10 support --- examples/using-emotion/.eslintrc.json | 8 -- examples/using-emotion/package.json | 8 +- examples/using-emotion/src/pages/index.js | 18 ++-- packages/gatsby-plugin-emotion/.babelrc | 1 - packages/gatsby-plugin-emotion/.npmignore | 34 -------- packages/gatsby-plugin-emotion/CHANGELOG.md | 82 ------------------- packages/gatsby-plugin-emotion/README.md | 28 ------- packages/gatsby-plugin-emotion/index.js | 1 - packages/gatsby-plugin-emotion/package.json | 13 +-- .../src/gatsby-browser.js | 3 + .../gatsby-plugin-emotion/src/gatsby-node.js | 19 +++++ .../gatsby-plugin-emotion/src/gatsby-ssr.js | 6 +- .../gatsby-plugin-emotion/src/wrap-element.js | 7 ++ 13 files changed, 55 insertions(+), 173 deletions(-) delete mode 100644 examples/using-emotion/.eslintrc.json create mode 100644 packages/gatsby-plugin-emotion/src/wrap-element.js diff --git a/examples/using-emotion/.eslintrc.json b/examples/using-emotion/.eslintrc.json deleted file mode 100644 index aadde9c0aa03d..0000000000000 --- a/examples/using-emotion/.eslintrc.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "env": { - "browser": true - }, - "globals": { - "graphql": false - } -} \ No newline at end of file diff --git a/examples/using-emotion/package.json b/examples/using-emotion/package.json index d0e305b674753..7806ec13513d3 100644 --- a/examples/using-emotion/package.json +++ b/examples/using-emotion/package.json @@ -4,8 +4,10 @@ "description": "Gatsby example site using using-emotion", "author": "Tegan Churchill ", "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", @@ -13,7 +15,6 @@ "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", @@ -21,6 +22,7 @@ "scripts": { "develop": "gatsby develop", "build": "gatsby build", + "serve": "gatsby serve", "start": "npm run develop" } } diff --git a/examples/using-emotion/src/pages/index.js b/examples/using-emotion/src/pages/index.js index b26aaa6542a4c..c5fac2bda7d34 100644 --- a/examples/using-emotion/src/pages/index.js +++ b/examples/using-emotion/src/pages/index.js @@ -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 @@ -52,11 +53,6 @@ const title = css` } ` -// Using css with object -const subtitle = css({ - color: `#bd93f9`, -}) - const IndexPage = () => ( @@ -65,12 +61,16 @@ const IndexPage = () => ( -

+

Hello World, this is my first component styled with {` `} emotion!

-

+

- -## [2.0.6](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.5...gatsby-plugin-emotion@2.0.6) (2018-10-29) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -## [2.0.5](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-rc.5...gatsby-plugin-emotion@2.0.5) (2018-09-17) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-rc.5](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-rc.4...gatsby-plugin-emotion@2.0.0-rc.5) (2018-09-11) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-rc.4](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-rc.3...gatsby-plugin-emotion@2.0.0-rc.4) (2018-09-11) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-rc.3](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-rc.2...gatsby-plugin-emotion@2.0.0-rc.3) (2018-09-11) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-rc.2](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-rc.1...gatsby-plugin-emotion@2.0.0-rc.2) (2018-09-11) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-rc.1](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-rc.0...gatsby-plugin-emotion@2.0.0-rc.1) (2018-08-29) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-rc.0](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-beta.4...gatsby-plugin-emotion@2.0.0-rc.0) (2018-08-21) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-beta.4](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-beta.3...gatsby-plugin-emotion@2.0.0-beta.4) (2018-08-20) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-beta.3](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-beta.2...gatsby-plugin-emotion@2.0.0-beta.3) (2018-07-21) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-beta.2](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-beta.1...gatsby-plugin-emotion@2.0.0-beta.2) (2018-06-20) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-beta.1](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@2.0.0-beta.0...gatsby-plugin-emotion@2.0.0-beta.1) (2018-06-17) - -**Note:** Version bump only for package gatsby-plugin-emotion - - - -# [2.0.0-beta.0](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-emotion/compare/gatsby-plugin-emotion@1.1.17...gatsby-plugin-emotion@2.0.0-beta.0) (2018-06-17) - -**Note:** Version bump only for package gatsby-plugin-emotion diff --git a/packages/gatsby-plugin-emotion/README.md b/packages/gatsby-plugin-emotion/README.md index 906ba6f9b9393..e69de29bb2d1d 100644 --- a/packages/gatsby-plugin-emotion/README.md +++ b/packages/gatsby-plugin-emotion/README.md @@ -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. - }, - }, - ], -} -``` diff --git a/packages/gatsby-plugin-emotion/index.js b/packages/gatsby-plugin-emotion/index.js index 172f1ae6a468c..e69de29bb2d1d 100644 --- a/packages/gatsby-plugin-emotion/index.js +++ b/packages/gatsby-plugin-emotion/index.js @@ -1 +0,0 @@ -// noop diff --git a/packages/gatsby-plugin-emotion/package.json b/packages/gatsby-plugin-emotion/package.json index 556201295af52..e94e97fb49586 100644 --- a/packages/gatsby-plugin-emotion/package.json +++ b/packages/gatsby-plugin-emotion/package.json @@ -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", @@ -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" + } } diff --git a/packages/gatsby-plugin-emotion/src/gatsby-browser.js b/packages/gatsby-plugin-emotion/src/gatsby-browser.js index a8b9e890525bd..8e43833468037 100644 --- a/packages/gatsby-plugin-emotion/src/gatsby-browser.js +++ b/packages/gatsby-plugin-emotion/src/gatsby-browser.js @@ -1,5 +1,6 @@ /* globals window */ import { hydrate } from "emotion" +import { wrapElement } from "./wrap-element" exports.onClientEntry = () => { if ( @@ -9,3 +10,5 @@ exports.onClientEntry = () => { hydrate(window.__EMOTION_CRITICAL_CSS_IDS__) } } + +exports.wrapRootElement = ({ element }) => wrapElement(element) diff --git a/packages/gatsby-plugin-emotion/src/gatsby-node.js b/packages/gatsby-plugin-emotion/src/gatsby-node.js index 24145c975b448..73c6ae66b04ff 100644 --- a/packages/gatsby-plugin-emotion/src/gatsby-node.js +++ b/packages/gatsby-plugin-emotion/src/gatsby-node.js @@ -1,3 +1,5 @@ +const webpack = require(`webpack`) + exports.onCreateBabelConfig = ({ actions }, pluginOptions) => { actions.setBabelPlugin({ name: `babel-plugin-emotion`, @@ -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`, + }), + ], + }) } diff --git a/packages/gatsby-plugin-emotion/src/gatsby-ssr.js b/packages/gatsby-plugin-emotion/src/gatsby-ssr.js index ee251e75f0b98..97a124cd3bc3c 100644 --- a/packages/gatsby-plugin-emotion/src/gatsby-ssr.js +++ b/packages/gatsby-plugin-emotion/src/gatsby-ssr.js @@ -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 =