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..03df3b3ef6 --- /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); + }); +});