Unverified Commit 7ced791d authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix CSS modules imported from client components in app dir (#38310)

* css modules support

* add test case
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 9d22da47
import type { webpack } from 'next/dist/compiled/webpack/webpack'
export function getClientStyleLoader({
isAppDir,
isDevelopment,
assetPrefix,
}: {
isAppDir: boolean
isDevelopment: boolean
assetPrefix: string
}): webpack.RuleSetUseItem {
......@@ -11,24 +13,33 @@ export function getClientStyleLoader({
return {
loader: 'next-style-loader',
options: {
// By default, style-loader injects CSS into the bottom
// of <head>. This causes ordering problems between dev
// and prod. To fix this, we render a <noscript> tag as
// an anchor for the styles to be placed before. These
// styles will be applied _before_ <style jsx global>.
insert: function (element: Node) {
// These elements should always exist. If they do not,
// this code should fail.
var anchorElement = document.querySelector(
'#__next_css__DO_NOT_USE__'
)!
var parentNode = anchorElement.parentNode! // Normally <head>
insert: isAppDir
? function (element: Node) {
// There is currently no anchor element in <head>.
// We temporarily insert the element as the last child
// of the first <head>.
const head = document.querySelector('head')!
head.insertBefore(element, head.lastChild)
}
: function (element: Node) {
// By default, style-loader injects CSS into the bottom
// of <head>. This causes ordering problems between dev
// and prod. To fix this, we render a <noscript> tag as
// an anchor for the styles to be placed before. These
// styles will be applied _before_ <style jsx global>.
// Each style tag should be placed right before our
// anchor. By inserting before and not after, we do not
// need to track the last inserted element.
parentNode.insertBefore(element, anchorElement)
},
// These elements should always exist. If they do not,
// this code should fail.
var anchorElement = document.querySelector(
'#__next_css__DO_NOT_USE__'
)!
var parentNode = anchorElement.parentNode! // Normally <head>
// Each style tag should be placed right before our
// anchor. By inserting before and not after, we do not
// need to track the last inserted element.
parentNode.insertBefore(element, anchorElement)
},
},
}
}
......
......@@ -16,6 +16,7 @@ export function getGlobalCssLoader(
// loader
loaders.push(
getClientStyleLoader({
isAppDir: !!ctx.experimental.appDir,
isDevelopment: ctx.isDevelopment,
assetPrefix: ctx.assetPrefix,
})
......
......@@ -16,6 +16,7 @@ export function getCssModuleLoader(
// loader
loaders.push(
getClientStyleLoader({
isAppDir: !!ctx.experimental.appDir,
isDevelopment: ctx.isDevelopment,
assetPrefix: ctx.assetPrefix,
})
......
import { useState, useEffect } from 'react'
import styles from './style.module.css'
export default function ClientNestedLayout({ children }) {
const [count, setCount] = useState(0)
useEffect(() => {
......@@ -7,7 +9,7 @@ export default function ClientNestedLayout({ children }) {
}, [])
return (
<>
<h1>Client Nested. Count: {count}</h1>
<h1 className={styles.red}>Client Nested. Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>{count}</button>
{children}
</>
......
......@@ -5,6 +5,8 @@ import path from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
const isDev = (global as any).isNextDev
describe('views dir', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
......@@ -285,4 +287,19 @@ describe('views dir', () => {
})
})
})
describe('css support', () => {
if (isDev) {
it('should support css modules inside client layouts', async () => {
const browser = await webdriver(next.url, '/client-nested')
// Should render h1 in red
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
})
}
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment