Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yusinto/universal-hot-reload
Hot reload client and server webpack bundles for the ultimate development experience
https://github.com/yusinto/universal-hot-reload
bundle hot hot-module-replacement hot-reload hotload hotloader hotloading live live-reload livereload reload reloading server server-side-bundle universal universal-hot-reload universal-web universal-webpack webpack webpack-dev-server
Last synced: 10 days ago
JSON representation
Hot reload client and server webpack bundles for the ultimate development experience
- Host: GitHub
- URL: https://github.com/yusinto/universal-hot-reload
- Owner: yusinto
- License: mit
- Created: 2016-10-21T09:22:54.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:03:23.000Z (almost 2 years ago)
- Last Synced: 2024-10-22T22:36:17.952Z (14 days ago)
- Topics: bundle, hot, hot-module-replacement, hot-reload, hotload, hotloader, hotloading, live, live-reload, livereload, reload, reloading, server, server-side-bundle, universal, universal-hot-reload, universal-web, universal-webpack, webpack, webpack-dev-server
- Language: JavaScript
- Size: 4.17 MB
- Stars: 79
- Watchers: 3
- Forks: 10
- Open Issues: 67
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# universal-hot-reload
[![Circle CI](https://img.shields.io/circleci/project/github/yusinto/universal-hot-reload/master.svg?style=for-the-badge&logo=circleci)](https://circleci.com/gh/yusinto/universal-hot-reload)
[![npm version](https://img.shields.io/npm/v/universal-hot-reload.svg?style=for-the-badge)](https://www.npmjs.com/package/universal-hot-reload)
[![npm downloads](https://img.shields.io/npm/dm/universal-hot-reload.svg?style=for-the-badge)](https://www.npmjs.com/package/universal-hot-reload)
[![npm](https://img.shields.io/npm/dt/universal-hot-reload.svg?style=for-the-badge)](https://www.npmjs.com/package/universal-hot-reload)
[![npm](https://img.shields.io/npm/l/universal-hot-reload.svg?style=for-the-badge)](https://www.npmjs.com/package/universal-hot-reload)> **Easily hot reload your server, client or universal apps** :clap:
Why this package?
* Setup hot reload for your app in four lines of code or less.
* Supports server, client and universal hot reloads!
* Works with react, typescript, graphql and nexus.This should be used in development only!
## Installation
yarn add universal-hot-reload -D
## Quickstart: server only
To hot reload graphql servers and express servers without ssr,
create index.js and server.js like below. For graphql, only `express-graphql`
and `apollo-server` are supported for now.#### index.js
```js
const { serverHotReload } = require('universal-hot-reload');// server.js is where you export your http.server instance (see below)
serverHotReload(require.resolve('./server.js'));
```#### server.js
```js
// You'll need to export default an instance of http.server so universal-hot-reload
// can restart your server when changes are detected.// For express or express-graphql
export default app.listen(PORT, () => console.log(`Listening at ${PORT}`));// For apollo-server
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(() => console.log(`Listening at ${PORT}`));
export default server.httpServer;
```Run your app:
```bash
node index.js
```### Quickstart: universal apps
To hot reload a universal app, create index.js like below and follow the same
steps as [Quickstart: server only](#quickstart-server-only).
#### index.js```js
const UniversalHotReload = require('universal-hot-reload').default;// supply your own webpack configs
const serverConfig = require('../webpack.config.server.js');
const clientConfig = require('../webpack.config.client.js');// the configs are optional, you can supply either one or both.
// if you omit say the server config, then your server won't hot reload.
UniversalHotReload({ serverConfig, clientConfig });
```## Advanced
If you use the `serverHotReload` function then you won't need to supply your own server webpack config. `universal-hot-reload`
uses a default server webpack config so you don't have to supply your own.If you want to use your own custom server webpack config or if you want to hot reload your universal app,
then you'll need to supply your own webpack configs. Follow the lines marked `Important`.1. Your server webpack config should look like this:
```js
const path = require('path');
const nodeExternals = require('webpack-node-externals');
module.exports = {
mode: 'development',
devtool: 'source-map',
entry: './src/server/server.js',
target: 'node', // Important
externals: [nodeExternals()], // Important
output: {
path: path.resolve('dist'),
filename: 'serverBundle.js',
libraryTarget: 'commonjs2' // Important
},
// other webpack config
};
```
2. Your client webpack config should look like this:```javascript
const path = require('path');
const webpackServeUrl = 'http://localhost:3002';
module.exports = {
mode: 'development',
devtool: 'source-map',
entry: './src/client/index',
output: {
path: path.resolve('dist'),
publicPath: `${webpackServeUrl}/dist/`, // Important: must be full path with trailing slash
filename: 'bundle.js'
},
// other webpack config
};
```
3. Create an `index.js` file:```javascript
const UniversalHotReload = require('universal-hot-reload').default;// You can provide only a server or a client config or both.
const serverConfig = require('../webpack.config.server.js');
const clientConfig = require('../webpack.config.client.js');
// Omitting a config means that side of your app won't hot reload.
UniversalHotReload({ serverConfig, clientConfig });
```4. Lastly in your server entry file:
```javascript
import { getDevServerBundleUrl } from 'universal-hot-reload';
import webpackClientConfig from 'path/to/webpack.config.client';// Important: gets webpack-dev-server url from your client config
const devServerBundleUrl = getDevServerBundleUrl(webpackClientConfig);
// Important: feed the url into script src
const html = `
${reactString}
`;
// Important: the listen method returns a http.server object which must be default exported
// so universal-hot-reload can access it
export default app.listen(PORT, () => {
log.info(`Listening at ${PORT}`);
});
```5. Run your app!
```javascript
node index.js
```## Examples
For graphql, check this [example](https://github.com/yusinto/universal-hot-reload/tree/master/examples/ts) with nexus,
apollo server and typescript. Only `express-graphql` and `apollo-server` are supported right now. `graphql-yoga`
does not expose its `http.server` instance and so it's not hot-reloadable this way for now.For universal webpack apps, check the react [example](https://github.com/yusinto/universal-hot-reload/tree/master/examples/js)
for a fully working spa with react and react-router.