Skip to content

Commit

Permalink
fix: allow plugins to use gatsby-browser.js to load global styles, et…
Browse files Browse the repository at this point in the history
…c. (#10845)
  • Loading branch information
KyleAMathews authored and pieh committed Jan 8, 2019
1 parent fe1f827 commit 33a3e61
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 2 deletions.
19 changes: 19 additions & 0 deletions e2e-tests/production-runtime/cypress/integration/global-styles.js
@@ -0,0 +1,19 @@
const zIndex = `9001`

describe(`Global style from gatsby-browser.js`, () => {
beforeEach(() => {
cy.visit(`/global-style`).waitForAPI(`onRouteUpdate`)
})

it(`should apply any styles in root gatsby-browser.js`, () => {
cy.getTestElement(`global-style`).should(`have.css`, `zIndex`, zIndex)
})

it(`should apply any styles in plugin(s) gatsby-browser.js`, () => {
cy.getTestElement(`global-plugin-style`).should(
`have.css`,
`zIndex`,
zIndex
)
})
})
2 changes: 2 additions & 0 deletions e2e-tests/production-runtime/gatsby-browser.js
@@ -1,3 +1,5 @@
require(`./src/index.css`)

if (typeof window !== `undefined`) {
window.___PageComponentLifecycleCallsLog = []
}
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/production-runtime/gatsby-config.js
Expand Up @@ -4,6 +4,7 @@ module.exports = {
},
plugins: [
`gatsby-plugin-react-helmet`,
`gatsby-plugin-global-style`,
{
resolve: `gatsby-plugin-manifest`,
options: {
Expand Down
@@ -0,0 +1 @@
import './index.css'
@@ -0,0 +1,4 @@
[data-testid='global-plugin-style'] {
position: relative;
z-index: 9001; /* it's over 9000 */
}
@@ -0,0 +1 @@
// noop
@@ -0,0 +1,3 @@
{
"name": "gatsby-plugin-global-style"
}
4 changes: 4 additions & 0 deletions e2e-tests/production-runtime/src/index.css
@@ -0,0 +1,4 @@
[data-testid='global-style'] {
position: relative;
z-index: 9001; /* it's over 9000 */
}
16 changes: 16 additions & 0 deletions e2e-tests/production-runtime/src/pages/global-style.js
@@ -0,0 +1,16 @@
import React from 'react'

import Layout from '../components/layout'

const GlobalStyle = () => (
<Layout>
<h1 data-testid="global-plugin-style">
This text should have a large z-index (via `gatsby-plugin-global-style`)
</h1>
<h2 data-testid="global-style">
This text should have a large z-index (via root-level `gatsby-browser.js`)
</h2>
</Layout>
)

export default GlobalStyle
5 changes: 5 additions & 0 deletions e2e-tests/production-runtime/src/pages/index.js
Expand Up @@ -34,6 +34,11 @@ const IndexPage = ({ pageContext }) => (
Client only paths
</Link>
</li>
<li>
<Link to="/global-style/" data-testid="global-style">
gatsby-browser.js (global styles)
</Link>
</li>
</ul>
</Layout>
)
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/bootstrap/index.js
Expand Up @@ -283,10 +283,10 @@ module.exports = async (args: BootstrapArgs) => {

const envAPIs = plugin[`${env}APIs`]

// Always include the site's gatsby-browser.js if it exists as it's
// Always include gatsby-browser.js files if they exists as they're
// a handy place to include global styles and other global imports.
try {
if (env === `browser` && plugin.name === `default-site-plugin`) {
if (env === `browser`) {
return slash(
require.resolve(path.join(plugin.resolve, `gatsby-${env}`))
)
Expand Down

0 comments on commit 33a3e61

Please sign in to comment.