Skip to content

Commit d689afd

Browse files
feat: add pure parser option for css/module and css/auto (#20946)
1 parent 0053c9f commit d689afd

27 files changed

Lines changed: 805 additions & 97 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add a `pure` parser option for `css/module` and `css/auto` types matching `postcss-modules-local-by-default`'s pure mode: every selector must contain at least one local class or id, otherwise webpack emits a build error. Two comments opt out — `/* cssmodules-pure-ignore */` directly before a rule suppresses that rule's check (per-rule, not propagated to children, matching PCSL), and `/* cssmodules-pure-no-check */` placed among the leading comments of the file (before any rule) disables the check for the whole file. Nested rules inside a local-bearing ancestor are treated as pure-compliant; `&` resolves to the parent rule's purity; `@keyframes` and `@counter-style` body contents are exempt; rules whose body contains only nested rules don't trigger the check (the children carry it instead).

cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@
119119
"hashs",
120120
"hotpink",
121121
"hotupdatechunk",
122+
"cssmodules",
122123
"icss",
124+
"PCSL",
123125
"ident",
124126
"idents",
125127
"IIFE",

declarations/WebpackOptions.d.ts

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -776,30 +776,6 @@ export type AssetModuleOutputPath =
776776
*/
777777
export type AssetParserDataUrlFunction =
778778
import("../lib/asset/AssetParser").AssetParserDataUrlFunction;
779-
/**
780-
* Configure the generated JS modules that use the ES modules syntax.
781-
*/
782-
export type CssGeneratorEsModule = boolean;
783-
/**
784-
* Specifies the convention of exported names.
785-
*/
786-
export type CssGeneratorExportsConvention =
787-
| ("as-is" | "camel-case" | "camel-case-only" | "dashes" | "dashes-only")
788-
| import("../lib/dependencies/CssIcssExportDependency").ExportsConventionFn;
789-
/**
790-
* Avoid generating and loading a stylesheet and only embed exports from css into output javascript files.
791-
*/
792-
export type CssGeneratorExportsOnly = boolean;
793-
/**
794-
* Configure the generated local ident name.
795-
*/
796-
export type CssGeneratorLocalIdentName =
797-
| string
798-
| import("../lib/TemplatedPathPlugin").TemplatePathFn;
799-
/**
800-
* Configure how CSS content is exported as default.
801-
*/
802-
export type CssParserExportType = "link" | "text" | "css-style-sheet" | "style";
803779
/**
804780
* Enable/disable renaming of `@keyframes`.
805781
*/
@@ -816,6 +792,10 @@ export type CssParserCustomIdents = boolean;
816792
* Enable/disable renaming of dashed identifiers, e. g. custom properties.
817793
*/
818794
export type CssParserDashedIdents = boolean;
795+
/**
796+
* Configure how CSS content is exported as default.
797+
*/
798+
export type CssParserExportType = "link" | "text" | "css-style-sheet" | "style";
819799
/**
820800
* Enable/disable renaming of `@function` names.
821801
*/
@@ -832,10 +812,34 @@ export type CssParserImport = boolean;
832812
* Use ES modules named export for css exports.
833813
*/
834814
export type CssParserNamedExports = boolean;
815+
/**
816+
* Enable strict pure mode: every selector must contain at least one local class or id selector.
817+
*/
818+
export type CssParserPure = boolean;
835819
/**
836820
* Enable/disable `url()`/`image-set()`/`src()`/`image()` functions handling.
837821
*/
838822
export type CssParserUrl = boolean;
823+
/**
824+
* Configure the generated JS modules that use the ES modules syntax.
825+
*/
826+
export type CssGeneratorEsModule = boolean;
827+
/**
828+
* Specifies the convention of exported names.
829+
*/
830+
export type CssGeneratorExportsConvention =
831+
| ("as-is" | "camel-case" | "camel-case-only" | "dashes" | "dashes-only")
832+
| import("../lib/dependencies/CssIcssExportDependency").ExportsConventionFn;
833+
/**
834+
* Avoid generating and loading a stylesheet and only embed exports from css into output javascript files.
835+
*/
836+
export type CssGeneratorExportsOnly = boolean;
837+
/**
838+
* Configure the generated local ident name.
839+
*/
840+
export type CssGeneratorLocalIdentName =
841+
| string
842+
| import("../lib/TemplatedPathPlugin").TemplatePathFn;
839843
/**
840844
* Options for defer import.
841845
*/
@@ -3162,6 +3166,55 @@ export interface AssetResourceGeneratorOptions {
31623166
*/
31633167
publicPath?: RawPublicPath;
31643168
}
3169+
/**
3170+
* Parser options for css/auto and css/module modules.
3171+
*/
3172+
export interface CssAutoOrModuleParserOptions {
3173+
/**
3174+
* Enable/disable renaming of `@keyframes`.
3175+
*/
3176+
animation?: CssParserAnimation;
3177+
/**
3178+
* Enable/disable renaming of `@container` names.
3179+
*/
3180+
container?: CssParserContainer;
3181+
/**
3182+
* Enable/disable renaming of custom identifiers.
3183+
*/
3184+
customIdents?: CssParserCustomIdents;
3185+
/**
3186+
* Enable/disable renaming of dashed identifiers, e. g. custom properties.
3187+
*/
3188+
dashedIdents?: CssParserDashedIdents;
3189+
/**
3190+
* Configure how CSS content is exported as default.
3191+
*/
3192+
exportType?: CssParserExportType;
3193+
/**
3194+
* Enable/disable renaming of `@function` names.
3195+
*/
3196+
function?: CssParserFunction;
3197+
/**
3198+
* Enable/disable renaming of grid identifiers.
3199+
*/
3200+
grid?: CssParserGrid;
3201+
/**
3202+
* Enable/disable `@import` at-rules handling.
3203+
*/
3204+
import?: CssParserImport;
3205+
/**
3206+
* Use ES modules named export for css exports.
3207+
*/
3208+
namedExports?: CssParserNamedExports;
3209+
/**
3210+
* Enable strict pure mode: every selector must contain at least one local class or id selector.
3211+
*/
3212+
pure?: CssParserPure;
3213+
/**
3214+
* Enable/disable `url()`/`image-set()`/`src()`/`image()` functions handling.
3215+
*/
3216+
url?: CssParserUrl;
3217+
}
31653218
/**
31663219
* Generator options for css modules.
31673220
*/
@@ -3217,7 +3270,7 @@ export interface CssModuleGeneratorOptions {
32173270
localIdentName?: CssGeneratorLocalIdentName;
32183271
}
32193272
/**
3220-
* Parser options for css/module modules.
3273+
* Parser options for css/global modules.
32213274
*/
32223275
export interface CssModuleParserOptions {
32233276
/**
@@ -4229,17 +4282,17 @@ export interface ParserOptionsByModuleTypeKnown {
42294282
*/
42304283
css?: CssParserOptions;
42314284
/**
4232-
* Parser options for css/module modules.
4285+
* Parser options for css/auto and css/module modules.
42334286
*/
4234-
"css/auto"?: CssModuleParserOptions;
4287+
"css/auto"?: CssAutoOrModuleParserOptions;
42354288
/**
4236-
* Parser options for css/module modules.
4289+
* Parser options for css/global modules.
42374290
*/
42384291
"css/global"?: CssModuleParserOptions;
42394292
/**
4240-
* Parser options for css/module modules.
4293+
* Parser options for css/auto and css/module modules.
42414294
*/
4242-
"css/module"?: CssModuleParserOptions;
4295+
"css/module"?: CssAutoOrModuleParserOptions;
42434296
/**
42444297
* Parser options for javascript modules.
42454298
*/

lib/css/CssModulesPlugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ class CssModulesPlugin {
263263
case CSS_MODULE_TYPE_MODULE: {
264264
defaultMode = "local";
265265
compiler.validate(
266-
() => getSchema("CssModuleParserOptions"),
266+
() => getSchema("CssAutoOrModuleParserOptions"),
267267
parserOptions,
268268
parserValidationOptions,
269269
(options) =>
270-
require("../../schemas/plugins/css/CssModuleParserOptions.check")(
270+
require("../../schemas/plugins/css/CssAutoOrModuleParserOptions.check")(
271271
options
272272
)
273273
);
@@ -276,11 +276,11 @@ class CssModulesPlugin {
276276
case CSS_MODULE_TYPE_AUTO: {
277277
defaultMode = "auto";
278278
compiler.validate(
279-
() => getSchema("CssModuleParserOptions"),
279+
() => getSchema("CssAutoOrModuleParserOptions"),
280280
parserOptions,
281281
parserValidationOptions,
282282
(options) =>
283-
require("../../schemas/plugins/css/CssModuleParserOptions.check")(
283+
require("../../schemas/plugins/css/CssAutoOrModuleParserOptions.check")(
284284
options
285285
)
286286
);

0 commit comments

Comments
 (0)