Unverified Commit 69fe4eee authored by Shu Ding's avatar Shu Ding Committed by GitHub

Refactor style loader code (#46490)

Should fix the vanilla-extract issue in #46374.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent ce234fb9
......@@ -271,7 +271,10 @@ export const css = curry(async function css(
issuerLayer: APP_LAYER_RULE,
use: [
require.resolve('../../../loaders/next-flight-css-loader'),
...getCssModuleLoader(ctx, true, lazyPostCSSInitializer),
...getCssModuleLoader(
{ ...ctx, isAppDir: true },
lazyPostCSSInitializer
),
],
})
: null,
......@@ -279,7 +282,10 @@ export const css = curry(async function css(
sideEffects: false,
test: regexCssModules,
issuerLayer: PAGES_LAYER_RULE,
use: getCssModuleLoader(ctx, false, lazyPostCSSInitializer),
use: getCssModuleLoader(
{ ...ctx, isAppDir: false },
lazyPostCSSInitializer
),
}),
].filter(nonNullable),
}),
......@@ -299,8 +305,7 @@ export const css = curry(async function css(
use: [
require.resolve('../../../loaders/next-flight-css-loader'),
...getCssModuleLoader(
ctx,
true,
{ ...ctx, isAppDir: true },
lazyPostCSSInitializer,
sassPreprocessors
),
......@@ -312,8 +317,7 @@ export const css = curry(async function css(
test: regexSassModules,
issuerLayer: PAGES_LAYER_RULE,
use: getCssModuleLoader(
ctx,
false,
{ ...ctx, isAppDir: false },
lazyPostCSSInitializer,
sassPreprocessors
),
......@@ -398,7 +402,10 @@ export const css = curry(async function css(
issuerLayer: APP_LAYER_RULE,
use: [
require.resolve('../../../loaders/next-flight-css-loader'),
...getGlobalCssLoader(ctx, true, lazyPostCSSInitializer),
...getGlobalCssLoader(
{ ...ctx, isAppDir: true },
lazyPostCSSInitializer
),
],
}),
markRemovable({
......@@ -408,8 +415,7 @@ export const css = curry(async function css(
use: [
require.resolve('../../../loaders/next-flight-css-loader'),
...getGlobalCssLoader(
ctx,
true,
{ ...ctx, isAppDir: true },
lazyPostCSSInitializer,
sassPreprocessors
),
......@@ -423,7 +429,10 @@ export const css = curry(async function css(
include: allowedPagesGlobalCSSPath,
issuer: allowedPagesGlobalCSSIssuer,
issuerLayer: PAGES_LAYER_RULE,
use: getGlobalCssLoader(ctx, false, lazyPostCSSInitializer),
use: getGlobalCssLoader(
{ ...ctx, isAppDir: false },
lazyPostCSSInitializer
),
}),
markRemovable({
sideEffects: true,
......@@ -432,8 +441,7 @@ export const css = curry(async function css(
issuer: allowedPagesGlobalCSSIssuer,
issuerLayer: PAGES_LAYER_RULE,
use: getGlobalCssLoader(
ctx,
false,
{ ...ctx, isAppDir: false },
lazyPostCSSInitializer,
sassPreprocessors
),
......@@ -450,7 +458,10 @@ export const css = curry(async function css(
sideEffects: true,
test: regexCssGlobal,
issuer: { and: [ctx.customAppFile] },
use: getGlobalCssLoader(ctx, false, lazyPostCSSInitializer),
use: getGlobalCssLoader(
{ ...ctx, isAppDir: false },
lazyPostCSSInitializer
),
}),
],
}),
......@@ -461,8 +472,7 @@ export const css = curry(async function css(
test: regexSassGlobal,
issuer: { and: [ctx.customAppFile] },
use: getGlobalCssLoader(
ctx,
false,
{ ...ctx, isAppDir: false },
lazyPostCSSInitializer,
sassPreprocessors
),
......
......@@ -2,15 +2,19 @@ import type { webpack } from 'next/dist/compiled/webpack/webpack'
export function getClientStyleLoader({
hasAppDir,
isAppDir,
isDevelopment,
assetPrefix,
}: {
hasAppDir: boolean
isAppDir?: boolean
isDevelopment: boolean
assetPrefix: string
}): webpack.RuleSetUseItem {
const shouldEnableApp = typeof isAppDir === 'boolean' ? isAppDir : hasAppDir
// Keep next-style-loader for development mode in `pages/`
if (isDevelopment && !hasAppDir) {
if (isDevelopment && !shouldEnableApp) {
return {
loader: 'next-style-loader',
options: {
......
......@@ -6,7 +6,6 @@ import { cssFileResolve } from './file-resolve'
export function getGlobalCssLoader(
ctx: ConfigurationContext,
hasAppDir: boolean,
postcss: any,
preProcessors: readonly webpack.RuleSetUseItem[] = []
): webpack.RuleSetUseItem[] {
......@@ -17,7 +16,8 @@ export function getGlobalCssLoader(
// loader
loaders.push(
getClientStyleLoader({
hasAppDir: hasAppDir,
hasAppDir: ctx.hasAppDir,
isAppDir: ctx.isAppDir,
isDevelopment: ctx.isDevelopment,
assetPrefix: ctx.assetPrefix,
})
......
......@@ -6,7 +6,6 @@ import { getCssModuleLocalIdent } from './getCssModuleLocalIdent'
export function getCssModuleLoader(
ctx: ConfigurationContext,
hasAppDir: boolean,
postcss: any,
preProcessors: readonly webpack.RuleSetUseItem[] = []
): webpack.RuleSetUseItem[] {
......@@ -17,7 +16,8 @@ export function getCssModuleLoader(
// loader
loaders.push(
getClientStyleLoader({
hasAppDir: hasAppDir,
hasAppDir: ctx.hasAppDir,
isAppDir: ctx.isAppDir,
isDevelopment: ctx.isDevelopment,
assetPrefix: ctx.assetPrefix,
})
......
......@@ -2,7 +2,10 @@ import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type { NextConfigComplete } from '../../../server/config-shared'
export type ConfigurationContext = {
// If the `appDir` feature is enabled
hasAppDir: boolean
// If the current rule matches a resource in the app layer
isAppDir?: boolean
supportedBrowsers: string[] | undefined
rootDirectory: string
customAppFile: RegExp | undefined
......
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