Unverified Commit 7e5cb510 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix CSSM loader applying for both pages and app (#38761)

## Bug

x-ref: #38691

* Previous configured loader.issue results into a single function and will bail during next build, tune the rule set conditions to make it work for both pages and app dir
* prefer to use `mod.resourceResolveData?.path` instead of `mod.userRequest` since userRquest contains the applied loaders info

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent 54f87245
...@@ -22,7 +22,7 @@ const regexCssModules = /\.module\.css$/ ...@@ -22,7 +22,7 @@ const regexCssModules = /\.module\.css$/
const regexSassGlobal = /(?<!\.module)\.(scss|sass)$/ const regexSassGlobal = /(?<!\.module)\.(scss|sass)$/
const regexSassModules = /\.module\.(scss|sass)$/ const regexSassModules = /\.module\.(scss|sass)$/
// Also match the virtual client entry which doesn't have file path // Also match the virtual client entry which doesn't have file path
const clientEntryMatcher = (file: string) => !file const regexClientEntry = /^$/
/** /**
* Mark a rule as removable if built-in CSS support is disabled * Mark a rule as removable if built-in CSS support is disabled
...@@ -215,13 +215,12 @@ export const css = curry(async function css( ...@@ -215,13 +215,12 @@ export const css = curry(async function css(
// CSS Modules are only supported in the user's application. We're // CSS Modules are only supported in the user's application. We're
// not yet allowing CSS imports _within_ `node_modules`. // not yet allowing CSS imports _within_ `node_modules`.
issuer: { issuer: {
or: [ and: [
{ {
and: [ctx.rootDirectory], or: [ctx.rootDirectory, regexClientEntry],
not: [/node_modules/],
}, },
clientEntryMatcher,
], ],
not: [/node_modules/],
}, },
use: getCssModuleLoader(ctx, lazyPostCSSInitializer), use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}), }),
...@@ -369,9 +368,13 @@ export const css = curry(async function css( ...@@ -369,9 +368,13 @@ export const css = curry(async function css(
sideEffects: true, sideEffects: true,
test: regexCssGlobal, test: regexCssGlobal,
issuer: { issuer: {
or: [ and: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] }, {
clientEntryMatcher, or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
regexClientEntry,
],
},
], ],
}, },
use: getGlobalCssLoader(ctx, lazyPostCSSInitializer), use: getGlobalCssLoader(ctx, lazyPostCSSInitializer),
...@@ -386,10 +389,8 @@ export const css = curry(async function css( ...@@ -386,10 +389,8 @@ export const css = curry(async function css(
sideEffects: false, sideEffects: false,
test: regexCssModules, test: regexCssModules,
issuer: { issuer: {
or: [ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/],
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] }, or: [regexClientEntry],
clientEntryMatcher,
],
}, },
use: getCssModuleLoader(ctx, lazyPostCSSInitializer), use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}), }),
...@@ -429,12 +430,8 @@ export const css = curry(async function css( ...@@ -429,12 +430,8 @@ export const css = curry(async function css(
? { ? {
// If it's inside the app dir, but not importing from a layout file, // If it's inside the app dir, but not importing from a layout file,
// throw an error. // throw an error.
or: [ and: [ctx.rootDirectory],
{ not: [/layout(\.client|\.server)?\.(js|mjs|jsx|ts|tsx)$/],
and: [ctx.rootDirectory],
not: [/layout(\.client|\.server)?\.(js|mjs|jsx|ts|tsx)$/],
},
],
} }
: undefined, : undefined,
use: { use: {
......
...@@ -87,9 +87,9 @@ export class ClientEntryPlugin { ...@@ -87,9 +87,9 @@ export class ClientEntryPlugin {
!rawRequest.startsWith('.') && !rawRequest.startsWith('.') &&
!rawRequest.startsWith('/') !rawRequest.startsWith('/')
? rawRequest ? rawRequest
: mod.userRequest : mod.resourceResolveData?.path
if (visited.has(modRequest)) return if (!modRequest || visited.has(modRequest)) return
visited.add(modRequest) visited.add(modRequest)
if ( if (
......
import Link from 'next/link' import Link from 'next/link'
import styles from '../styles/shared.module.css'
export default function Page(props) { export default function Page(props) {
return ( return (
<> <>
<p>hello from pages/index</p> <p className={styles.content}>hello from pages/index</p>
<Link href="/dashboard">Dashboard</Link> <Link href="/dashboard">Dashboard</Link>
</> </>
) )
......
...@@ -23,6 +23,7 @@ describe('app dir', () => { ...@@ -23,6 +23,7 @@ describe('app dir', () => {
next = await createNext({ next = await createNext({
files: { files: {
public: new FileRef(path.join(__dirname, 'app/public')), public: new FileRef(path.join(__dirname, 'app/public')),
styles: new FileRef(path.join(__dirname, 'app/styles')),
pages: new FileRef(path.join(__dirname, 'app/pages')), pages: new FileRef(path.join(__dirname, 'app/pages')),
app: new FileRef(path.join(__dirname, 'app/app')), app: new FileRef(path.join(__dirname, 'app/app')),
'next.config.js': new FileRef( 'next.config.js': new FileRef(
......
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