From 39a9feff2ceca3c86d69eb0a3bf4f44b564a4247 Mon Sep 17 00:00:00 2001 From: wangyiming Date: Tue, 3 Sep 2024 19:47:05 +0800 Subject: [PATCH 1/2] fix(plugin-swc): should verify the version correctly --- .changeset/silly-moons-drive.md | 6 ++++++ packages/compat/plugin-swc/src/utils.ts | 6 +++++- .../compat/plugin-swc/tests/utils.test.ts | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .changeset/silly-moons-drive.md create mode 100644 packages/compat/plugin-swc/tests/utils.test.ts diff --git a/.changeset/silly-moons-drive.md b/.changeset/silly-moons-drive.md new file mode 100644 index 0000000000..4cf1951839 --- /dev/null +++ b/.changeset/silly-moons-drive.md @@ -0,0 +1,6 @@ +--- +'@rsbuild/plugin-swc': patch +--- + +fix(plugin-swc): should verify the version correctly +fix(plugin-swc): 修复验证 react 版本的问题 diff --git a/packages/compat/plugin-swc/src/utils.ts b/packages/compat/plugin-swc/src/utils.ts index 84b5698fd7..c5713efb08 100644 --- a/packages/compat/plugin-swc/src/utils.ts +++ b/packages/compat/plugin-swc/src/utils.ts @@ -50,6 +50,10 @@ async function findUp({ } } +export const isVersionBeyond17 = (version: string): boolean => { + return semver.gte(semver.minVersion(version)!, '17.0.0'); +}; + const isBeyondReact17 = async (cwd: string) => { const pkgPath = await findUp({ cwd, filename: 'package.json' }); @@ -67,7 +71,7 @@ const isBeyondReact17 = async (cwd: string) => { return false; } - return semver.satisfies(semver.minVersion(deps.react)!, '>=17.0.0'); + return isVersionBeyond17(deps.react); }; /** diff --git a/packages/compat/plugin-swc/tests/utils.test.ts b/packages/compat/plugin-swc/tests/utils.test.ts new file mode 100644 index 0000000000..40584071f7 --- /dev/null +++ b/packages/compat/plugin-swc/tests/utils.test.ts @@ -0,0 +1,19 @@ +import { isVersionBeyond17 } from '../src/utils'; + +describe('isVersionBeyondReact17', () => { + test('Should return true for version 17 and above', () => { + expect(isVersionBeyond17('17.0.0')).toBe(true); + expect(isVersionBeyond17('17.0.1')).toBe(true); + expect(isVersionBeyond17('17.0.1-canary')).toBe(true); + expect(isVersionBeyond17('^17.0.0')).toBe(true); + expect(isVersionBeyond17('~18.2.0')).toBe(true); + expect(isVersionBeyond17('18.3.0-canary')).toBe(true); + }); + + test('Should return false for below version 17', () => { + expect(isVersionBeyond17('16.14.0')).toBe(false); + expect(isVersionBeyond17('16.8.0-alpha-1')).toBe(false); + expect(isVersionBeyond17('^16.0.0')).toBe(false); + expect(isVersionBeyond17('~15.0.0')).toBe(false); + }); +}); From 1dc7a3334fc5f71a6099de88deaa8138a82afbd3 Mon Sep 17 00:00:00 2001 From: wangyiming Date: Tue, 3 Sep 2024 19:54:28 +0800 Subject: [PATCH 2/2] fix: typo --- .changeset/silly-moons-drive.md | 6 ------ packages/compat/plugin-swc/tests/utils.test.ts | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) delete mode 100644 .changeset/silly-moons-drive.md diff --git a/.changeset/silly-moons-drive.md b/.changeset/silly-moons-drive.md deleted file mode 100644 index 4cf1951839..0000000000 --- a/.changeset/silly-moons-drive.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@rsbuild/plugin-swc': patch ---- - -fix(plugin-swc): should verify the version correctly -fix(plugin-swc): 修复验证 react 版本的问题 diff --git a/packages/compat/plugin-swc/tests/utils.test.ts b/packages/compat/plugin-swc/tests/utils.test.ts index 40584071f7..03df3b3ef6 100644 --- a/packages/compat/plugin-swc/tests/utils.test.ts +++ b/packages/compat/plugin-swc/tests/utils.test.ts @@ -1,7 +1,7 @@ import { isVersionBeyond17 } from '../src/utils'; describe('isVersionBeyondReact17', () => { - test('Should return true for version 17 and above', () => { + test('should return true for version 17 and above', () => { expect(isVersionBeyond17('17.0.0')).toBe(true); expect(isVersionBeyond17('17.0.1')).toBe(true); expect(isVersionBeyond17('17.0.1-canary')).toBe(true); @@ -10,7 +10,7 @@ describe('isVersionBeyondReact17', () => { expect(isVersionBeyond17('18.3.0-canary')).toBe(true); }); - test('Should return false for below version 17', () => { + test('should return false for below version 17', () => { expect(isVersionBeyond17('16.14.0')).toBe(false); expect(isVersionBeyond17('16.8.0-alpha-1')).toBe(false); expect(isVersionBeyond17('^16.0.0')).toBe(false);