Skip to content
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
39 changes: 39 additions & 0 deletions e2e/cases/syntax-es/decorator-config/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ test('should allow to use stage 3 decorators', async ({
expect(await page.evaluate('window.ccc')).toBe('hello world');
});

test('should allow to use 2023-11 decorators', async ({
page,
buildPreview,
}) => {
await buildPreview({
config: {
source: {
decorators: {
version: '2023-11',
},
},
},
});

expect(await page.evaluate('window.aaa')).toBe('hello');
expect(await page.evaluate('window.bbb')).toBe('world');
expect(await page.evaluate('window.ccc')).toBe('hello world');
});

test('should use legacy decorators with babel-plugin', async ({
page,
buildPreview,
Expand Down Expand Up @@ -65,3 +84,23 @@ test('should allow to use stage 3 decorators with babel-plugin', async ({
expect(await page.evaluate('window.bbb')).toBe('world');
expect(await page.evaluate('window.ccc')).toBe('hello world');
});

test('should allow to use 2023-11 decorators with babel-plugin', async ({
page,
buildPreview,
}) => {
await buildPreview({
config: {
plugins: [pluginBabel()],
source: {
decorators: {
version: '2023-11',
},
},
},
});

expect(await page.evaluate('window.aaa')).toBe('hello');
expect(await page.evaluate('window.bbb')).toBe('world');
expect(await page.evaluate('window.ccc')).toBe('hello world');
});
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"@rspack/core": "2.0.0-beta.8",
"@swc/helpers": "^0.5.19"
"@swc/helpers": "^0.5.20"
},
"peerDependencies": {
"core-js": ">= 3.0.0"
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,10 @@ function applySwcDecoratorConfig(
swcConfig.jsc.transform.useDefineForClassFields = false;
break;
case '2022-03':
case '2023-11':
swcConfig.jsc.transform.legacyDecorator = false;
swcConfig.jsc.transform.decoratorVersion = '2022-03';
// TODO `@swc/types` does not include `2023-11` yet.
(swcConfig.jsc.transform.decoratorVersion as string) = version;
Comment on lines 314 to +318
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

builtin:swc-loader/SWC does not currently implement a working transform for decoratorVersion: '2023-11' (it can crash when that code path is hit). Setting swcConfig.jsc.transform.decoratorVersion to the user-provided '2023-11' risks breaking builds at runtime.

Suggestion: avoid passing '2023-11' into SWC until it’s supported. Either (a) throw a clear error when source.decorators.version === '2023-11' and SWC is the active transformer, recommending using the Babel plugin for 2023-11, or (b) map '2023-11' to '2022-03' for SWC and warn that semantics may differ.

Copilot uses AI. Check for mistakes.
break;
default:
throw new Error(
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ export type Decorators = {
*/
version?:
| 'legacy' // stage 1
| '2022-03'; // stage 3
| '2022-03' // stage 3
| '2023-11'; // stage 3
};

export interface SourceConfig {
Expand Down
73 changes: 73 additions & 0 deletions packages/core/tests/__snapshots__/swc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,79 @@ exports[`plugin-swc > should allow using \`tools.swc\` to configure swc-loader o
]
`;

exports[`plugin-swc > should apply decorators version 2023-11 1`] = `
[
[
{
"resolve": {
"fullySpecified": false,
},
"test": /\\\\\\.m\\?js/,
},
{
"dependency": {
"not": "url",
},
"include": [
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
{
"type": "javascript/auto",
"use": [
{
"loader": "builtin:swc-loader",
"options": {
"collectTypeScriptInfo": {
"exportedEnum": false,
"typeExports": true,
},
"env": {
"targets": [
"chrome >= 107",
"edge >= 107",
"firefox >= 104",
"safari >= 16",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"transform": {
"decoratorVersion": "2023-11",
"legacyDecorator": false,
},
},
},
},
],
},
],
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
},
],
]
`;

exports[`plugin-swc > should apply environment config correctly 1`] = `
[
{
Expand Down
10 changes: 10 additions & 0 deletions packages/core/tests/swc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ describe('plugin-swc', () => {
});
});

it('should apply decorators version 2023-11', async () => {
await matchConfigSnapshot({
source: {
decorators: {
version: '2023-11',
},
},
});
});

it('should allow using `tools.swc` to configure swc-loader options', async () => {
const rsbuild = await createRsbuild({
cwd: defaultCwd,
Expand Down
91 changes: 91 additions & 0 deletions packages/plugin-babel/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,97 @@ exports[`plugins/babel > babel-loader should works with builtin:swc-loader for R
}
`;

exports[`plugins/babel > should apply decorators version 2023-11 correctly 1`] = `
{
"dependency": {
"not": "url",
},
"include": [
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
{
"type": "javascript/auto",
"use": [
{
"loader": "builtin:swc-loader",
"options": {
"collectTypeScriptInfo": {
"exportedEnum": false,
"typeExports": true,
},
"env": {
"targets": [
"chrome >= 107",
"edge >= 107",
"firefox >= 104",
"safari >= 16",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/plugin-babel/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"transform": {
"decoratorVersion": "2023-11",
"legacyDecorator": false,
},
},
},
},
{
"loader": "<ROOT>/packages/plugin-babel/compiled/babel-loader/index.js",
"options": {
"babelrc": false,
"compact": false,
"configFile": false,
"plugins": [
[
"<PNPM_INNER>/@babel/plugin-proposal-decorators/lib/index.js",
{
"version": "2023-11",
},
],
],
"presets": [
[
"<PNPM_INNER>/@babel/preset-typescript/lib/index.js",
{
"allExtensions": true,
"allowDeclareFields": true,
"allowNamespaces": true,
"isTSX": true,
"optimizeConstEnums": true,
},
],
],
},
},
],
},
],
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
}
`;

exports[`plugins/babel > should apply environment config correctly 1`] = `
{
"dependency": {
Expand Down
20 changes: 20 additions & 0 deletions packages/plugin-babel/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ describe('plugins/babel', () => {
}
});

it('should apply decorators version 2023-11 correctly', async () => {
const rsbuild = await createRsbuild({
cwd: import.meta.dirname,
config: {
plugins: [pluginBabel()],
source: {
decorators: {
version: '2023-11',
},
},
performance: {
buildCache: false,
},
},
});

const configs = await rsbuild.initConfigs();
expect(matchRules(configs[0], 'a.tsx')[0]).toMatchSnapshot();
});

it('should set babel-loader', async () => {
const rsbuild = await createRsbuild({
cwd: import.meta.dirname,
Expand Down
Loading
Loading