Skip to content

Commit e6f2d45

Browse files
committed
feat: clean code, compatible with more preprocessor rules
1 parent 0965cd5 commit e6f2d45

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsbuild-plugin-unplugin-vue",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-template",
55
"license": "MIT",
66
"type": "module",
@@ -14,9 +14,7 @@
1414
"main": "./dist/index.js",
1515
"module": "./dist/index.mjs",
1616
"types": "./dist/index.d.ts",
17-
"files": [
18-
"dist"
19-
],
17+
"files": ["dist"],
2018
"scripts": {
2119
"build": "rslib build",
2220
"dev": "rslib build --watch",

src/index.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ const isVirtualModule = (request?: string) => {
1313
return request.includes('/node_modules/.virtual');
1414
};
1515

16+
// Preprocessor defined rule in `lib` field will be suffixed with self-increasing serial number.
17+
// e.g. less, less-1, sass-2.
18+
const isPreprocessorRule = (
19+
preprocessRuleId: string,
20+
toMatchRuleId: string,
21+
) => {
22+
if (preprocessRuleId === toMatchRuleId) {
23+
return true;
24+
}
25+
26+
if (new RegExp(`${preprocessRuleId}-\\d`).test(toMatchRuleId)) {
27+
return true;
28+
}
29+
30+
return false;
31+
};
32+
1633
const isScopedStyle = (request?: string) => {
1734
if (!request) return false;
1835

@@ -28,16 +45,38 @@ export const pluginUnpluginVue = ({
2845
const callerName = api.context.callerName;
2946
const isRslib = callerName === 'rslib';
3047

48+
api.modifyEnvironmentConfig({
49+
handler: (config) => {
50+
config.output.target = 'web';
51+
},
52+
order: 'default',
53+
});
54+
3155
api.modifyRspackConfig((config) => {
3256
// Not using webpack-chain here.
3357
// https://github.com/neutrinojs/webpack-chain/issues/352
3458
config.plugins?.push(RspackPluginVue(unpluginVueOptions));
3559
});
3660

37-
api.modifyBundlerChain((config, { CHAIN_ID }) => {
38-
const baseRule = config.module.rules.get(CHAIN_ID.RULE.CSS);
39-
baseRule.enforce('post');
40-
config.resolve.extensions.add('.vue');
61+
api.modifyBundlerChain({
62+
handler: (config, { CHAIN_ID }) => {
63+
for (const ruleId in config.module.rules.entries()) {
64+
if (
65+
CHAIN_ID.RULE.CSS === ruleId ||
66+
isPreprocessorRule(CHAIN_ID.RULE.LESS, ruleId) ||
67+
isPreprocessorRule(CHAIN_ID.RULE.SASS, ruleId) ||
68+
isPreprocessorRule(CHAIN_ID.RULE.STYLUS, ruleId)
69+
) {
70+
const baseRule = config.module.rules.get(ruleId);
71+
if (baseRule) {
72+
baseRule.enforce('post');
73+
}
74+
}
75+
}
76+
77+
config.resolve.extensions.add('.vue');
78+
},
79+
order: 'post',
4180
});
4281

4382
// Rslib bundleless specific config, will only be used in bundleless mode
@@ -68,34 +107,6 @@ export const pluginUnpluginVue = ({
68107
}
69108
}
70109
});
71-
72-
// api.modifyBundlerChain((config, { CHAIN_ID }) => {
73-
// const cssRule = config.module.rules.get(CHAIN_ID.RULE.CSS);
74-
// const vueCssRule = config.module
75-
// .rule('vue-scoped-css')
76-
// .test((value) => {
77-
// const matched = /lang\.css$/.test(value);
78-
// if (matched) {
79-
// }
80-
// return isStyle(value);
81-
// })
82-
// .before(CHAIN_ID.RULE.CSS)
83-
// .merge(cssRule.entries());
84-
// const ruleId = CHAIN_ID.RULE.CSS;
85-
// const rule = config.module.rule(ruleId);
86-
// const rspackPath = require.resolve('@rspack/core');
87-
// // TODO: hard coded loader path
88-
// const loaderPath = path.resolve(rspackPath, '../cssExtractLoader.js');
89-
// if (rule.uses.has(CHAIN_ID.USE.MINI_CSS_EXTRACT)) {
90-
// rule
91-
// .use(CHAIN_ID.USE.MINI_CSS_EXTRACT)
92-
// .loader(loaderPath)
93-
// .options({});
94-
// }
95-
// for (const use of cssRule.uses.values()) {
96-
// vueCssRule.use(use.name).merge(use.entries());
97-
// }
98-
// });
99110
}
100111
},
101112
});

0 commit comments

Comments
 (0)