Skip to content

Commit 126997e

Browse files
committed
feat: basic implementation
1 parent 611a0ca commit 126997e

File tree

6 files changed

+359
-14
lines changed

6 files changed

+359
-14
lines changed

biome.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
"linter": {
2626
"enabled": true,
2727
"rules": {
28-
"recommended": true
28+
"recommended": true,
29+
"suspicious": {
30+
"noExplicitAny": "off"
31+
}
2932
}
3033
}
3134
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"main": "./dist/index.js",
1515
"module": "./dist/index.mjs",
1616
"types": "./dist/index.d.ts",
17-
"files": ["dist"],
17+
"files": [
18+
"dist"
19+
],
1820
"scripts": {
1921
"build": "tsup",
2022
"dev": "tsup --watch",
@@ -36,6 +38,7 @@
3638
"@playwright/test": "^1.44.1",
3739
"@rsbuild/core": "^0.7.10",
3840
"@types/node": "^20.14.1",
41+
"line-diff": "2.1.1",
3942
"nano-staged": "^0.8.0",
4043
"playwright": "^1.44.1",
4144
"simple-git-hooks": "^2.11.1",

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,56 @@
1-
import type { RsbuildPlugin } from '@rsbuild/core';
1+
import path from 'node:path';
2+
import type { CSSLoaderOptions, RsbuildPlugin } from '@rsbuild/core';
23

3-
export type pluginTypedCSSModulesOptions = {
4-
foo?: string;
5-
bar?: boolean;
6-
};
4+
export const PLUGIN_TYPED_CSS_MODULES_NAME = 'rsbuild:typed-css-modules';
75

8-
export const pluginTypedCSSModules = (
9-
options: pluginTypedCSSModulesOptions = {},
10-
): RsbuildPlugin => ({
11-
name: 'plugin-example',
6+
export const pluginTypedCSSModules = (): RsbuildPlugin => ({
7+
name: PLUGIN_TYPED_CSS_MODULES_NAME,
128

13-
setup() {
14-
console.log('Hello Rsbuild!', options);
9+
setup(api) {
10+
api.modifyBundlerChain({
11+
order: 'post',
12+
handler: async (chain, { target, CHAIN_ID }) => {
13+
if (target === 'web') {
14+
const ruleIds = [
15+
CHAIN_ID.RULE.CSS,
16+
CHAIN_ID.RULE.SASS,
17+
CHAIN_ID.RULE.LESS,
18+
CHAIN_ID.RULE.STYLUS,
19+
];
20+
21+
for (const ruleId of ruleIds) {
22+
if (!chain.module.rules.has(ruleId)) {
23+
continue;
24+
}
25+
26+
const rule = chain.module.rule(ruleId);
27+
28+
if (!rule.uses.has(CHAIN_ID.USE.CSS)) {
29+
continue;
30+
}
31+
32+
const cssLoaderOptions: CSSLoaderOptions = rule
33+
.use(CHAIN_ID.USE.CSS)
34+
.get('options');
35+
36+
if (
37+
!cssLoaderOptions.modules ||
38+
(typeof cssLoaderOptions.modules === 'object' &&
39+
cssLoaderOptions.modules.auto === false)
40+
) {
41+
continue;
42+
}
43+
44+
rule
45+
.use(CHAIN_ID.USE.CSS_MODULES_TS)
46+
.loader(path.resolve(__dirname, './loader.cjs'))
47+
.options({
48+
modules: cssLoaderOptions.modules,
49+
})
50+
.before(CHAIN_ID.USE.CSS);
51+
}
52+
}
53+
},
54+
});
1555
},
1656
});

0 commit comments

Comments
 (0)