-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental flag for styled-components transform + add test
- Loading branch information
1 parent
af1b30b
commit 9a19263
Showing
11 changed files
with
369 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
|
||
describe('styled-components SWC transform', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
// 'next.config.js': new FileRef( | ||
// join(__dirname, 'styled-components/next.config.js') | ||
// ), | ||
pages: new FileRef(join(__dirname, 'styled-components/pages')), | ||
}, | ||
dependencies: { | ||
'styled-components': '5.3.3', | ||
}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
async function matchLogs$(browser) { | ||
let foundLog = false | ||
|
||
const browserLogs = await browser.log('browser') | ||
|
||
browserLogs.forEach((log) => { | ||
console.error({ log }) | ||
if (log.message.includes('Warning: Prop `%s` did not match.')) { | ||
foundLog = true | ||
} | ||
}) | ||
return foundLog | ||
} | ||
it('should not have hydration mismatch with styled-components transform enabled', async () => { | ||
let browser | ||
try { | ||
browser = await webdriver(next.appPort, '/') | ||
|
||
// Compile /_error | ||
await fetchViaHTTP(next.appPort, '/404') | ||
|
||
// Try 4 times to be sure there is no mismatch | ||
expect(await matchLogs$(browser)).toBe(false) | ||
await browser.refresh() | ||
expect(await matchLogs$(browser)).toBe(false) | ||
await browser.refresh() | ||
expect(await matchLogs$(browser)).toBe(false) | ||
await browser.refresh() | ||
expect(await matchLogs$(browser)).toBe(false) | ||
} finally { | ||
if (browser) { | ||
await browser.close() | ||
} | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
experimental: { | ||
styledComponents: true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createGlobalStyle, ThemeProvider } from 'styled-components' | ||
|
||
const GlobalStyle = createGlobalStyle` | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
box-sizing: border-box; | ||
} | ||
` | ||
|
||
const theme = { | ||
colors: { | ||
primary: '#0070f3', | ||
}, | ||
} | ||
|
||
export default function App({ Component, pageProps }) { | ||
return ( | ||
<> | ||
<GlobalStyle /> | ||
<ThemeProvider theme={theme}> | ||
<Component {...pageProps} /> | ||
</ThemeProvider> | ||
</> | ||
) | ||
} |
30 changes: 30 additions & 0 deletions
30
test/development/basic/styled-components/pages/_document.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Document from 'next/document' | ||
import { ServerStyleSheet } from 'styled-components' | ||
|
||
export default class MyDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
const sheet = new ServerStyleSheet() | ||
const originalRenderPage = ctx.renderPage | ||
|
||
try { | ||
ctx.renderPage = () => | ||
originalRenderPage({ | ||
enhanceApp: (App) => (props) => | ||
sheet.collectStyles(<App {...props} />), | ||
}) | ||
|
||
const initialProps = await Document.getInitialProps(ctx) | ||
return { | ||
...initialProps, | ||
styles: ( | ||
<> | ||
{initialProps.styles} | ||
{sheet.getStyleElement()} | ||
</> | ||
), | ||
} | ||
} finally { | ||
sheet.seal() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import styled, { css } from 'styled-components' | ||
const Button = styled.a` | ||
/* This renders the buttons above... Edit me! */ | ||
display: inline-block; | ||
border-radius: 3px; | ||
padding: 0.5rem 0; | ||
margin: 0.5rem 1rem; | ||
width: 11rem; | ||
background: transparent; | ||
color: white; | ||
border: 2px solid white; | ||
/* The GitHub button is a primary button | ||
* edit this to target it specifically! */ | ||
${(props) => | ||
props.primary && | ||
css` | ||
background: white; | ||
color: black; | ||
`} | ||
` | ||
|
||
export default function Home() { | ||
return ( | ||
<div> | ||
<Button | ||
href="https://github.com/styled-components/styled-components" | ||
target="_blank" | ||
rel="noopener" | ||
primary | ||
> | ||
GitHub | ||
</Button> | ||
|
||
<Button href="/docs">Documentation</Button> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.