Unverified Commit 653d5e71 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix missing client entry matcher and simplify imports path (#38691)

### Changes

#### Add missing virtual client entry matcher for css modules
client entry module has no file path, add the missing file matcher for client css modules loader

#### Use relative paths for imports in client entry to avoid module resolving failure.

if it's `next/head` or other 3rd party installed path, just import it instead of the resolved file path
parent 67e10a70
...@@ -21,6 +21,8 @@ const regexCssModules = /\.module\.css$/ ...@@ -21,6 +21,8 @@ const regexCssModules = /\.module\.css$/
// RegExps for Syntactically Awesome Style Sheets // RegExps for Syntactically Awesome Style Sheets
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
const clientEntryMatcher = (file: string) => !file
/** /**
* Mark a rule as removable if built-in CSS support is disabled * Mark a rule as removable if built-in CSS support is disabled
...@@ -213,8 +215,13 @@ export const css = curry(async function css( ...@@ -213,8 +215,13 @@ 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: {
and: [ctx.rootDirectory], or: [
not: [/node_modules/], {
and: [ctx.rootDirectory],
not: [/node_modules/],
},
clientEntryMatcher,
],
}, },
use: getCssModuleLoader(ctx, lazyPostCSSInitializer), use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}), }),
...@@ -364,8 +371,7 @@ export const css = curry(async function css( ...@@ -364,8 +371,7 @@ export const css = curry(async function css(
issuer: { issuer: {
or: [ or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] }, { and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
// Also match the virtual client entry which doesn't have file path clientEntryMatcher,
(filePath) => !filePath,
], ],
}, },
use: getGlobalCssLoader(ctx, lazyPostCSSInitializer), use: getGlobalCssLoader(ctx, lazyPostCSSInitializer),
...@@ -382,8 +388,7 @@ export const css = curry(async function css( ...@@ -382,8 +388,7 @@ export const css = curry(async function css(
issuer: { issuer: {
or: [ or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] }, { and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
// Also match the virtual client entry which doesn't have file path clientEntryMatcher,
(filePath) => !filePath,
], ],
}, },
use: getCssModuleLoader(ctx, lazyPostCSSInitializer), use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
......
...@@ -75,10 +75,20 @@ export class ClientEntryPlugin { ...@@ -75,10 +75,20 @@ export class ClientEntryPlugin {
const clientComponentImports: string[] = [] const clientComponentImports: string[] = []
function filterClientComponents(dependency: any) { function filterClientComponents(dependency: any) {
const module = compilation.moduleGraph.getResolvedModule(dependency) const mod = compilation.moduleGraph.getResolvedModule(dependency)
if (!module) return if (!mod) return
// Keep client imports as simple
// native or installed js module: -> raw request, e.g. next/head
// client js or css: -> user request
const rawRequest = mod.rawRequest || ''
const modRequest =
!rawRequest.endsWith('.css') &&
!rawRequest.startsWith('.') &&
!rawRequest.startsWith('/')
? rawRequest
: mod.userRequest
const modRequest = module.userRequest
if (visited.has(modRequest)) return if (visited.has(modRequest)) return
visited.add(modRequest) visited.add(modRequest)
...@@ -90,7 +100,7 @@ export class ClientEntryPlugin { ...@@ -90,7 +100,7 @@ export class ClientEntryPlugin {
} }
compilation.moduleGraph compilation.moduleGraph
.getOutgoingConnections(module) .getOutgoingConnections(mod)
.forEach((connection: any) => { .forEach((connection: any) => {
filterClientComponents(connection.dependency) filterClientComponents(connection.dependency)
}) })
......
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