Skip to content

Commit

Permalink
Disable css-in-js transform for server layer in RSC
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 5, 2022
1 parent 4a4119e commit 1bf800c
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 25 deletions.
10 changes: 9 additions & 1 deletion packages/next-swc/crates/core/src/lib.rs
Expand Up @@ -90,6 +90,9 @@ pub struct TransformOptions {
#[serde(default)]
pub server_components: Option<react_server_components::Config>,

#[serde(default)]
pub styled_jsx: Option<bool>,

#[serde(default)]
pub styled_components: Option<styled_components::Config>,

Expand Down Expand Up @@ -159,7 +162,12 @@ where
)),
_ => Either::Right(noop()),
},
styled_jsx::styled_jsx(cm.clone(), file.name.clone()),
match opts.styled_jsx {
Some(true) =>
Either::Left(styled_jsx::styled_jsx(cm.clone(), file.name.clone())),
_ =>
Either::Right(noop())
},
hook_optimizer::hook_optimizer(),
match &opts.styled_components {
Some(config) => Either::Left(styled_components::styled_components(
Expand Down
15 changes: 7 additions & 8 deletions packages/next/build/swc/options.js
Expand Up @@ -36,9 +36,6 @@ function getBaseSWCOptions({
relativeFilePathFromRoot,
hasServerComponents,
}) {
if (filename.includes('page.js')) {
console.log('isServerLayer', isServerLayer, filename)
}
const parserConfig = getParserOptions({ filename, jsConfig })
const paths = jsConfig?.compilerOptions?.paths
const enableDecorators = Boolean(
Expand Down Expand Up @@ -121,12 +118,14 @@ function getBaseSWCOptions({
: nextConfig?.compiler?.reactRemoveProperties,
modularizeImports: nextConfig?.experimental?.modularizeImports,
relay: nextConfig?.compiler?.relay,
...(!isServerLayer
? {
styledComponents: getStyledComponentsOptions(nextConfig, development),
// Disable css-in-js transform on server layer for server components
...(isServerLayer
? {}
: {
emotion: getEmotionOptions(nextConfig, development),
}
: undefined),
styledComponents: getStyledComponentsOptions(nextConfig, development),
styledJsx: true,
}),
serverComponents: hasServerComponents
? {
isServer: !!isServerLayer,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -1626,7 +1626,7 @@ export default async function getBaseWebpackConfig(
{
...codeCondition,
issuerLayer: WEBPACK_LAYERS.middleware,
use: getBabelOrSwcLoader(),
use: defaultLoaders.babel,
},
...(hasServerComponents
? [
Expand Down
3 changes: 3 additions & 0 deletions packages/next/server/config-schema.ts
Expand Up @@ -265,6 +265,9 @@ const configSchema = {
},
] as any,
},
appDir: {
type: 'boolean',
},
externalDir: {
type: 'boolean',
},
Expand Down
24 changes: 16 additions & 8 deletions test/e2e/app-dir/rsc-basic.test.ts
Expand Up @@ -76,8 +76,6 @@ describe('app dir - react server components', () => {
expect(homeHTML).toMatch(/^<!DOCTYPE html><html/)
expect(homeHTML).toContain('component:index.server')
expect(homeHTML).toContain('header:test-util')
// support esm module on server side
expect(homeHTML).toContain('random-module-instance')

const inlineFlightContents = []
const $ = cheerio.load(homeHTML)
Expand Down Expand Up @@ -247,12 +245,17 @@ describe('app dir - react server components', () => {
})

it('should handle external async module libraries correctly', async () => {
const html = await renderViaHTTP(next.url, '/external-imports')
expect(html).toContain('module type:esm-export')
expect(html).toContain('export named:named')
expect(html).toContain('export value:123')
expect(html).toContain('export array:4,5,6')
expect(html).toContain('export object:{x:1}')
const clientHtml = await renderViaHTTP(next.url, '/external-imports/client')
const serverHtml = await renderViaHTTP(next.url, '/external-imports/server')

expect(clientHtml).toContain('module type:esm-export')
expect(clientHtml).toContain('export named:named')
expect(clientHtml).toContain('export value:123')
expect(clientHtml).toContain('export array:4,5,6')
expect(clientHtml).toContain('export object:{x:1}')

// support esm module on server side
expect(serverHtml).toContain('random-module-instance')
})

it('should handle various kinds of exports correctly', async () => {
Expand Down Expand Up @@ -314,6 +317,11 @@ describe('app dir - react server components', () => {

// from styled-components
expect(head).toMatch(/{color:(\s*)blue;?}/)

// css-in-js like styled-jsx in server components won't be transformed
expect(html).toMatch(
/\<style\>\.this-wont-be-transformed\{color:purple;\}<\/style\>/
)
})

it('should stick to the url without trailing /page suffix', async () => {
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/rsc-basic/app/css-in-js/page.js
Expand Up @@ -6,6 +6,11 @@ export default function Page() {
<div>
<Comp />
<StyledComp />
<style jsx>{`
.this-wont-be-transformed {
color: purple;
}
`}</style>
</div>
)
}
@@ -0,0 +1,9 @@
import { name } from 'random-module-instance'

export default function Page() {
return (
<div>
<p>{name}</p>
</div>
)
}
7 changes: 0 additions & 7 deletions test/e2e/app-dir/rsc-basic/app/page.js
@@ -1,6 +1,5 @@
import Nav from '../components/nav'
import { headers } from 'next/dist/client/components/hooks-server'
// import { name } from 'random-module-instance'

const envVar = process.env.ENV_VAR_TEST
const headerKey = 'x-next-test-client'
Expand All @@ -11,15 +10,9 @@ export default function Index() {

return (
<div>
<style jsx>{`
.this-wont-be-transformed {
color: blue;
}
`}</style>
<h1>{`component:index.server`}</h1>
<div>{'env:' + envVar}</div>
<div>{'header:' + header}</div>
{/* <p className='this-wont-be-transformed'>{name}</p> */}
<Nav />
</div>
)
Expand Down

0 comments on commit 1bf800c

Please sign in to comment.