From 98b25040a2b6bdb624cbb5e8bb4de3b1952f276f Mon Sep 17 00:00:00 2001 From: HuJean <7037477+HuJean@users.noreply.github.com> Date: Tue, 24 Feb 2026 14:29:42 +0800 Subject: [PATCH] fix(rspeedy/react): alias `use-sync-external-store` (#2200) ## Summary by CodeRabbit * **Bug Fixes** * Improved module alias resolution to include .js variants for the external store package (with-selector), ensuring consistent resolution. * **Tests** * Added assertions covering .js variant alias resolution in both development and production configs. * **Chores** * Added a changeset entry and updated bundle alias snapshots to reflect adjusted module path extensions. ## Checklist - [ ] Tests updated (or not required). - [ ] Documentation updated (or not required). - [ ] Changeset added, and when a BREAKING CHANGE occurs, it needs to be clearly marked (or not required). --- .changeset/big-otters-tickle.md | 5 ++ .../test/external-bundle.test.ts | 66 ++++++++++--------- .../plugin-react/src/useSyncExternalStore.ts | 11 ++-- .../rspeedy/plugin-react/test/config.test.ts | 12 ++++ 4 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 .changeset/big-otters-tickle.md diff --git a/.changeset/big-otters-tickle.md b/.changeset/big-otters-tickle.md new file mode 100644 index 0000000000..4fb33b759e --- /dev/null +++ b/.changeset/big-otters-tickle.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/react-rsbuild-plugin": patch +--- + +Add alias for `use-sync-external-store/with-selector.js` and `use-sync-external-store/shim/with-selector.js` pointing to @lynx-js/use-sync-external-store. diff --git a/packages/rspeedy/lynx-bundle-rslib-config/test/external-bundle.test.ts b/packages/rspeedy/lynx-bundle-rslib-config/test/external-bundle.test.ts index e964b14d70..c6ae70ca89 100644 --- a/packages/rspeedy/lynx-bundle-rslib-config/test/external-bundle.test.ts +++ b/packages/rspeedy/lynx-bundle-rslib-config/test/external-bundle.test.ts @@ -343,38 +343,40 @@ describe('pluginReactLynx', () => { const config = await rslib.inspectConfig() expect(config.origin.bundlerConfigs[0]!.resolve!.alias) .toMatchInlineSnapshot(` - { - "@lynx-js/preact-devtools$": false, - "@lynx-js/react$": "/packages/react/runtime/lib/index.js", - "@lynx-js/react/compat$": "/packages/react/runtime/compat/index.js", - "@lynx-js/react/debug$": false, - "@lynx-js/react/experimental/lazy/import$": "/packages/react/runtime/lazy/import.js", - "@lynx-js/react/internal$": "/packages/react/runtime/lib/internal.js", - "@lynx-js/react/legacy-react-runtime$": "/packages/react/runtime/lib/legacy-react-runtime/index.js", - "@lynx-js/react/runtime-components$": "/packages/react/components/lib/index.js", - "@lynx-js/react/worklet-runtime/bindings$": "/packages/react/worklet-runtime/lib/bindings/index.js", - "@swc/helpers": "/node_modules//@swc/helpers", - "preact$": "/node_modules//@hongzhiyuan/preact/dist/preact.mjs", - "preact/compat$": "/node_modules//@hongzhiyuan/preact/compat/dist/compat.mjs", - "preact/compat/client$": "/node_modules//@hongzhiyuan/preact/compat/client.mjs", - "preact/compat/jsx-dev-runtime$": "/node_modules//@hongzhiyuan/preact/compat/jsx-dev-runtime.mjs", - "preact/compat/jsx-runtime$": "/node_modules//@hongzhiyuan/preact/compat/jsx-runtime.mjs", - "preact/compat/scheduler$": "/node_modules//@hongzhiyuan/preact/compat/scheduler.mjs", - "preact/compat/server$": "/node_modules//@hongzhiyuan/preact/compat/server.mjs", - "preact/debug$": "/node_modules//@hongzhiyuan/preact/debug/dist/debug.mjs", - "preact/devtools$": "/node_modules//@hongzhiyuan/preact/devtools/dist/devtools.mjs", - "preact/hooks$": "/node_modules//@hongzhiyuan/preact/hooks/dist/hooks.mjs", - "preact/jsx-dev-runtime$": "/node_modules//@hongzhiyuan/preact/jsx-runtime/dist/jsxRuntime.mjs", - "preact/jsx-runtime$": "/node_modules//@hongzhiyuan/preact/jsx-runtime/dist/jsxRuntime.mjs", - "preact/test-utils$": "/node_modules//@hongzhiyuan/preact/test-utils/dist/testUtils.mjs", - "react$": "/packages/react/runtime/lib/index.js", - "react-compiler-runtime": "/node_modules//react-compiler-runtime", - "use-sync-external-store$": "/packages/use-sync-external-store/index.js", - "use-sync-external-store/shim$": "/packages/use-sync-external-store/index.js", - "use-sync-external-store/shim/with-selector$": "/packages/use-sync-external-store/with-selector.js", - "use-sync-external-store/with-selector$": "/packages/use-sync-external-store/with-selector.js", - } - `) + { + "@lynx-js/preact-devtools$": false, + "@lynx-js/react$": "/packages/react/runtime/lib/index.js", + "@lynx-js/react/compat$": "/packages/react/runtime/compat/index.js", + "@lynx-js/react/debug$": false, + "@lynx-js/react/experimental/lazy/import$": "/packages/react/runtime/lazy/import.js", + "@lynx-js/react/internal$": "/packages/react/runtime/lib/internal.js", + "@lynx-js/react/legacy-react-runtime$": "/packages/react/runtime/lib/legacy-react-runtime/index.js", + "@lynx-js/react/runtime-components$": "/packages/react/components/lib/index.js", + "@lynx-js/react/worklet-runtime/bindings$": "/packages/react/worklet-runtime/lib/bindings/index.js", + "@swc/helpers": "/node_modules//@swc/helpers", + "preact$": "/node_modules//@hongzhiyuan/preact/dist/preact.mjs", + "preact/compat$": "/node_modules//@hongzhiyuan/preact/compat/dist/compat.mjs", + "preact/compat/client$": "/node_modules//@hongzhiyuan/preact/compat/client.mjs", + "preact/compat/jsx-dev-runtime$": "/node_modules//@hongzhiyuan/preact/compat/jsx-dev-runtime.mjs", + "preact/compat/jsx-runtime$": "/node_modules//@hongzhiyuan/preact/compat/jsx-runtime.mjs", + "preact/compat/scheduler$": "/node_modules//@hongzhiyuan/preact/compat/scheduler.mjs", + "preact/compat/server$": "/node_modules//@hongzhiyuan/preact/compat/server.mjs", + "preact/debug$": "/node_modules//@hongzhiyuan/preact/debug/dist/debug.mjs", + "preact/devtools$": "/node_modules//@hongzhiyuan/preact/devtools/dist/devtools.mjs", + "preact/hooks$": "/node_modules//@hongzhiyuan/preact/hooks/dist/hooks.mjs", + "preact/jsx-dev-runtime$": "/node_modules//@hongzhiyuan/preact/jsx-runtime/dist/jsxRuntime.mjs", + "preact/jsx-runtime$": "/node_modules//@hongzhiyuan/preact/jsx-runtime/dist/jsxRuntime.mjs", + "preact/test-utils$": "/node_modules//@hongzhiyuan/preact/test-utils/dist/testUtils.mjs", + "react$": "/packages/react/runtime/lib/index.js", + "react-compiler-runtime": "/node_modules//react-compiler-runtime", + "use-sync-external-store$": "/packages/use-sync-external-store/index.js", + "use-sync-external-store/shim$": "/packages/use-sync-external-store/index.js", + "use-sync-external-store/shim/with-selector$": "/packages/use-sync-external-store/with-selector.js", + "use-sync-external-store/shim/with-selector.js$": "/packages/use-sync-external-store/with-selector.js", + "use-sync-external-store/with-selector$": "/packages/use-sync-external-store/with-selector.js", + "use-sync-external-store/with-selector.js$": "/packages/use-sync-external-store/with-selector.js", + } + `) }) it('should handle macros', () => { diff --git a/packages/rspeedy/plugin-react/src/useSyncExternalStore.ts b/packages/rspeedy/plugin-react/src/useSyncExternalStore.ts index f9b0970422..5ea23a9ac7 100644 --- a/packages/rspeedy/plugin-react/src/useSyncExternalStore.ts +++ b/packages/rspeedy/plugin-react/src/useSyncExternalStore.ts @@ -11,19 +11,22 @@ export function applyUseSyncExternalStore(api: RsbuildPluginAPI): void { const useSyncExternalStoreEntries = [ 'use-sync-external-store', 'use-sync-external-store/with-selector', + 'use-sync-external-store/with-selector.js', 'use-sync-external-store/shim', 'use-sync-external-store/shim/with-selector', + 'use-sync-external-store/shim/with-selector.js', ] await Promise.all( - useSyncExternalStoreEntries.map(entry => - resolve(`@lynx-js/${entry}`).then(value => { + useSyncExternalStoreEntries.map(key => { + const entry = key.endsWith('.js') ? key.replace('.js', '') : key + return resolve(`@lynx-js/${entry}`).then(value => { chain .resolve .alias - .set(`${entry}$`, value) + .set(`${key}$`, value) }) - ), + }), ) }) } diff --git a/packages/rspeedy/plugin-react/test/config.test.ts b/packages/rspeedy/plugin-react/test/config.test.ts index d729186fd9..3c0e9559ff 100644 --- a/packages/rspeedy/plugin-react/test/config.test.ts +++ b/packages/rspeedy/plugin-react/test/config.test.ts @@ -168,6 +168,12 @@ describe('Config', () => { '/use-sync-external-store/with-selector.js'.replaceAll('/', path.sep), ), ) + expect(config.resolve.alias).toHaveProperty( + 'use-sync-external-store/with-selector.js$', + expect.stringContaining( + '/use-sync-external-store/with-selector.js'.replaceAll('/', path.sep), + ), + ) expect(config.resolve.alias).toHaveProperty( 'use-sync-external-store/shim$', expect.stringContaining( @@ -180,6 +186,12 @@ describe('Config', () => { '/use-sync-external-store/with-selector.js'.replaceAll('/', path.sep), ), ) + expect(config.resolve.alias).toHaveProperty( + 'use-sync-external-store/shim/with-selector.js$', + expect.stringContaining( + '/use-sync-external-store/with-selector.js'.replaceAll('/', path.sep), + ), + ) }) test('alias with production', async () => {