Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make CSS preprocessor plugins compat with Rsbuild 1.2 #4909

Merged
merged 2 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/plugin-less/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@ export const pluginLess = (
.resolve.preferRelative(true)
.end();

const inlineRule = chain.module
.rule(findRuleId(chain, CHAIN_ID.RULE.LESS_INLINE))
.test(include)
.resourceQuery(/inline/);

// Support for importing raw Less files
chain.module
.rule(CHAIN_ID.RULE.LESS_RAW)
Expand All @@ -173,7 +168,15 @@ export const pluginLess = (
callback: (rule: RspackChain.Rule, type: 'normal' | 'inline') => void,
) => {
callback(rule, 'normal');
callback(inlineRule, 'inline');

// Rsbuild < 1.3.0 does not have RULE.CSS_INLINE.
if (chain.module.rules.has(CHAIN_ID.RULE.CSS_INLINE)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be preferable to use CHAIN_ID.RULE.CSS_INLINE && chain.module.rules.has(CHAIN_ID.RULE.CSS_INLINE) because, theoretically, there could be a rule named undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be better👍

const inlineRule = chain.module
.rule(findRuleId(chain, CHAIN_ID.RULE.LESS_INLINE))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findRuleId should not be put inside the updateRules method. It should be only called once.

.test(include)
.resourceQuery(/inline/);
callback(inlineRule, 'inline');
}
};

const lessLoaderPath = path.join(
Expand Down
50 changes: 25 additions & 25 deletions packages/plugin-less/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ exports[`plugin-less > should add less-loader 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
{
"resourceQuery": /inline/,
"test": /\\\\\\.less\\$/,
Expand Down Expand Up @@ -97,11 +102,6 @@ exports[`plugin-less > should add less-loader 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
]
`;

Expand Down Expand Up @@ -163,6 +163,11 @@ exports[`plugin-less > should add less-loader and css-loader when injectStyles 1
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
{
"resourceQuery": /inline/,
"test": /\\\\\\.less\\$/,
Expand Down Expand Up @@ -202,11 +207,6 @@ exports[`plugin-less > should add less-loader and css-loader when injectStyles 1
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
]
`;

Expand Down Expand Up @@ -271,6 +271,11 @@ exports[`plugin-less > should add less-loader with excludes 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
{
"exclude": [
/node_modules/,
Expand Down Expand Up @@ -313,11 +318,6 @@ exports[`plugin-less > should add less-loader with excludes 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
]
`;

Expand Down Expand Up @@ -379,6 +379,11 @@ exports[`plugin-less > should add less-loader with tools.less 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
{
"resourceQuery": /inline/,
"test": /\\\\\\.less\\$/,
Expand Down Expand Up @@ -418,11 +423,6 @@ exports[`plugin-less > should add less-loader with tools.less 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
]
`;

Expand Down Expand Up @@ -491,6 +491,11 @@ exports[`plugin-less > should allow to use Less plugins 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
{
"resourceQuery": /inline/,
"test": /\\\\\\.less\\$/,
Expand Down Expand Up @@ -537,10 +542,5 @@ exports[`plugin-less > should allow to use Less plugins 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.less\\$/,
"type": "asset/source",
},
]
`;
28 changes: 27 additions & 1 deletion packages/plugin-less/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRsbuild } from '@rsbuild/core';
import { type RsbuildPluginAPI, createRsbuild } from '@rsbuild/core';
import { matchRules } from '@scripts/test-helper';
import { pluginLess } from '../src';

Expand Down Expand Up @@ -111,4 +111,30 @@ describe('plugin-less', () => {
expect(matchRules(bundlerConfigs[0], 'a.less').length).toBe(2);
expect(matchRules(bundlerConfigs[0], 'b.less').length).toBe(5);
});

it('should be compatible with Rsbuild < 1.3.0', async () => {
const rsbuild = await createRsbuild({
rsbuildConfig: {
plugins: [
{
name: 'rsbuild-plugin-test',
post: ['rsbuild:css'],
setup(api: RsbuildPluginAPI) {
// Mock the behavior of Rsbuild < 1.3.0
api.modifyBundlerChain((chain, { CHAIN_ID }) => {
chain.module.rules.delete(CHAIN_ID.RULE.CSS_INLINE);
});
},
},
pluginLess(),
],
},
});

await rsbuild.initConfigs();

const bundlerConfigs = await rsbuild.initConfigs();
expect(matchRules(bundlerConfigs[0], 'a.less').length).toBe(2);
expect(matchRules(bundlerConfigs[0], 'a.less?inline').length).toBe(0);
});
});
27 changes: 15 additions & 12 deletions packages/plugin-sass/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,6 @@ export const pluginSass = (
.resolve.preferRelative(true)
.end();

const inlineRule = chain.module
.rule(findRuleId(chain, CHAIN_ID.RULE.SASS_INLINE))
.test(include)
.resourceQuery(/inline/);

// Support for importing raw Sass files
chain.module
.rule(CHAIN_ID.RULE.SASS_RAW)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHAIN_ID.RULE.SASS_RAW is undefined in v1.2.x. So we will get a rule like this:

      /* config.module.rule('undefined') */
      {
        test: /\.s(?:a|c)ss$/,
        resourceQuery: /raw/,
        type: 'asset/source'
      },

I'll make a patch in the next PR

Suggested change
.rule(CHAIN_ID.RULE.SASS_RAW)
.rule(CHAIN_ID.RULE.SASS_RAW ?? "sass-raw")

Expand All @@ -140,12 +135,20 @@ export const pluginSass = (

// Update the normal rule and the inline rule
const updateRules = (
callback: (
rule: RspackChain.Rule,
type: 'normal' | 'inline',
) => Promise<void>,
) =>
Promise.all([callback(rule, 'normal'), callback(inlineRule, 'inline')]);
callback: (rule: RspackChain.Rule, type: 'normal' | 'inline') => void,
) => {
callback(rule, 'normal');

// Rsbuild < 1.3.0 does not have RULE.CSS_INLINE.
if (chain.module.rules.has(CHAIN_ID.RULE.CSS_INLINE)) {
const inlineRule = chain.module
.rule(findRuleId(chain, CHAIN_ID.RULE.SASS_INLINE))
.test(include)
.resourceQuery(/inline/);

callback(inlineRule, 'inline');
}
};

const sassLoaderPath = path.join(
__dirname,
Expand All @@ -165,7 +168,7 @@ export const pluginSass = (
sourceMap: false,
};

await updateRules(async (rule, type) => {
updateRules((rule, type) => {
for (const item of excludes) {
rule.exclude.add(item);
}
Expand Down
40 changes: 20 additions & 20 deletions packages/plugin-sass/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ exports[`plugin-sass > should add sass-loader 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
{
"resourceQuery": /inline/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
Expand Down Expand Up @@ -113,11 +118,6 @@ exports[`plugin-sass > should add sass-loader 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
]
`;

Expand Down Expand Up @@ -187,6 +187,11 @@ exports[`plugin-sass > should add sass-loader and css-loader when injectStyles 1
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
{
"resourceQuery": /inline/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
Expand Down Expand Up @@ -234,11 +239,6 @@ exports[`plugin-sass > should add sass-loader and css-loader when injectStyles 1
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
]
`;

Expand Down Expand Up @@ -311,6 +311,11 @@ exports[`plugin-sass > should add sass-loader with excludes 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
{
"exclude": [
/node_modules/,
Expand Down Expand Up @@ -361,11 +366,6 @@ exports[`plugin-sass > should add sass-loader with excludes 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
]
`;

Expand Down Expand Up @@ -436,6 +436,11 @@ exports[`plugin-sass > should allow to use legacy API and mute deprecation warni
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
{
"resourceQuery": /inline/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
Expand Down Expand Up @@ -484,10 +489,5 @@ exports[`plugin-sass > should allow to use legacy API and mute deprecation warni
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"type": "asset/source",
},
]
`;
28 changes: 27 additions & 1 deletion packages/plugin-sass/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRsbuild } from '@rsbuild/core';
import { type RsbuildPluginAPI, createRsbuild } from '@rsbuild/core';
import { matchRules } from '@scripts/test-helper';
import { pluginSass } from '../src';

Expand Down Expand Up @@ -80,4 +80,30 @@ describe('plugin-sass', () => {
expect(matchRules(bundlerConfigs[0], 'a.scss').length).toBe(2);
expect(matchRules(bundlerConfigs[0], 'b.scss').length).toBe(5);
});

it('should be compatible with Rsbuild < 1.3.0', async () => {
const rsbuild = await createRsbuild({
rsbuildConfig: {
plugins: [
{
name: 'rsbuild-plugin-test',
post: ['rsbuild:css'],
setup(api: RsbuildPluginAPI) {
// Mock the behavior of Rsbuild < 1.3.0
api.modifyBundlerChain((chain, { CHAIN_ID }) => {
chain.module.rules.delete(CHAIN_ID.RULE.CSS_INLINE);
});
},
},
pluginSass(),
],
},
});

await rsbuild.initConfigs();

const bundlerConfigs = await rsbuild.initConfigs();
expect(matchRules(bundlerConfigs[0], 'a.scss').length).toBe(2);
expect(matchRules(bundlerConfigs[0], 'a.scss?inline').length).toBe(0);
});
});
16 changes: 10 additions & 6 deletions packages/plugin-stylus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ export const pluginStylus = (options?: PluginStylusOptions): RsbuildPlugin => ({
.resolve.preferRelative(true)
.end();

const inlineRule = chain.module
.rule(CHAIN_ID.RULE.STYLUS_INLINE)
.test(test)
.resourceQuery(/inline/);

// Support for importing raw Stylus files
chain.module
.rule(CHAIN_ID.RULE.STYLUS_RAW)
Expand All @@ -112,7 +107,16 @@ export const pluginStylus = (options?: PluginStylusOptions): RsbuildPlugin => ({
callback: (rule: RspackChain.Rule, type: 'normal' | 'inline') => void,
) => {
callback(rule, 'normal');
callback(inlineRule, 'inline');

// Rsbuild < 1.3.0 does not have RULE.CSS_INLINE.
if (chain.module.rules.has(CHAIN_ID.RULE.CSS_INLINE)) {
const inlineRule = chain.module
.rule(CHAIN_ID.RULE.STYLUS_INLINE)
.test(test)
.resourceQuery(/inline/);

callback(inlineRule, 'inline');
}
};

updateRules((rule, type) => {
Expand Down
Loading
Loading