Skip to content

Commit 37aa74b

Browse files
author
Suh Donghwi
committed
feat: include, exclude options
1 parent e8085d0 commit 37aa74b

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

packages/nextjs-plugin/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ const withStyleX =
217217
},
218218
// Enforce nextjsMode to true
219219
nextjsMode: true,
220+
// Pass through include/exclude options
221+
include: pluginOptions?.include,
222+
exclude: pluginOptions?.exclude,
220223
...(extractCSS
221224
? {
222225
async transformCss(css, filePath) {

packages/webpack-plugin/src/index.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export default class StyleXPlugin {
4444

4545
transformCss: CSSTransformer;
4646

47+
include: (string | RegExp)[];
48+
exclude: (string | RegExp)[];
49+
4750
constructor({
4851
stylexImports = ['stylex', '@stylexjs/stylex'],
4952
useCSSLayers = false,
@@ -52,6 +55,8 @@ export default class StyleXPlugin {
5255
transformCss = identityTransfrom,
5356
transformer = 'rs-compiler',
5457
extractCSS = true,
58+
include = [],
59+
exclude = [],
5560
}: StyleXPluginOption = {}) {
5661
this.useCSSLayers = useCSSLayers;
5762
this.loaderOption = {
@@ -69,6 +74,35 @@ export default class StyleXPlugin {
6974
extractCSS,
7075
};
7176
this.transformCss = transformCss;
77+
this.include = include;
78+
this.exclude = exclude;
79+
}
80+
81+
private shouldProcessModule(resourcePath: string): boolean {
82+
// First check exclude patterns
83+
for (const pattern of this.exclude) {
84+
if (this.matchesPattern(resourcePath, pattern)) {
85+
// If excluded, check if it's explicitly included
86+
for (const includePattern of this.include) {
87+
if (this.matchesPattern(resourcePath, includePattern)) {
88+
return true; // Explicitly included, override exclude
89+
}
90+
}
91+
92+
return false; // Excluded and not explicitly included
93+
}
94+
}
95+
96+
// Not excluded, so include it
97+
return true;
98+
}
99+
100+
private matchesPattern(resourcePath: string, pattern: string | RegExp): boolean {
101+
if (pattern instanceof RegExp) {
102+
return pattern.test(resourcePath);
103+
}
104+
105+
return resourcePath.includes(pattern);
72106
}
73107

74108
apply(compiler: webpack.Compiler) {
@@ -114,8 +148,9 @@ export default class StyleXPlugin {
114148
PLUGIN_NAME,
115149
(loaderContext, mod) => {
116150
const extname = path.extname(mod.matchResource || mod.resource);
151+
const resourcePath = mod.matchResource || mod.resource;
117152

118-
if (INCLUDE_REGEXP.test(extname)) {
153+
if (INCLUDE_REGEXP.test(extname) && this.shouldProcessModule(resourcePath)) {
119154
(loaderContext as SupplementedLoaderContext).StyleXWebpackContextKey = {
120155
registerStyleXRules: (resourcePath, stylexRules) => {
121156
this.stylexRules.set(resourcePath, stylexRules);
@@ -134,7 +169,7 @@ export default class StyleXPlugin {
134169
});
135170
}
136171

137-
if (VIRTUAL_CSS_PATTERN.test(mod.matchResource || mod.resource)) {
172+
if (VIRTUAL_CSS_PATTERN.test(resourcePath)) {
138173
mod.loaders.push({
139174
loader: stylexVirtualLoaderPath,
140175
options: {},

packages/webpack-plugin/src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ export interface StyleXPluginOption extends Pick<StyleXWebpackLoaderOptions, 'tr
5252
* @default true
5353
*/
5454
extractCSS?: boolean;
55+
56+
/**
57+
* Patterns to include for StyleX transformation.
58+
* By default, all files are processed. This option overrides `exclude` option.
59+
*
60+
* @example ['@myorg/design-system']
61+
*/
62+
include?: (string | RegExp)[];
63+
64+
/**
65+
* Patterns to exclude from StyleX transformation.
66+
* By default, no files are excluded. This option is overridden by `include` option.
67+
*
68+
* @example [/node_modules/]
69+
*/
70+
exclude?: (string | RegExp)[];
5571
}
5672
export type StyleXWebpackLoaderOptions = {
5773
stylexImports: StyleXOptions['importSources'];

0 commit comments

Comments
 (0)