Skip to content

Commit f628037

Browse files
colinaaaYradex
andauthored
feat(rspeedy/core): add dev.hmr and dev.liveReload (#458)
<!-- Thank you for submitting a pull request! We appreciate the time and effort you have invested in making these changes. Please ensure that you provide enough information to allow others to review your pull request. Upon submission, your pull request will be automatically assigned with reviewers. If you want to learn more about contributing to this project, please visit: https://github.com/lynx-family/lynx-stack/blob/main/CONTRIBUTING.md. --> ## Summary <!-- Can you explain the reasoning behind implementing this change? What problem or issue does this pull request resolve? --> fix: #298 <!-- It would be helpful if you could provide any relevant context, such as GitHub issues or related discussions. --> ## Checklist <!--- Check and mark with an "x" --> - [x] Tests updated (or not required). - [x] Documentation updated (or not required). --------- Signed-off-by: Qingyu Wang <[email protected]> Co-authored-by: Yradex <[email protected]>
1 parent c2523bc commit f628037

File tree

6 files changed

+182
-1
lines changed

6 files changed

+182
-1
lines changed

.changeset/sweet-crabs-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lynx-js/rspeedy": patch
3+
---
4+
5+
Support `dev.hmr` and `dev.liveReload`.

packages/rspeedy/core/etc/rspeedy.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ export function defineConfig(config: Config): Config;
134134
export interface Dev {
135135
assetPrefix?: string | boolean | undefined;
136136
client?: DevClient | undefined;
137+
hmr?: boolean | undefined;
138+
liveReload?: boolean | undefined;
137139
progressBar?: boolean | {
138140
id?: string;
139141
} | undefined;

packages/rspeedy/core/src/config/dev/index.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,88 @@ export interface Dev {
7575
*/
7676
client?: Client | undefined
7777

78+
/**
79+
* Whether to enable Hot Module Replacement (HMR).
80+
*
81+
* @remarks
82+
*
83+
* Defaults to `true`.
84+
*
85+
* By default, Rspeedy uses HMR as the preferred method to update modules. If HMR is disabled or cannot be used in certain scenarios, it will automatically fallback to {@link Dev.liveReload}.
86+
*
87+
* To completely disable both HMR and live reload, set both `dev.hmr` and `dev.liveReload` to `false`. Then, no WebSocket requests will be made to the dev server on the page, and the page will not automatically refresh when file changes.
88+
*
89+
* @example
90+
*
91+
* Disable HMR:
92+
*
93+
* ```js
94+
* import { defineConfig } from '@lynx-js/rspeedy'
95+
*
96+
* export default defineConfig({
97+
* dev: {
98+
* hmr: false,
99+
* },
100+
* })
101+
* ```
102+
*
103+
* @example
104+
*
105+
* Disable both HMR and live reload:
106+
*
107+
* ```js
108+
* import { defineConfig } from '@lynx-js/rspeedy'
109+
*
110+
* export default defineConfig({
111+
* dev: {
112+
* hmr: false,
113+
* liveReload: false,
114+
* },
115+
* })
116+
* ```
117+
*/
118+
hmr?: boolean | undefined
119+
120+
/**
121+
* Whether to enable live reload functionality.
122+
*
123+
* Defaults to `true`.
124+
*
125+
* Live reload is used as a fallback when {@link Dev.hmr} is disabled or cannot be used in certain scenarios. When enabled, the page will automatically refresh when source files are changed.
126+
*
127+
* To completely disable both HMR and live reload, set both `dev.hmr` and `dev.liveReload` to `false`. Then, no WebSocket requests will be made to the dev server on the page, and the page will not automatically refresh when file changes.
128+
*
129+
* @example
130+
*
131+
* Disable live reload:
132+
*
133+
* ```js
134+
* import { defineConfig } from '@lynx-js/rspeedy'
135+
*
136+
* export default defineConfig({
137+
* dev: {
138+
* liveReload: false,
139+
* },
140+
* })
141+
* ```
142+
*
143+
* @example
144+
*
145+
* Disable both HMR and live reload:
146+
*
147+
* ```js
148+
* import { defineConfig } from '@lynx-js/rspeedy'
149+
*
150+
* export default defineConfig({
151+
* dev: {
152+
* hmr: false,
153+
* liveReload: false,
154+
* },
155+
* })
156+
* ```
157+
*/
158+
liveReload?: boolean | undefined
159+
78160
/**
79161
* Watch specified files and directories for changes. When a file change is detected, it can trigger a page reload or restart the dev server.
80162
*

packages/rspeedy/core/src/plugins/dev.plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function pluginDev(
116116
hostname
117117
}&port=${
118118
api.context.devServer?.port
119-
}&pathname=/rsbuild-hmr&hot=true&live-reload=true&protocol=ws`
119+
}&pathname=/rsbuild-hmr&hot=${options?.hmr ?? true}&live-reload=${options?.liveReload ?? true}&protocol=ws`
120120
)
121121
.set(
122122
'@rspack/core/hot/dev-server',

packages/rspeedy/core/test/config/dev.test-d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ describe('Config - Dev', () => {
3434
})
3535
})
3636

37+
test('hmr', () => {
38+
assertType<Dev>({ hmr: true })
39+
assertType<Dev>({ hmr: false })
40+
})
41+
42+
test('liveReload', () => {
43+
assertType<Dev>({ liveReload: true })
44+
assertType<Dev>({ liveReload: false })
45+
})
46+
3747
test('watchFiles', () => {
3848
assertType<Dev>({ watchFiles: { paths: '' } })
3949
assertType<Dev>({ watchFiles: { paths: [] } })

packages/rspeedy/core/test/plugins/dev.plugin.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,88 @@ describe('Plugins - Dev', () => {
356356
expect(config.output?.publicPath).not.toBe(`http://example.com:${port}`)
357357
})
358358

359+
test('dev.hmr default', async () => {
360+
const rsbuild = await createStubRspeedy({})
361+
362+
const config = await rsbuild.unwrapConfig()
363+
364+
expect(config.resolve?.alias).toHaveProperty(
365+
'@lynx-js/webpack-dev-transport/client',
366+
expect.stringContaining('hot=true'),
367+
)
368+
})
369+
370+
test('dev.hmr: false', async () => {
371+
const rsbuild = await createStubRspeedy({
372+
dev: {
373+
hmr: false,
374+
},
375+
})
376+
377+
const config = await rsbuild.unwrapConfig()
378+
379+
expect(config.resolve?.alias).toHaveProperty(
380+
'@lynx-js/webpack-dev-transport/client',
381+
expect.stringContaining('hot=false'),
382+
)
383+
})
384+
385+
test('dev.hmr: true', async () => {
386+
const rsbuild = await createStubRspeedy({
387+
dev: {
388+
hmr: true,
389+
},
390+
})
391+
392+
const config = await rsbuild.unwrapConfig()
393+
394+
expect(config.resolve?.alias).toHaveProperty(
395+
'@lynx-js/webpack-dev-transport/client',
396+
expect.stringContaining('hot=true'),
397+
)
398+
})
399+
400+
test('dev.liveReload default', async () => {
401+
const rsbuild = await createStubRspeedy({})
402+
403+
const config = await rsbuild.unwrapConfig()
404+
405+
expect(config.resolve?.alias).toHaveProperty(
406+
'@lynx-js/webpack-dev-transport/client',
407+
expect.stringContaining('live-reload=true'),
408+
)
409+
})
410+
411+
test('dev.liveReload: false', async () => {
412+
const rsbuild = await createStubRspeedy({
413+
dev: {
414+
liveReload: false,
415+
},
416+
})
417+
418+
const config = await rsbuild.unwrapConfig()
419+
420+
expect(config.resolve?.alias).toHaveProperty(
421+
'@lynx-js/webpack-dev-transport/client',
422+
expect.stringContaining('live-reload=false'),
423+
)
424+
})
425+
426+
test('dev.liveReload: true', async () => {
427+
const rsbuild = await createStubRspeedy({
428+
dev: {
429+
liveReload: true,
430+
},
431+
})
432+
433+
const config = await rsbuild.unwrapConfig()
434+
435+
expect(config.resolve?.alias).toHaveProperty(
436+
'@lynx-js/webpack-dev-transport/client',
437+
expect.stringContaining('live-reload=true'),
438+
)
439+
})
440+
359441
test('websocketTransport', async () => {
360442
const rsbuild = await createStubRspeedy({
361443
dev: {

0 commit comments

Comments
 (0)