Fix app dir vanilla-extract support (#48306)

Closes #48222.

This PR introduces a new utility function that checks the resource path,
module type, and loaders to replace the existing CSS regex in the Flight
loaders and plugins. The new function is more robust and can be extended
to correctly support other libraries such as `vanilla-extract`.

cc @SuttonJack and @SukkaW (this could be related to the other issue
we've discussed too).
This commit is contained in:
Shu Ding 2023-04-13 00:58:30 +02:00 committed by GitHub
parent 1d300456c0
commit d105b23f7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 16 deletions

View file

@ -15,6 +15,25 @@ export function isClientComponentModule(mod: {
export const regexCSS = /\.(css|scss|sass)(\?.*)?$/
// This function checks if a module is able to emit CSS resources. You should
// never only rely on a single regex to do that.
export function isCSSMod(mod: {
resource: string
type?: string
loaders?: { loader: string }[]
}): boolean {
return !!(
mod.type === 'css/mini-extract' ||
(mod.resource && regexCSS.test(mod.resource)) ||
mod.loaders?.some(
({ loader }) =>
loader.includes('next-style-loader/index.js') ||
loader.includes('mini-css-extract-plugin/loader.js') ||
loader.includes('@vanilla-extract/webpack-plugin/loader/')
)
)
}
export function getActions(mod: {
resource: string
buildInfo: any

View file

@ -27,7 +27,7 @@ import {
generateActionId,
getActions,
isClientComponentModule,
regexCSS,
isCSSMod,
} from '../loaders/utils'
import { traverseModules, forEachEntryModule } from '../utils'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
@ -354,7 +354,7 @@ export class ClientReferenceEntryPlugin {
const resource = mod.resource
const modId = resource
if (modId) {
if (regexCSS.test(modId)) {
if (isCSSMod(mod)) {
cssImportsForChunk[entryName].add(modId)
}
}
@ -506,8 +506,7 @@ export class ClientReferenceEntryPlugin {
compilation.moduleGraph.getResolvedModule(dependencyToFilter)
if (!mod) return
const rawRequest = mod.rawRequest
const isCSS = regexCSS.test(rawRequest)
const isCSS = isCSSMod(mod)
// We have to always use the resolved request here to make sure the
// server and client are using the same module path (required by RSC), as

View file

@ -12,7 +12,7 @@ import {
SYSTEM_ENTRYPOINTS,
} from '../../../shared/lib/constants'
import { relative, sep } from 'path'
import { isClientComponentModule, regexCSS } from '../loaders/utils'
import { isClientComponentModule, isCSSMod } from '../loaders/utils'
import { getProxiedPluginState } from '../../build-context'
import { traverseModules } from '../utils'
@ -157,17 +157,7 @@ export class ClientReferenceManifestPlugin {
mod: webpack.NormalModule,
chunkCSS: string[]
) => {
const isCSSModule =
regexCSS.test(mod.resource) ||
mod.type === 'css/mini-extract' ||
(!!mod.loaders &&
(dev
? mod.loaders.some((item) =>
item.loader.includes('next-style-loader/index.js')
)
: mod.loaders.some((item) =>
item.loader.includes('mini-css-extract-plugin/loader.js')
)))
const isCSSModule = isCSSMod(mod)
// Skip all modules from the pages folder. CSS modules are a special case
// as they are generated by mini-css-extract-plugin and these modules