Skip to content

Commit

Permalink
Merge pull request #23 from blainekasten/support-whm
Browse files Browse the repository at this point in the history
Support webpack-hot-middleware
  • Loading branch information
pmmmwh committed Feb 27, 2020
2 parents a220083 + 9a7fe78 commit b240742
Show file tree
Hide file tree
Showing 21 changed files with 5,148 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ The allowed values are as follows:
| :-----------------------: | :-------: | :-----: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **`disableRefreshCheck`** | `boolean` | `false` | Disables detection of react-refresh's Babel plugin. Useful if you do not parse JS files within `node_modules`, or if you have a Babel setup not entirely controlled by Webpack. |
| **`forceEnable`** | `boolean` | `false` | Enables the plugin forcefully. Useful if you want to use the plugin in production, or if you are using Webpack's `none` mode without `NODE_ENV`, for example. |
| **`useLegacyWDSSockets`** | `boolean` | `false` | Set this to true if you are using a webpack-dev-server version prior to 3.8 as it requires a custom SocketJS implementation. |

## Related Work

Expand Down
2 changes: 1 addition & 1 deletion examples/wds-kitchen-sink/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React App</title>
<title>WDS React App</title>
</head>
<body>
<div id="app"></div>
Expand Down
9 changes: 9 additions & 0 deletions examples/whm-kitchen-sink/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = api => {
// This caches the Babel config
api.cache.using(() => process.env.NODE_ENV);
return {
presets: ['@babel/preset-env', '@babel/preset-react'],
// Applies the react-refresh Babel plugin on non-production modes only
...(!api.env('production') && { plugins: ['react-refresh/babel'] }),
};
};
29 changes: 29 additions & 0 deletions examples/whm-kitchen-sink/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "whm-kitchen-sink",
"version": "0.1.0",
"main": "index.js",
"author": "Michael Mok",
"license": "MIT",
"scripts": {
"start": "node ./server",
"build": "cross-env NODE_ENV=production webpack --mode production"
},
"dependencies": {
"react": "^16.9.0",
"react-dom": "^16.9.0"
},
"devDependencies": {
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6",
"cross-env": "^6.0.3",
"express": "^4.17.1",
"html-webpack-plugin": "^3.2.0",
"react-refresh": "^0.7.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.1.1",
"webpack": "^4.41.2",
"webpack-dev-middleware": "^3.7.2",
"webpack-hot-middleware": "^2.25.0"
}
}
11 changes: 11 additions & 0 deletions examples/whm-kitchen-sink/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WHM React App</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
23 changes: 23 additions & 0 deletions examples/whm-kitchen-sink/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const express = require('express');
const webpack = require('webpack');
const path = require('path');
const config = require('./webpack.config.js');
const compiler = webpack(config);

const app = express();

app.use(
require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
})
);

app.use(
require(`webpack-hot-middleware`)(compiler, {
log: false,
path: `/__webpack_hmr`,
heartbeat: 10 * 1000,
})
);

app.listen(3000, () => console.log('App is listening on port 3000!'));
23 changes: 23 additions & 0 deletions examples/whm-kitchen-sink/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';
import ClassDefault from './ClassDefault';
import { ClassNamed } from './ClassNamed';
import FunctionDefault from './FunctionDefault';
import { FunctionNamed } from './FunctionNamed';

const LazyComponent = React.lazy(() => import('./LazyComponent'));

function App() {
return (
<div>
<ClassDefault />
<ClassNamed />
<FunctionDefault />
<FunctionNamed />
<React.Suspense fallback={<h1>Loading</h1>}>
<LazyComponent />
</React.Suspense>
</div>
);
}

export default App;
9 changes: 9 additions & 0 deletions examples/whm-kitchen-sink/src/ClassDefault.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react';

class ClassDefault extends React.Component {
render() {
return <h1>Default Export Class</h1>;
}
}

export default ClassDefault;
7 changes: 7 additions & 0 deletions examples/whm-kitchen-sink/src/ClassNamed.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

export class ClassNamed extends React.Component {
render() {
return <h1>Named Export Class</h1>;
}
}
7 changes: 7 additions & 0 deletions examples/whm-kitchen-sink/src/FunctionDefault.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

function FunctionDefault() {
return <h1>Default Export Function</h1>;
}

export default FunctionDefault;
5 changes: 5 additions & 0 deletions examples/whm-kitchen-sink/src/FunctionNamed.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from 'react';

export function FunctionNamed() {
return <h1>Named Export Function</h1>;
}
7 changes: 7 additions & 0 deletions examples/whm-kitchen-sink/src/LazyComponent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

function LazyComponent() {
return <h1>Lazy Component</h1>;
}

export default LazyComponent;
5 changes: 5 additions & 0 deletions examples/whm-kitchen-sink/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from 'react';
import { render } from 'react-dom';
import App from './App';

render(<App />, document.getElementById('app'));
38 changes: 38 additions & 0 deletions examples/whm-kitchen-sink/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const ReactRefreshPlugin = require('../../src');
const path = require('path');

module.exports = {
entry: {
main: ['webpack-hot-middleware/client', './src/index.js'],
},
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js',
},
mode: 'development',
target: 'web',
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
],
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new ReactRefreshPlugin(),
new HtmlWebpackPlugin({
template: './public/index.html',
filename: './index.html',
}),
new webpack.NoEmitOnErrorsPlugin(),
],
resolve: {
extensions: ['.js', '.jsx'],
},
};
Loading

0 comments on commit b240742

Please sign in to comment.