๐Ÿ“ฆ hediet / node-reload

๐Ÿ“„ README.md ยท 70 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70# Hot Reloading for NodeJS

[![](https://img.shields.io/twitter/follow/hediet_dev.svg?style=social)](https://twitter.com/intent/follow?screen_name=hediet_dev)

A thoughtfully designed library that brings advanced hot reloading to NodeJS.

## Features

-   Tracks a dependency graph (files in `node_modules` and there like can be ignored).
-   Tracked files are watched for changes.
-   If a file has changed, reconcilers try to apply the change to the running app on a module basis.
-   If a reconciler is not successful, the reconcilers of the dependees are asked to apply the change.

## Usage

### Installation

```
yarn add @hediet/node-reload
```

or

```
npm install @hediet/node-reload --save
```

See the `./examples` folder for detailed examples.
Works best with TypeScript.

### Hot Reload Exported Items

`hotReloadExportedItem` makes it very easy to track changes of exported items.

```ts
import { enableHotReload } from "@hediet/node-reload/node"; // This import needs nodejs.

// Call this before importing modules that should be hot-reloaded!
enableHotReload({
	entryModule: module, // only this module and its transitive dependencies are tracked
	logging: 'debug', // useful for debugging if hot-reload does not work
});

import { hotReloadExportedItem } from "@hediet/node-reload"; // This import is bundler-friendly and works in any environment!
import { myFunction } from './dep1';

const d = hotReloadExportedItem(myFunction, myFunction => {
	// Runs initially and on every change of `myFunction`
    console.log('myFunction: ' + myFunction());
	return {
		dispose: () => {
			console.log('cleanup');
		}
	}
});
// ...
d.dispose();

```

## Similar libs

-   [node-hot](https://github.com/mihe/node-hot): Inspired this library.

## Changelog

-   0.0.2 - Initial release.
-   0.4.2 - Implements Live Debug
-   0.10.0 - Rewrite. Focus on `hotReloadExportedItem` and more portable code.