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