Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CSSM loader applying for both pages and app #38761

Merged
merged 5 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 15 additions & 18 deletions packages/next/build/webpack/config/blocks/css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const regexCssModules = /\.module\.css$/
const regexSassGlobal = /(?<!\.module)\.(scss|sass)$/
const regexSassModules = /\.module\.(scss|sass)$/
// 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
Expand Down Expand Up @@ -215,13 +215,12 @@ export const css = curry(async function css(
// CSS Modules are only supported in the user's application. We're
// not yet allowing CSS imports _within_ `node_modules`.
issuer: {
or: [
and: [
{
and: [ctx.rootDirectory],
not: [/node_modules/],
or: [ctx.rootDirectory, regexClientEntry],
},
clientEntryMatcher,
],
not: [/node_modules/],
},
use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}),
Expand Down Expand Up @@ -369,9 +368,13 @@ export const css = curry(async function css(
sideEffects: true,
test: regexCssGlobal,
issuer: {
or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
clientEntryMatcher,
and: [
{
or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
regexClientEntry,
],
},
],
},
use: getGlobalCssLoader(ctx, lazyPostCSSInitializer),
Expand All @@ -386,10 +389,8 @@ export const css = curry(async function css(
sideEffects: false,
test: regexCssModules,
issuer: {
or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
clientEntryMatcher,
],
and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/],
or: [regexClientEntry],
},
use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}),
Expand Down Expand Up @@ -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,
// 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,
use: {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/webpack/plugins/client-entry-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ export class ClientEntryPlugin {
!rawRequest.startsWith('.') &&
!rawRequest.startsWith('/')
? rawRequest
: mod.userRequest
: mod.resourceResolveData?.path

if (visited.has(modRequest)) return
if (!modRequest || visited.has(modRequest)) return
visited.add(modRequest)

if (
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/app-dir/app/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Link from 'next/link'
import styles from '../styles/shared.module.css'

export default function Page(props) {
return (
<>
<p>hello from pages/index</p>
<p className={styles.content}>hello from pages/index</p>
<Link href="/dashboard">Dashboard</Link>
</>
)
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/styles/shared.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: blueviolet;
}
1 change: 1 addition & 0 deletions test/e2e/app-dir/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('app dir', () => {
next = await createNext({
files: {
public: new FileRef(path.join(__dirname, 'app/public')),
styles: new FileRef(path.join(__dirname, 'app/styles')),
pages: new FileRef(path.join(__dirname, 'app/pages')),
app: new FileRef(path.join(__dirname, 'app/app')),
'next.config.js': new FileRef(
Expand Down