Skip to content

Commit 8572047

Browse files
committed
feat: trigger reload when dependent file or reload path changes
1 parent 3abb011 commit 8572047

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/hot_hook/src/loader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export class HotHookLoader {
5757
* to the main thread.
5858
*/
5959
debug('Changed %s', realFilePath)
60-
if (this.#isReloadPath(realFilePath)) {
60+
const dependents = this.#dependencyTree.getDependents(realFilePath) || []
61+
if ([...dependents, realFilePath].some((path) => this.#isReloadPath(path))) {
6162
return this.#messagePort?.postMessage({ type: 'hot-hook:full-reload', path: realFilePath })
6263
}
6364

packages/hot_hook/tests/loader.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,56 @@ test.group('Loader', () => {
222222

223223
assert.isDefined(result)
224224
})
225+
226+
test('dependent files of reload paths should trigger a full reload', async ({ fs, assert }) => {
227+
await fakeInstall(fs.basePath)
228+
229+
await fs.createJson('package.json', { type: 'module' })
230+
await fs.create(
231+
'config/test.js',
232+
`
233+
import '../app/test.js'
234+
console.log("Hello")
235+
`
236+
)
237+
await fs.create('app/test.js', 'console.log("Hello")')
238+
await fs.create(
239+
'server.js',
240+
`import * as http from 'http'
241+
import { hot } from 'hot-hook'
242+
import { join } from 'node:path'
243+
import { setTimeout } from 'node:timers/promises'
244+
245+
await hot.init({
246+
projectRoot: join(import.meta.dirname, '.'),
247+
reload: ['config/**/*'],
248+
})
249+
250+
await import('./config/test.js')
251+
await setTimeout(100)
252+
console.log('Server is running')
253+
await setTimeout(2000)
254+
`
255+
)
256+
257+
const server = runProcess('server.js', {
258+
cwd: fs.basePath,
259+
env: { NODE_DEBUG: 'hot-hook' },
260+
})
261+
262+
await server.waitForOutput('Server is running')
263+
264+
await fs.create('app/test.js', 'console.log("Hello Updated")')
265+
await setTimeout(100)
266+
267+
const result = await pEvent(
268+
server.child,
269+
'message',
270+
(message: any) =>
271+
message?.type === 'hot-hook:full-reload' &&
272+
message.path === join(fs.basePath, 'app/test.js')
273+
)
274+
275+
assert.isDefined(result)
276+
})
225277
})

0 commit comments

Comments
 (0)