Skip to content

Commit 582d3a3

Browse files
author
Chenchao Gao
committed
feat: support multipleConfig
1 parent 446af93 commit 582d3a3

File tree

9 files changed

+198
-4
lines changed

9 files changed

+198
-4
lines changed

src/TailwindCSSRspackPlugin.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ interface TailwindRspackPluginOptions {
116116
*/
117117
include?: FilterPattern | undefined;
118118

119+
/**
120+
* Setting Tailwind config path for different entries
121+
*
122+
* {@link multipleConfig} can be configured as an object where the key is the entryName and the value is the config path.
123+
* If the key cannot be found, it will fallback to {@link config} , which is the default config path.
124+
*
125+
* @example
126+
*
127+
* ```js
128+
* // rspack.config.js
129+
* import { TailwindRspackPlugin } from 'rsbuild-plugin-tailwindcss'
130+
*
131+
* export default {
132+
* plugins: [
133+
* new TailwindRspackPlugin({
134+
* multipleConfig: {
135+
* index: './config/tailwind.index.config.js',
136+
* main: './config/tailwind.main.config.js'
137+
* },
138+
* }),
139+
* ],
140+
* }
141+
* ```
142+
*/
143+
multipleConfig?: Record<string, string> | undefined;
144+
119145
/**
120146
* The postcss options to be applied.
121147
*
@@ -315,10 +341,14 @@ class TailwindRspackPluginImpl {
315341
entryName: string,
316342
entryModules: Array<string>,
317343
): Promise<string> {
318-
const userConfig = path.isAbsolute(this.options.config)
319-
? this.options.config
344+
const multipleConfig = this.options.multipleConfig || {};
345+
346+
const userConfig = multipleConfig[entryName] || this.options.config;
347+
348+
const resolvedConfig = path.isAbsolute(userConfig)
349+
? userConfig
320350
: // biome-ignore lint/style/noNonNullAssertion: should have context
321-
path.resolve(this.compiler.options.context!, this.options.config);
351+
path.resolve(this.compiler.options.context!, userConfig);
322352

323353
const outputDir = DEBUG
324354
? path.resolve(
@@ -334,7 +364,7 @@ class TailwindRspackPluginImpl {
334364
}
335365

336366
const [configName, configContent] = await this.#generateTailwindConfig(
337-
userConfig,
367+
resolvedConfig,
338368
entryModules,
339369
);
340370
const configPath = path.resolve(outputDir, configName);

src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,32 @@ export interface PluginTailwindCSSOptions {
112112
* ```
113113
*/
114114
include?: FilterPattern | undefined;
115+
116+
/**
117+
* Setting Tailwind config path for different entries
118+
*
119+
* {@link multipleConfig} can be configured as an object where the key is the entryName and the value is the config path.
120+
* If the key cannot be found, it will fallback to {@link config} , which is the default config path.
121+
*
122+
* @example
123+
*
124+
* ```js
125+
* // rspack.config.js
126+
* import { TailwindRspackPlugin } from 'rsbuild-plugin-tailwindcss'
127+
*
128+
* export default {
129+
* plugins: [
130+
* new TailwindRspackPlugin({
131+
* multipleConfig: {
132+
* index: './config/tailwind.index.config.js',
133+
* main: './config/tailwind.main.config.js'
134+
* },
135+
* }),
136+
* ],
137+
* }
138+
* ```
139+
*/
140+
multipleConfig?: Record<string, string> | undefined;
115141
}
116142

117143
export const pluginTailwindCSS = (
@@ -160,6 +186,7 @@ export const pluginTailwindCSS = (
160186
config: options.config ?? 'tailwind.config.js',
161187
include: options.include,
162188
exclude: options.exclude,
189+
multipleConfig: options.multipleConfig,
163190
postcssOptions,
164191
},
165192
]);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('tailwindcss').Config} */
2+
export default {
3+
content: ['./src/**/*.{js,jsx,ts,tsx}'],
4+
theme: {
5+
extend: {
6+
colors: {
7+
index: '#0055ff',
8+
},
9+
},
10+
},
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('tailwindcss').Config} */
2+
export default {
3+
content: ['./src/**/*.{js,jsx,ts,tsx}'],
4+
theme: {
5+
extend: {
6+
colors: {
7+
main: '#ff5500',
8+
},
9+
},
10+
},
11+
};

test/multiple-config/index.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
import test, { expect } from '@playwright/test';
5+
import { createRsbuild } from '@rsbuild/core';
6+
7+
import { pluginTailwindCSS } from '../../src';
8+
import { getRandomPort } from '../helper';
9+
10+
const __dirname = dirname(fileURLToPath(import.meta.url));
11+
12+
test('should build with multiple configs for different entries', async ({
13+
page,
14+
}) => {
15+
const rsbuild = await createRsbuild({
16+
cwd: __dirname,
17+
rsbuildConfig: {
18+
source: {
19+
entry: {
20+
index: './src/index.js',
21+
main: './src/main.js',
22+
default: './src/default.js',
23+
},
24+
},
25+
plugins: [
26+
pluginTailwindCSS({
27+
multipleConfig: {
28+
index: './config/tailwind.index.config.js',
29+
main: './config/tailwind.main.config.js',
30+
},
31+
}),
32+
],
33+
server: {
34+
port: getRandomPort(),
35+
},
36+
},
37+
});
38+
39+
await rsbuild.build();
40+
41+
const { server, urls } = await rsbuild.startDevServer();
42+
43+
await page.goto(`${urls[0]}/index`);
44+
let style = await getStyle();
45+
expect(style.backgroundColor).toBe('rgb(0, 85, 255)');
46+
47+
await page.goto(`${urls[0]}/main`);
48+
style = await getStyle();
49+
expect(style.backgroundColor).toBe('rgb(255, 85, 0)');
50+
51+
await page.goto(`${urls[0]}/default`);
52+
style = await getStyle();
53+
expect(style.backgroundColor).toBe('rgb(85, 255, 0)');
54+
55+
await server.close();
56+
57+
async function getStyle() {
58+
return await page.evaluate(() => {
59+
const el = document.getElementById('test');
60+
61+
if (!el) {
62+
throw new Error('#test not found');
63+
}
64+
65+
return window.getComputedStyle(el);
66+
});
67+
}
68+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'tailwindcss/utilities.css';
2+
3+
function className() {
4+
return 'flex bg-default';
5+
}
6+
7+
const root = document.getElementById('root');
8+
const element = document.createElement('div');
9+
element.id = 'test';
10+
element.className = className();
11+
element.textContent = 'Default Entry';
12+
root.appendChild(element);

test/multiple-config/src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'tailwindcss/utilities.css';
2+
3+
function className() {
4+
return 'flex bg-index';
5+
}
6+
7+
const root = document.getElementById('root');
8+
const element = document.createElement('div');
9+
element.id = 'test';
10+
element.className = className();
11+
element.textContent = 'Index Entry';
12+
root.appendChild(element);

test/multiple-config/src/main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'tailwindcss/utilities.css';
2+
3+
function className() {
4+
return 'flex bg-main';
5+
}
6+
7+
const root = document.getElementById('root');
8+
const element = document.createElement('div');
9+
element.id = 'test';
10+
element.className = className();
11+
element.textContent = 'Main Entry';
12+
root.appendChild(element);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** @type {import('tailwindcss').Config} */
2+
export default {
3+
content: ['./src/**/*.{js,jsx,ts,tsx}'],
4+
theme: {
5+
extend: {
6+
colors: {
7+
default: '#55ff00',
8+
},
9+
},
10+
},
11+
};

0 commit comments

Comments
 (0)