Git Product home page Git Product logo

universal-hot-reload's Introduction

universal-hot-reload

npm version npm downloads npm npm

Hot reload universally bundled webpack apps for the ultimate development experience ๐Ÿ‘

If you universally bundle your app using webpack (i.e. you use webpack to bundle your server AND client side code) this package will set up hot reloading for both server and client side.

Why use this package?

  • Automatic re-bundle on server code changes so server side rendering always reflect the latest changes.
  • Automatic re-bundle on client code changes using webpack-dev-server.
  • You can finally get rid of babel register from your server code!

This should be used in development only!

Installation

yarn add universal-hot-reload -D

Quickstart

In server bootstrap, do this:

const UniversalHotReload = require('universal-hot-reload').default;

UniversalHotReload(
  require('path/to/webpack.server.config.js'), 
  require('path/to/webpack.client.config.js'));

Advanced

This is rough guide to set up your server and client webpack configs. Follow the lines marked Important.

  1. Your server webpack config should look like this:

    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 standard webpack config like loaders, plugins, etc...
    };
  2. Your client webpack config should look like this:

    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'
      },
      module: {
        rules: [
          {
            test: /\.jsx?$/,
            loader: 'babel-loader',
            include: path.resolve('src'),
            exclude: /node_modules/,
            options: {
              cacheDirectory: true,
            }
          }]
      },
    };
  3. Your server bootstrap:

    const UniversalHotReload = require('universal-hot-reload').default;
    
    UniversalHotReload(
     require('path/to/webpack.server.config.js'), 
     require('path/to/webpack.client.config.js'));
  4. Lastly in your server entry file:

    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 = `<!DOCTYPE html>
                <html>
                  <body>
                    <div id="reactDiv">${reactString}</div>
                    <script src="${devServerBundleUrl}"></script>
                  </body>
                </html>`;
                
    // 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!

    node src/server/index.js

Example

Check the example for a fully working spa with react and react-router.

universal-hot-reload's People

Contributors

yusinto avatar alex-ferreli avatar

Watchers

James Cloos avatar esteban avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.