diff --git a/packages/next/build/webpack/config/blocks/css/index.ts b/packages/next/build/webpack/config/blocks/css/index.ts index 68304c2350a4..d6b734173ff0 100644 --- a/packages/next/build/webpack/config/blocks/css/index.ts +++ b/packages/next/build/webpack/config/blocks/css/index.ts @@ -22,7 +22,7 @@ const regexCssModules = /\.module\.css$/ const regexSassGlobal = /(? !file +const regexClientEntry = /^$/ /** * Mark a rule as removable if built-in CSS support is disabled @@ -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), }), @@ -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), @@ -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), }), @@ -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: { diff --git a/packages/next/build/webpack/plugins/client-entry-plugin.ts b/packages/next/build/webpack/plugins/client-entry-plugin.ts index 46ba3398fed6..122e69cd0d82 100644 --- a/packages/next/build/webpack/plugins/client-entry-plugin.ts +++ b/packages/next/build/webpack/plugins/client-entry-plugin.ts @@ -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 ( diff --git a/test/e2e/app-dir/app/pages/index.js b/test/e2e/app-dir/app/pages/index.js index 562e43cca6a3..dd991a34e30a 100644 --- a/test/e2e/app-dir/app/pages/index.js +++ b/test/e2e/app-dir/app/pages/index.js @@ -1,8 +1,10 @@ import Link from 'next/link' +import styles from '../styles/shared.module.css' + export default function Page(props) { return ( <> -

hello from pages/index

+

hello from pages/index

Dashboard ) diff --git a/test/e2e/app-dir/app/styles/shared.module.css b/test/e2e/app-dir/app/styles/shared.module.css new file mode 100644 index 000000000000..dccdb6cb678f --- /dev/null +++ b/test/e2e/app-dir/app/styles/shared.module.css @@ -0,0 +1,3 @@ +.content { + color: blueviolet; +} diff --git a/test/e2e/app-dir/index.test.ts b/test/e2e/app-dir/index.test.ts index 90ebe52e76bc..c05c91f88f29 100644 --- a/test/e2e/app-dir/index.test.ts +++ b/test/e2e/app-dir/index.test.ts @@ -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(