Skip to content

Commit

Permalink
fix: fix various bugs (#857)
Browse files Browse the repository at this point in the history
* safe define displayName, fix #845

* stand children did not exists, fix #843

* improve TS documentation

* add SSR+HRM example

* subrender - fix deferred updates

* dont fix package version

* should have react as a peer dep

* use didMount instead of will, related to #860
  • Loading branch information
theKashey authored and gregberge committed Feb 16, 2018
1 parent 99da77b commit 8fa1d42
Show file tree
Hide file tree
Showing 25 changed files with 5,242 additions and 235 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ Just add babel-loader into your webpack configuration, with RHL-only config.
}
```

You **also have to modify tsconfig**

```json
{
...
"module": "commonjs", // module should be commonjs, as long "imports" in TS and Babel works differently.
"target": "es6", // target should be es6, or RHL will be unable to change some class members
...
}
```

Yet again - module = es6 **will not work**.

### Parcel Bundler

Parcel's HRM is a bit different.
Expand Down
11 changes: 11 additions & 0 deletions examples/SSR/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"presets": [
"env",
"react"
],
"plugins": [
"react-hot-loader/babel",
"transform-class-properties",
"dynamic-import-node"
]
}
1 change: 1 addition & 0 deletions examples/SSR/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
28 changes: 28 additions & 0 deletions examples/SSR/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "react-hot-loader-ssr",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"build": "webpack",
"start": "babel-node ./src/server.js",
"start:webpack": "webpack-dev-server --hot"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"html-webpack-plugin": "^2.30.1",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.9.7"
},
"dependencies": {
"babel-cli": "^6.26.0",
"express": "^4.16.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-hot-loader": "next",
"react-portal": "^4.1.2"
}
}
19 changes: 19 additions & 0 deletions examples/SSR/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { hot, setConfig } from 'react-hot-loader'
import * as React from 'react'
import Counter from './Counter'
// import hidden from './HiddenComponent';

const App = () => (
<h1>
<p>{40}!</p>
<Counter />
<div>
{/*{hidden().counter}*/}
SSR, and I work fine!
</div>
</h1>
)

setConfig({ logLevel: 'debug' })

export default hot(module)(App)
29 changes: 29 additions & 0 deletions examples/SSR/src/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'

const RAND = 1 //Math.round(Math.random() * 1000)

class Counter extends React.Component {
state = { count: 0 }
gen = 0

componentDidMount() {
this.setState({
count: RAND,
})
}

componentWillUnmount() {
clearInterval(this.interval)
}

render() {
// gen should change. count - no.
return (
<span>
{this.state.count}:{this.gen++}
</span>
)
}
}

export default Counter
14 changes: 14 additions & 0 deletions examples/SSR/src/HiddenComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import Counter from './Counter'

const hidden = function() {
return {
counter: () => (
<div>
this is hidden counter(<Counter />)
</div>
),
}
}

export default hidden
10 changes: 10 additions & 0 deletions examples/SSR/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'babel-polyfill'
import React from 'react'
import { hydrate } from 'react-dom'
import App from './App'

//const root = document.createElement('div')
//document.body.appendChild(root)

const root = document.getElementById('root');
hydrate(<App />, root)
25 changes: 25 additions & 0 deletions examples/SSR/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import express from 'express'
import React from 'react'
import { renderToString } from 'react-dom/server'
import App from './App'
import template from './template'

const server = express()

server.use('/dist', express.static('dist'))

server.get('/', (req, res) => {
const app = <App />

const appString = renderToString(app)

res.send(
template({
body: appString,
}),
)
})

server.listen(8080)

console.log('server ready')
15 changes: 15 additions & 0 deletions examples/SSR/src/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default ({ body }) => {
return `
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="root">${body}</div>
</body>
<script src="/dist/bundle.js"></script>
</html>
`
}
26 changes: 26 additions & 0 deletions examples/SSR/webpack.config.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable */
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
entry: ['webpack/hot/dev-server', './src/index'],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
//exclude: /node_modules|packages/, // should work without exclude
test: /\.js$/,
use: 'babel-loader',
},
],
},
plugins: [
new HtmlWebpackPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
],
}
Loading

0 comments on commit 8fa1d42

Please sign in to comment.