@@ -4,6 +4,7 @@ import { tmpdir } from 'node:os';
4
4
import path from 'node:path' ;
5
5
import { pathToFileURL } from 'node:url' ;
6
6
7
+ import { createFilter } from '@rollup/pluginutils' ;
7
8
import type { PostCSSLoaderOptions , Rspack } from '@rsbuild/core' ;
8
9
9
10
/**
@@ -56,6 +57,61 @@ interface TailwindRspackPluginOptions {
56
57
*/
57
58
config : string ;
58
59
60
+ /**
61
+ * The modules to be excluded.
62
+ *
63
+ * If {@link include} is omitted or empty,
64
+ * all modules that do not match any of the {@link exclude} patterns will be included.
65
+ * Otherwise, only modules that match one or more of the {@link include} patterns
66
+ * and do not match any of the {@link exclude} patterns will be included.
67
+ *
68
+ * @example
69
+ *
70
+ * ```js
71
+ * // rspack.config.js
72
+ * import { TailwindRspackPlugin } from 'rsbuild-plugin-tailwindcss'
73
+ *
74
+ * export default {
75
+ * plugins: [
76
+ * new TailwindRspackPlugin({
77
+ * exclude: [
78
+ * './src/store/**',
79
+ * /[\\/]node_modules[\\/]/,
80
+ * ],
81
+ * }),
82
+ * ],
83
+ * }
84
+ * ```
85
+ */
86
+ exclude ?: FilterPattern | undefined ;
87
+
88
+ /**
89
+ * The modules to be included using `picomatch` patterns.
90
+ *
91
+ * If {@link include} is omitted or empty,
92
+ * all modules that do not match any of the {@link exclude} patterns will be included.
93
+ * Otherwise, only modules that match one or more of the {@link include} patterns
94
+ * and do not match any of the {@link exclude} patterns will be included.
95
+ *
96
+ * @example
97
+ *
98
+ * ```js
99
+ * // rspack.config.js
100
+ * import { TailwindRspackPlugin } from 'rsbuild-plugin-tailwindcss'
101
+ *
102
+ * export default {
103
+ * plugins: [
104
+ * new TailwindRspackPlugin({
105
+ * include: [
106
+ * /\.[jt]sx?/,
107
+ * ],
108
+ * }),
109
+ * ],
110
+ * }
111
+ * ```
112
+ */
113
+ include ?: FilterPattern | undefined ;
114
+
59
115
/**
60
116
* The postcss options to be applied.
61
117
*
@@ -86,6 +142,16 @@ interface TailwindRspackPluginOptions {
86
142
> ;
87
143
}
88
144
145
+ // From `@rollup/pluginutils`
146
+ /**
147
+ * A valid `picomatch` glob pattern, or array of patterns.
148
+ */
149
+ export type FilterPattern =
150
+ | ReadonlyArray < string | RegExp >
151
+ | string
152
+ | RegExp
153
+ | null ;
154
+
89
155
/**
90
156
* The Rspack plugin for Tailwind integration.
91
157
*
@@ -94,17 +160,6 @@ interface TailwindRspackPluginOptions {
94
160
class TailwindRspackPlugin {
95
161
constructor ( private readonly options : TailwindRspackPluginOptions ) { }
96
162
97
- /**
98
- * `defaultOptions` is the default options that the {@link TailwindRspackPlugin} uses.
99
- *
100
- * @public
101
- */
102
- static defaultOptions : Readonly < Required < TailwindRspackPluginOptions > > =
103
- Object . freeze < Required < TailwindRspackPluginOptions > > ( {
104
- config : 'tailwind.config.js' ,
105
- postcssOptions : { } ,
106
- } ) ;
107
-
108
163
/**
109
164
* The entry point of a Rspack plugin.
110
165
* @param compiler - the Rspack compiler
@@ -124,6 +179,11 @@ class TailwindRspackPluginImpl {
124
179
private compiler : Rspack . Compiler ,
125
180
private options : TailwindRspackPluginOptions ,
126
181
) {
182
+ const filter = createFilter ( options . include , options . exclude , {
183
+ // biome-ignore lint/style/noNonNullAssertion: context should exist
184
+ resolve : compiler . options . context ! ,
185
+ } ) ;
186
+
127
187
const { RawSource } = compiler . webpack . sources ;
128
188
compiler . hooks . thisCompilation . tap ( this . name , ( compilation ) => {
129
189
compilation . hooks . processAssets . tapPromise ( this . name , async ( ) => {
@@ -159,7 +219,10 @@ class TailwindRspackPluginImpl {
159
219
] = await Promise . all ( [
160
220
import ( 'postcss' ) ,
161
221
import ( 'tailwindcss' ) ,
162
- this . #prepareTailwindConfig( entryName , entryModules ) ,
222
+ this . #prepareTailwindConfig(
223
+ entryName ,
224
+ Array . from ( entryModules ) . filter ( filter ) ,
225
+ ) ,
163
226
] ) ;
164
227
165
228
const postcssTransform = postcss ( [
@@ -203,7 +266,7 @@ class TailwindRspackPluginImpl {
203
266
204
267
async #prepareTailwindConfig(
205
268
entryName : string ,
206
- entryModules : Set < string > ,
269
+ entryModules : Array < string > ,
207
270
) : Promise < string > {
208
271
const userConfig = path . isAbsolute ( this . options . config )
209
272
? this . options . config
@@ -225,7 +288,7 @@ class TailwindRspackPluginImpl {
225
288
226
289
const configPath = path . resolve ( outputDir , 'tailwind.config.mjs' ) ;
227
290
228
- const content = JSON . stringify ( Array . from ( entryModules ) ) ;
291
+ const content = JSON . stringify ( entryModules ) ;
229
292
230
293
await writeFile (
231
294
configPath ,
0 commit comments