Skip to content

Commit 35be3af

Browse files
authored
Merge pull request #54 from smooth-code/hot-reload
Hot Reload support
2 parents 7acd6e8 + c79085e commit 35be3af

File tree

7 files changed

+518
-375
lines changed

7 files changed

+518
-375
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

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,40 @@
3333
"devDependencies": {
3434
"babel-cli": "^6.26.0",
3535
"babel-core": "^6.26.0",
36-
"babel-eslint": "^8.2.1",
37-
"babel-jest": "^22.1.0",
36+
"babel-eslint": "^8.2.2",
37+
"babel-jest": "^22.4.3",
3838
"babel-plugin-dynamic-import-node": "^1.2.0",
3939
"babel-plugin-external-helpers": "^6.22.0",
4040
"babel-plugin-transform-class-properties": "^6.24.1",
4141
"babel-plugin-transform-object-rest-spread": "^6.26.0",
4242
"babel-preset-env": "^1.6.1",
4343
"babel-preset-react": "^6.24.1",
44-
"bundlesize": "^0.16.0",
44+
"bundlesize": "^0.17.0",
4545
"codecov": "^3.0.0",
46-
"conventional-github-releaser": "^2.0.0",
46+
"conventional-github-releaser": "^2.0.2",
4747
"enzyme": "^3.3.0",
4848
"enzyme-adapter-react-16": "^1.1.1",
49-
"eslint": "^4.16.0",
49+
"eslint": "^4.19.1",
5050
"eslint-config-airbnb": "^16.1.0",
5151
"eslint-config-prettier": "^2.9.0",
52-
"eslint-plugin-import": "^2.8.0",
52+
"eslint-plugin-import": "^2.9.0",
5353
"eslint-plugin-jsx-a11y": "^6.0.3",
54-
"eslint-plugin-react": "^7.6.1",
55-
"jest": "^22.1.4",
56-
"prettier": "^1.10.2",
54+
"eslint-plugin-react": "^7.7.0",
55+
"jest": "^22.4.3",
56+
"prettier": "^1.11.1",
5757
"react": "^16.2.0",
5858
"react-dom": "^16.2.0",
5959
"react-router": "^4.2.0",
6060
"react-test-renderer": "^16.2.0",
61-
"rollup": "^0.55.3",
61+
"rollup": "^0.57.1",
6262
"rollup-plugin-babel": "^3.0.3",
63-
"rollup-plugin-commonjs": "^8.3.0",
63+
"rollup-plugin-commonjs": "^9.1.0",
6464
"rollup-plugin-json": "^2.3.0",
65-
"rollup-plugin-node-resolve": "^3.0.2",
65+
"rollup-plugin-node-resolve": "^3.3.0",
6666
"rollup-plugin-replace": "^2.0.0",
6767
"rollup-plugin-sourcemaps": "^0.4.2",
6868
"rollup-plugin-uglify": "^3.0.0",
69-
"rollup-plugin-visualizer": "^0.3.1",
69+
"rollup-plugin-visualizer": "^0.4.0",
7070
"standard-version": "^4.3.0"
7171
},
7272
"dependencies": {

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)