Skip to content

Commit c79085e

Browse files
committed
feat: add Hot Reload support
Thanks to @likun7981 for the original idea!
1 parent 01c36b8 commit c79085e

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,17 @@ import { getState } from 'loadable-components'
340340
window.snapSaveState = () => getState()
341341
```
342342

343+
### Hot Reloading
344+
345+
Loadable Components is Hot Reload friendly, it works out of the box with [React Hot Loader](https://github.com/gaearon/react-hot-loader) without adding a single line of code. All components are loaded when a reload occurs, if you want to disable this behaviour, you can set a global config:
346+
347+
```js
348+
import { setConfig } from 'loadable-components'
349+
350+
// Disable automatic "load" of components
351+
setConfig({ hotReload: false })
352+
```
353+
343354
## API Reference
344355

345356
### loadable

src/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
const _config = {
4+
// Automatically load components in hot reload environment
5+
hotReload: process.env.NODE_ENV === 'development',
6+
}
7+
8+
export const setConfig = config => Object.assign(_config, config)
9+
export const getConfig = () => _config

src/config.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getConfig, setConfig } from './config'
2+
3+
describe('config', () => {
4+
it('should set and get config', () => {
5+
expect(getConfig()).toEqual({ hotReload: false })
6+
setConfig({ hotReload: true })
7+
expect(getConfig()).toEqual({ hotReload: true })
8+
})
9+
})

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export { default as loadComponents } from './loadComponents'
44
export { default as getState } from './getState'
55
export { LOADABLE } from './constants'
66
export { default } from './loadable'
7+
export { setConfig } from './config'
78
export const componentTracker = tracker

src/loadable.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react'
33
import { LOADABLE } from './constants'
44
import resolveModuleDefault from './utils/resolveModuleDefault'
55
import * as componentTracker from './componentTracker'
6+
import { getConfig } from './config'
67

78
const EmptyComponent = () => null
89

@@ -93,6 +94,10 @@ function loadable(
9394

9495
LoadableComponent[LOADABLE] = () => LoadableComponent
9596

97+
if (module && module.hot && getConfig().hotReload) {
98+
LoadableComponent.load()
99+
}
100+
96101
if (modules) {
97102
const id = componentTracker.track(LoadableComponent, modules)
98103
LoadableComponent.componentId = id

0 commit comments

Comments
 (0)