Skip to content

Commit 4a2236d

Browse files
authored
Dedupe CSS Regexes (vercel#10206)
1 parent a3f1d65 commit 4a2236d

File tree

1 file changed

+19
-9
lines changed
  • packages/next/build/webpack/config/blocks/css

1 file changed

+19
-9
lines changed

packages/next/build/webpack/config/blocks/css/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import {
1313
} from './messages'
1414
import { getPostCssPlugins } from './plugins'
1515

16+
// RegExps for Stylesheets
17+
const regexCssAll = /\.css$/
18+
const regexCssGlobal = /(?<!\.module)\.css$/
19+
const regexCssModules = /\.module\.css$/
20+
1621
function getClientStyleLoader({
1722
isDevelopment,
1823
assetPrefix,
@@ -106,7 +111,7 @@ export const css = curry(async function css(
106111
loader({
107112
oneOf: [
108113
{
109-
test: /\.css$/,
114+
test: regexCssAll,
110115
// Use a loose regex so we don't have to crawl the file system to
111116
// find the real file name (if present).
112117
issuer: { test: /pages[\\/]_document\./ },
@@ -133,7 +138,7 @@ export const css = curry(async function css(
133138
// via the `pure` mode in `css-loader`.
134139
sideEffects: false,
135140
// CSS Modules are activated via this specific extension.
136-
test: /\.module\.css$/,
141+
test: regexCssModules,
137142
// CSS Modules are only supported in the user's application. We're
138143
// not yet allowing CSS imports _within_ `node_modules`.
139144
issuer: {
@@ -191,7 +196,7 @@ export const css = curry(async function css(
191196
loader({
192197
oneOf: [
193198
{
194-
test: /\.module\.css$/,
199+
test: regexCssModules,
195200
use: {
196201
loader: 'error-loader',
197202
options: {
@@ -206,7 +211,9 @@ export const css = curry(async function css(
206211
if (ctx.isServer) {
207212
fns.push(
208213
loader({
209-
oneOf: [{ test: /\.css$/, use: require.resolve('ignore-loader') }],
214+
oneOf: [
215+
{ test: regexCssGlobal, use: require.resolve('ignore-loader') },
216+
],
210217
})
211218
)
212219
} else if (ctx.customAppFile) {
@@ -219,7 +226,7 @@ export const css = curry(async function css(
219226
// no side-effects.
220227
// See https://github.com/webpack/webpack/issues/6571
221228
sideEffects: true,
222-
test: /\.css$/,
229+
test: regexCssGlobal,
223230
issuer: { include: ctx.customAppFile },
224231

225232
use: [
@@ -257,7 +264,7 @@ export const css = curry(async function css(
257264
loader({
258265
oneOf: [
259266
{
260-
test: /\.css$/,
267+
test: regexCssGlobal,
261268
issuer: { include: [/node_modules/] },
262269
use: {
263270
loader: 'error-loader',
@@ -275,7 +282,7 @@ export const css = curry(async function css(
275282
loader({
276283
oneOf: [
277284
{
278-
test: /\.css$/,
285+
test: regexCssGlobal,
279286
use: {
280287
loader: 'error-loader',
281288
options: {
@@ -298,7 +305,7 @@ export const css = curry(async function css(
298305
oneOf: [
299306
{
300307
// This should only be applied to CSS files
301-
issuer: { test: /\.css$/ },
308+
issuer: { test: regexCssAll },
302309
// Exclude extensions that webpack handles by default
303310
exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
304311
use: {
@@ -323,12 +330,15 @@ export const css = curry(async function css(
323330
new MiniCssExtractPlugin({
324331
filename: 'static/css/[contenthash].css',
325332
chunkFilename: 'static/css/[contenthash].css',
326-
// Next.js guarantees that CSS order doesn't matter, due to imposed
333+
// Next.js guarantees that CSS order "doesn't matter", due to imposed
327334
// restrictions:
328335
// 1. Global CSS can only be defined in a single entrypoint (_app)
329336
// 2. CSS Modules generate scoped class names by default and cannot
330337
// include Global CSS (:global() selector).
331338
//
339+
// While not a perfect guarantee (e.g. liberal use of `:global()`
340+
// selector), this assumption is required to code-split CSS.
341+
//
332342
// If this warning were to trigger, it'd be unactionable by the user,
333343
// but also not valid -- so we disable it.
334344
ignoreOrder: true,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy