Unverified Commit bb9bb648 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix global CSS loader behavior when appDir is enabled (#45739)

The current behavior is, when `appDir` is enabled, global CSS should be
allowed to be imported from anywhere (because components can be re-used
by both pages and app). Changes in #45619 made it not behaving correctly
and this PR fixes it.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent d73cceb2
...@@ -337,22 +337,32 @@ export const css = curry(async function css( ...@@ -337,22 +337,32 @@ export const css = curry(async function css(
) )
} else { } else {
// External CSS files are allowed to be loaded when any of the following is true: // External CSS files are allowed to be loaded when any of the following is true:
// - hasAppDir: If the issuerLayer is RSC // - hasAppDir: all CSS files are allowed
// - If the CSS file is located in `node_modules` // - If the CSS file is located in `node_modules`
// - If the CSS file is located in another package in a monorepo (outside of the current rootDir) // - If the CSS file is located in another package in a monorepo (outside of the current rootDir)
// - If the issuer is pages/_app (matched later) // - If the issuer is pages/_app (matched later)
const allowedExternalCSSImports = { const allowedPagesGlobalCSSPath = ctx.hasAppDir
and: [ ? undefined
{ : {
or: [ and: [
/node_modules/,
{ {
not: [ctx.rootDirectory], or: [
/node_modules/,
{
not: [ctx.rootDirectory],
},
],
}, },
], ],
}, }
], const allowedPagesGlobalCSSIssuer = ctx.hasAppDir
} ? undefined
: shouldIncludeExternalCSSImports
? undefined
: {
and: [ctx.rootDirectory],
not: [/node_modules/],
}
fns.push( fns.push(
loader({ loader({
...@@ -387,26 +397,16 @@ export const css = curry(async function css( ...@@ -387,26 +397,16 @@ export const css = curry(async function css(
markRemovable({ markRemovable({
sideEffects: true, sideEffects: true,
test: regexCssGlobal, test: regexCssGlobal,
include: allowedExternalCSSImports, include: allowedPagesGlobalCSSPath,
issuer: shouldIncludeExternalCSSImports issuer: allowedPagesGlobalCSSIssuer,
? undefined
: {
and: [ctx.rootDirectory],
not: [/node_modules/],
},
issuerLayer: PAGES_LAYER_RULE, issuerLayer: PAGES_LAYER_RULE,
use: getGlobalCssLoader(ctx, false, lazyPostCSSInitializer), use: getGlobalCssLoader(ctx, false, lazyPostCSSInitializer),
}), }),
markRemovable({ markRemovable({
sideEffects: true, sideEffects: true,
test: regexSassGlobal, test: regexSassGlobal,
include: allowedExternalCSSImports, include: allowedPagesGlobalCSSPath,
issuer: shouldIncludeExternalCSSImports issuer: allowedPagesGlobalCSSIssuer,
? undefined
: {
and: [ctx.rootDirectory],
not: [/node_modules/],
},
issuerLayer: PAGES_LAYER_RULE, issuerLayer: PAGES_LAYER_RULE,
use: getGlobalCssLoader( use: getGlobalCssLoader(
ctx, ctx,
......
...@@ -2223,7 +2223,7 @@ createNextDescribe( ...@@ -2223,7 +2223,7 @@ createNextDescribe(
// Pages directory shouldn't be affected when `appDir` is enabled // Pages directory shouldn't be affected when `appDir` is enabled
describe('pages dir', () => { describe('pages dir', () => {
it('should include css modules after page transition', async () => { it('should include css modules and global css after page transition', async () => {
const browser = await next.browser('/css-modules/page1') const browser = await next.browser('/css-modules/page1')
await browser.elementByCss('a').click() await browser.elementByCss('a').click()
await browser.waitForElementByCss('#page2') await browser.waitForElementByCss('#page2')
...@@ -2232,6 +2232,12 @@ createNextDescribe( ...@@ -2232,6 +2232,12 @@ createNextDescribe(
`window.getComputedStyle(document.querySelector('h1')).backgroundColor` `window.getComputedStyle(document.querySelector('h1')).backgroundColor`
) )
).toBe('rgb(205, 92, 92)') ).toBe('rgb(205, 92, 92)')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('.page-2')).backgroundColor`
)
).toBe('rgb(255, 228, 181)')
}) })
}) })
} }
......
import Link from 'next/link' import Link from 'next/link'
import classes from './page1.module.css' import classes from './page1.module.css'
import './page1.css'
export default function Page() { export default function Page() {
return ( return (
<> <>
<h1 className={classes.box}>Page 1</h1> <h1 className={classes.box}>Page 1</h1>
<Link href="/css-modules/page2">Page 2</Link> <div className="page-1">
<Link href="/css-modules/page2">Page 2</Link>
</div>
</> </>
) )
} }
import Link from 'next/link' import Link from 'next/link'
import classes from './page2.module.css' import classes from './page2.module.css'
import './page2.css'
export default function Page() { export default function Page() {
return ( return (
...@@ -7,7 +8,9 @@ export default function Page() { ...@@ -7,7 +8,9 @@ export default function Page() {
<h1 id="page2" className={classes.box}> <h1 id="page2" className={classes.box}>
Page 2 Page 2
</h1> </h1>
<Link href="/css-modules/page1">Page 1</Link> <div className="page-2">
<Link href="/css-modules/page1">Page 1</Link>
</div>
</> </>
) )
} }
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