diff --git a/packages/next/build/swc/options.js b/packages/next/build/swc/options.js index 362ce88a42a55..8e1a5820a350b 100644 --- a/packages/next/build/swc/options.js +++ b/packages/next/build/swc/options.js @@ -43,10 +43,7 @@ function getBaseSWCOptions({ }, }, }, - styledComponents: { - displayName: styledComponents, - ssr: styledComponents, - }, + styledComponents: styledComponents ? {} : null, } } diff --git a/test/development/basic/styled-components-disabled.test.ts b/test/development/basic/styled-components-disabled.test.ts new file mode 100644 index 0000000000000..af49dd5deb910 --- /dev/null +++ b/test/development/basic/styled-components-disabled.test.ts @@ -0,0 +1,65 @@ +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-disabled/next.config.js') + ), + pages: new FileRef(join(__dirname, 'styled-components-disabled/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) => { + if (log.message.includes('Warning: Prop `%s` did not match.')) { + foundLog = true + } + }) + return foundLog + } + it('should have hydration mismatch with styled-components transform disabled', async () => { + let browser + try { + browser = await webdriver(next.appPort, '/') + + // Compile /_error + await fetchViaHTTP(next.appPort, '/404') + + try { + // 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) + throw new Error('did not find mismatch') + } catch (err) { + // Verify that it really has the logs + expect(await matchLogs$(browser)).toBe(true) + } + } finally { + if (browser) { + await browser.close() + } + } + }) +}) diff --git a/test/development/basic/styled-components-disabled/next.config.js b/test/development/basic/styled-components-disabled/next.config.js new file mode 100644 index 0000000000000..4ba52ba2c8df6 --- /dev/null +++ b/test/development/basic/styled-components-disabled/next.config.js @@ -0,0 +1 @@ +module.exports = {} diff --git a/test/development/basic/styled-components-disabled/pages/_app.js b/test/development/basic/styled-components-disabled/pages/_app.js new file mode 100644 index 0000000000000..019f78b2afc64 --- /dev/null +++ b/test/development/basic/styled-components-disabled/pages/_app.js @@ -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 ( + <> + + + + + + ) +} diff --git a/test/development/basic/styled-components-disabled/pages/_document.js b/test/development/basic/styled-components-disabled/pages/_document.js new file mode 100644 index 0000000000000..2a59afeb93c1f --- /dev/null +++ b/test/development/basic/styled-components-disabled/pages/_document.js @@ -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(), + }) + + const initialProps = await Document.getInitialProps(ctx) + return { + ...initialProps, + styles: ( + <> + {initialProps.styles} + {sheet.getStyleElement()} + + ), + } + } finally { + sheet.seal() + } + } +} diff --git a/test/development/basic/styled-components-disabled/pages/index.js b/test/development/basic/styled-components-disabled/pages/index.js new file mode 100644 index 0000000000000..31bd1226e7689 --- /dev/null +++ b/test/development/basic/styled-components-disabled/pages/index.js @@ -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 ( +
+ + + +
+ ) +} diff --git a/test/development/basic/styled-components.test.ts b/test/development/basic/styled-components.test.ts index 99838fd36f4fb..c17cc8f7be609 100644 --- a/test/development/basic/styled-components.test.ts +++ b/test/development/basic/styled-components.test.ts @@ -10,9 +10,9 @@ describe('styled-components SWC transform', () => { beforeAll(async () => { next = await createNext({ files: { - // 'next.config.js': new FileRef( - // join(__dirname, 'styled-components/next.config.js') - // ), + 'next.config.js': new FileRef( + join(__dirname, 'styled-components/next.config.js') + ), pages: new FileRef(join(__dirname, 'styled-components/pages')), }, dependencies: { @@ -28,7 +28,6 @@ describe('styled-components SWC transform', () => { const browserLogs = await browser.log('browser') browserLogs.forEach((log) => { - console.error({ log }) if (log.message.includes('Warning: Prop `%s` did not match.')) { foundLog = true }