-
Notifications
You must be signed in to change notification settings - Fork 159
feat: add node compatibility guard and lazy command loading for rspack #1397
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
Open
dannyhw
wants to merge
2
commits into
feat/rspack-2-foundations
Choose a base branch
from
feat/rspack-2-node-guard
base: feat/rspack-2-foundations
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@callstack/repack": patch | ||
| --- | ||
|
|
||
| Raise a clear error when running the Rspack-based commands with Rspack 2 on Node.js older than 20.19 (Rspack 2 requires Node `^20.19.0 || >=22.12.0`), instead of failing with `ERR_REQUIRE_ESM`. Rspack commands are now loaded lazily so the version check runs before `@rspack/core` is touched. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/repack/src/commands/rspack/__tests__/commandsModuleLoad.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Loading '@rspack/core' at require time would crash with ERR_REQUIRE_ESM | ||
| // on Node versions unsupported by Rspack 2, before the Node compatibility | ||
| // guard has a chance to run. This mock turns any eager load into a test | ||
| // failure. Note that bundle.js/start.js are deliberately NOT mocked here, | ||
| // so this fails if the commands module ever imports them eagerly again. | ||
| jest.mock('@rspack/core', () => { | ||
| throw new Error( | ||
| '@rspack/core must not be loaded when the rspack commands module is imported' | ||
| ); | ||
| }); | ||
|
|
||
| describe('rspack commands module', () => { | ||
| it('registers commands without loading @rspack/core', () => { | ||
| expect(() => require('../index.js')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('exposes the expected commands', () => { | ||
| const commands: typeof import('../index.js').default = | ||
| require('../index.js').default; | ||
|
|
||
| expect(commands.map((command) => command.name)).toEqual([ | ||
| 'bundle', | ||
| 'webpack-bundle', | ||
| 'start', | ||
| 'webpack-start', | ||
| ]); | ||
|
|
||
| for (const command of commands) { | ||
| expect(typeof command.func).toBe('function'); | ||
| } | ||
| }); | ||
| }); |
72 changes: 72 additions & 0 deletions
72
packages/repack/src/commands/rspack/__tests__/ensureNodeCompat.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { CLIError, getRspackVersion } from '../../../helpers/index.js'; | ||
| import { ensureNodeSupportsRspack } from '../ensureNodeCompat.js'; | ||
|
|
||
| jest.mock('../../../helpers/index.js', () => ({ | ||
| ...jest.requireActual<typeof import('../../../helpers/index.js')>( | ||
| '../../../helpers/index.js' | ||
| ), | ||
| getRspackVersion: jest.fn(), | ||
| })); | ||
|
|
||
| const getRspackVersionMock = jest.mocked(getRspackVersion); | ||
|
|
||
| // `process.versions.node` is a read-only property, so it cannot be | ||
| // mocked with `jest.replaceProperty` - redefine it manually instead. | ||
| const originalNodeVersion = Object.getOwnPropertyDescriptor( | ||
| process.versions, | ||
| 'node' | ||
| ); | ||
|
|
||
| const setup = (rspackVersion: string | null, nodeVersion: string) => { | ||
| getRspackVersionMock.mockReturnValue(rspackVersion); | ||
| Object.defineProperty(process.versions, 'node', { | ||
| value: nodeVersion, | ||
| configurable: true, | ||
| }); | ||
| }; | ||
|
|
||
| afterAll(() => { | ||
| if (originalNodeVersion) { | ||
| Object.defineProperty(process.versions, 'node', originalNodeVersion); | ||
| } | ||
| }); | ||
|
|
||
| describe('ensureNodeSupportsRspack', () => { | ||
| it('does nothing when @rspack/core is not resolvable', () => { | ||
| setup(null, '18.20.0'); | ||
| expect(() => ensureNodeSupportsRspack()).not.toThrow(); | ||
| }); | ||
|
|
||
| it('does nothing for Rspack 1.x regardless of Node version', () => { | ||
| setup('1.5.8', '18.20.0'); | ||
| expect(() => ensureNodeSupportsRspack()).not.toThrow(); | ||
| }); | ||
|
|
||
| it.each(['18.20.0', '20.18.3', '21.7.3', '22.11.0'])( | ||
| 'throws CLIError for Rspack 2 on unsupported Node %s', | ||
| (nodeVersion) => { | ||
| setup('2.0.0', nodeVersion); | ||
| expect(() => ensureNodeSupportsRspack()).toThrow(CLIError); | ||
| } | ||
| ); | ||
|
|
||
| it.each(['20.19.0', '20.19.4', '22.12.0', '24.0.0'])( | ||
| 'does nothing for Rspack 2 on supported Node %s', | ||
| (nodeVersion) => { | ||
| setup('2.0.0', nodeVersion); | ||
| expect(() => ensureNodeSupportsRspack()).not.toThrow(); | ||
| } | ||
| ); | ||
|
|
||
| it('treats prerelease Rspack 2 versions as Rspack 2', () => { | ||
| setup('2.0.0-beta.1', '18.20.0'); | ||
| expect(() => ensureNodeSupportsRspack()).toThrow(CLIError); | ||
| }); | ||
|
|
||
| it('reports both the Rspack and Node versions in the error', () => { | ||
| setup('2.0.0', '18.20.0'); | ||
| expect(() => ensureNodeSupportsRspack()).toThrow( | ||
| /Rspack 2\.0\.0 requires Node\.js .+ found 18\.20\.0/ | ||
| ); | ||
| }); | ||
| }); |
94 changes: 94 additions & 0 deletions
94
packages/repack/src/commands/rspack/__tests__/lazyCommands.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type { | ||
| BundleArguments, | ||
| CliConfig, | ||
| StartArguments, | ||
| } from '../../types.js'; | ||
| import { bundle } from '../bundle.js'; | ||
| import { ensureNodeSupportsRspack } from '../ensureNodeCompat.js'; | ||
| import commands from '../index.js'; | ||
| import { start } from '../start.js'; | ||
|
|
||
| // safety net: any code path that loads '@rspack/core' fails loudly | ||
| jest.mock('@rspack/core', () => { | ||
| throw new Error( | ||
| '@rspack/core must not be loaded before the Node compatibility guard runs' | ||
| ); | ||
| }); | ||
|
|
||
| jest.mock('../ensureNodeCompat.js', () => ({ | ||
| ensureNodeSupportsRspack: jest.fn(), | ||
| })); | ||
| jest.mock('../bundle.js', () => ({ bundle: jest.fn() })); | ||
| jest.mock('../start.js', () => ({ start: jest.fn() })); | ||
|
|
||
| const guardMock = jest.mocked(ensureNodeSupportsRspack); | ||
| const bundleMock = jest.mocked(bundle); | ||
| const startMock = jest.mocked(start); | ||
|
|
||
| const [bundleCommand, webpackBundleCommand, startCommand, webpackStartCommand] = | ||
| commands; | ||
|
|
||
| const cliConfig: CliConfig = { | ||
| root: '/project', | ||
| platforms: ['ios', 'android'], | ||
| reactNativePath: '/project/node_modules/react-native', | ||
| }; | ||
|
|
||
| const bundleArgs: BundleArguments = { platform: 'ios', dev: true }; | ||
| const startArgs: StartArguments = { host: '' }; | ||
|
|
||
| describe('lazy rspack commands', () => { | ||
| it.each([ | ||
| ['bundle', bundleCommand], | ||
| ['webpack-bundle', webpackBundleCommand], | ||
| ] as const)( | ||
| '%s runs the Node compatibility guard before delegating to bundle', | ||
| async (_name, command) => { | ||
| await command.func([], cliConfig, bundleArgs); | ||
|
|
||
| expect(guardMock).toHaveBeenCalledTimes(1); | ||
| expect(bundleMock).toHaveBeenCalledWith([], cliConfig, bundleArgs); | ||
| expect(guardMock.mock.invocationCallOrder[0]).toBeLessThan( | ||
| bundleMock.mock.invocationCallOrder[0] | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| it.each([ | ||
| ['start', startCommand], | ||
| ['webpack-start', webpackStartCommand], | ||
| ] as const)( | ||
| '%s runs the Node compatibility guard before delegating to start', | ||
| async (_name, command) => { | ||
| await command.func([], cliConfig, startArgs); | ||
|
|
||
| expect(guardMock).toHaveBeenCalledTimes(1); | ||
| expect(startMock).toHaveBeenCalledWith([], cliConfig, startArgs); | ||
| expect(guardMock.mock.invocationCallOrder[0]).toBeLessThan( | ||
| startMock.mock.invocationCallOrder[0] | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| it('does not run bundle when the guard throws', async () => { | ||
| guardMock.mockImplementationOnce(() => { | ||
| throw new Error('Rspack 2.0.0 requires Node.js ^20.19.0 || >=22.12.0'); | ||
| }); | ||
|
|
||
| await expect(bundleCommand.func([], cliConfig, bundleArgs)).rejects.toThrow( | ||
| 'Rspack 2.0.0 requires Node.js' | ||
| ); | ||
| expect(bundleMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not run start when the guard throws', async () => { | ||
| guardMock.mockImplementationOnce(() => { | ||
| throw new Error('Rspack 2.0.0 requires Node.js ^20.19.0 || >=22.12.0'); | ||
| }); | ||
|
|
||
| await expect(startCommand.func([], cliConfig, startArgs)).rejects.toThrow( | ||
| 'Rspack 2.0.0 requires Node.js' | ||
| ); | ||
| expect(startMock).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import semver from 'semver'; | ||
| import { CLIError, getRspackVersion } from '../../helpers/index.js'; | ||
|
|
||
| // Rspack 2 is published as a pure ESM package and declares | ||
| // engines.node: "^20.19.0 || >=22.12.0" - the versions where | ||
| // loading ESM through require() is supported. | ||
| const RSPACK_2_NODE_RANGE = '^20.19.0 || >=22.12.0'; | ||
|
|
||
| /** | ||
| * Fails fast with an actionable error when the installed Rspack major | ||
| * requires a newer Node.js version than the current one. | ||
| * | ||
| * Without this guard, loading `@rspack/core` v2 on an unsupported Node | ||
| * version crashes with an obscure `ERR_REQUIRE_ESM` error. | ||
| */ | ||
| export function ensureNodeSupportsRspack() { | ||
| const rspackVersion = getRspackVersion(); | ||
|
|
||
| // not resolvable - let the import of '@rspack/core' surface its own error | ||
| if (!rspackVersion) return; | ||
|
|
||
| if (semver.major(semver.coerce(rspackVersion) ?? '0.0.0') < 2) return; | ||
|
|
||
| if (semver.satisfies(process.versions.node, RSPACK_2_NODE_RANGE)) return; | ||
|
|
||
| throw new CLIError( | ||
| `Rspack ${rspackVersion} requires Node.js ^20.19.0 || >=22.12.0 - ` + | ||
| `found ${process.versions.node}. ` + | ||
| 'Upgrade Node.js or use @rspack/core@1 instead.' | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.