diff --git a/.changeset/crazy-jeans-hunt.md b/.changeset/crazy-jeans-hunt.md new file mode 100644 index 0000000000..50e2cfbca1 --- /dev/null +++ b/.changeset/crazy-jeans-hunt.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/rspeedy": patch +--- + +Fix the "lynx.getJSModule is not a function" error on Web platform diff --git a/packages/rspeedy/core/src/plugins/dev.plugin.ts b/packages/rspeedy/core/src/plugins/dev.plugin.ts index ba80b9c8fd..49942a337a 100644 --- a/packages/rspeedy/core/src/plugins/dev.plugin.ts +++ b/packages/rspeedy/core/src/plugins/dev.plugin.ts @@ -12,6 +12,7 @@ import color from 'picocolors' import type { Dev } from '../config/dev/index.js' import type { Server } from '../config/server/index.js' import { debug } from '../debug.js' +import { isLynx } from '../utils/is-lynx.js' import { ProvidePlugin } from '../webpack/ProvidePlugin.js' export function pluginDev( @@ -147,13 +148,9 @@ export function pluginDev( ) .end() .end() - .plugin('lynx.hmr.provide') + .plugin('lynx.hmr.provide.dev_server_client') .use(ProvidePlugin, [ { - WebSocket: [ - options?.client?.websocketTransport ?? require.resolve('@lynx-js/websocket'), - 'default', - ], __webpack_dev_server_client__: [ require.resolve( './client/hmr/WebSocketClient.js', @@ -166,6 +163,17 @@ export function pluginDev( } ]) .end() + if (isLynx(environment)) { + chain.plugin('lynx.hmr.provide.websocket') + .use(ProvidePlugin, [{ + WebSocket: [ + options?.client?.websocketTransport + ?? require.resolve('@lynx-js/websocket'), + 'default', + ], + }]) + .end() + } }) }, } diff --git a/packages/rspeedy/core/test/plugins/dev.plugin.test.ts b/packages/rspeedy/core/test/plugins/dev.plugin.test.ts index cf7fdc0736..d08f7db9ff 100644 --- a/packages/rspeedy/core/test/plugins/dev.plugin.test.ts +++ b/packages/rspeedy/core/test/plugins/dev.plugin.test.ts @@ -73,8 +73,10 @@ describe('Plugins - Dev', () => { expect(vi.isMockFunction(ProvidePlugin)).toBe(true) expect(vi.mocked(ProvidePlugin)).toBeCalled() - expect(ProvidePlugin).toBeCalledWith({ + expect(ProvidePlugin).toHaveBeenCalledWith({ WebSocket: [require.resolve('@lynx-js/websocket'), 'default'], + }) + expect(ProvidePlugin).toHaveBeenCalledWith({ __webpack_dev_server_client__: [ require.resolve('../../client/hmr/WebSocketClient.js'), 'default', @@ -99,6 +101,27 @@ describe('Plugins - Dev', () => { ) }) + test('no Websocket class injected for web', async () => { + const rsbuild = await createStubRspeedy({ + environments: { + web: {}, + }, + }) + + await rsbuild.unwrapConfig() + + const { ProvidePlugin } = await import('../../src/webpack/ProvidePlugin.js') + + expect(vi.isMockFunction(ProvidePlugin)).toBe(true) + expect(vi.mocked(ProvidePlugin)).toBeCalled() + expect(ProvidePlugin).toBeCalledWith({ + __webpack_dev_server_client__: [ + require.resolve('../../client/hmr/WebSocketClient.js'), + 'default', + ], + }) + }) + test('not inject entry and provide variables in production', async () => { vi.stubEnv('NODE_ENV', 'production') const rsbuild = await createStubRspeedy({}) @@ -501,8 +524,10 @@ describe('Plugins - Dev', () => { const { ProvidePlugin } = await import('../../src/webpack/ProvidePlugin.js') - expect(ProvidePlugin).toBeCalledWith({ + expect(ProvidePlugin).toHaveBeenCalledWith({ WebSocket: ['/foo', 'default'], + }) + expect(ProvidePlugin).toHaveBeenCalledWith({ __webpack_dev_server_client__: [ require.resolve('../../client/hmr/WebSocketClient.js'), 'default',