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-styled-components): Add ability to disable vendor prefixes #33147

Merged
merged 5 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
10 changes: 10 additions & 0 deletions packages/gatsby-plugin-styled-components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ options: {
```

Note: The `ssr` option will be ignored. Gatsby will apply it automatically when needed.

### Disabling vendor prefixing

If you don't require vendor prefixes for adding legacy CSS properties then this can be disabled by supplying the `disableVendorPrefixes` option:

```js
options: {
disableVendorPrefixes: true
}
```
11 changes: 11 additions & 0 deletions packages/gatsby-plugin-styled-components/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react"
import { StyleSheetManager } from "styled-components"

// eslint-disable-next-line react/prop-types,react/display-name
exports.wrapRootElement = ({ element }, pluginOptions) => (
<StyleSheetManager
disableVendorPrefixes={pluginOptions?.disableVendorPrefixes === true}
>
{element}
</StyleSheetManager>
)
6 changes: 5 additions & 1 deletion packages/gatsby-plugin-styled-components/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ exports.pluginOptionsSchema = ({ Joi }) =>
.description(
`By default minifiers cannot properly perform dead code elimination on styled components because they are assumed to have side effects. This enables "pure annotations" to tell the compiler that they do not have side effects.`
),
disableVendorPrefixes: Joi.boolean()
.default(false)
.description(`Disables vendor prefixing`),
})

exports.onCreateBabelConfig = ({ stage, actions }, pluginOptions) => {
const ssr = stage === `build-html` || stage === `build-javascript`
const { disableVendorPrefixes: _, ...babelOptions } = pluginOptions

actions.setBabelPlugin({
name: `babel-plugin-styled-components`,
stage,
options: { ...pluginOptions, ssr },
options: { ...babelOptions, ssr },
})
}
11 changes: 9 additions & 2 deletions packages/gatsby-plugin-styled-components/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import { ServerStyleSheet, StyleSheetManager } from "styled-components"
const sheetByPathname = new Map()

// eslint-disable-next-line react/prop-types,react/display-name
exports.wrapRootElement = ({ element, pathname }) => {
exports.wrapRootElement = ({ element, pathname }, pluginOptions) => {
const sheet = new ServerStyleSheet()
sheetByPathname.set(pathname, sheet)
return <StyleSheetManager sheet={sheet.instance}>{element}</StyleSheetManager>
return (
<StyleSheetManager
sheet={sheet.instance}
disableVendorPrefixes={pluginOptions?.disableVendorPrefixes}
>
{element}
</StyleSheetManager>
)
}

exports.onRenderBody = ({ setHeadComponents, pathname }) => {
Expand Down