Giter VIP home page Giter VIP logo

livereactload's Introduction

LiveReactload 2.x

Live code editing with Browserify and React.

Gitter npm version Build Status

Motivation

Hot reloading is de facto in today's front-end scene but unfortunately there isn't any decent implementation for Browserify yet. This is shame because (in my opinion) Browserify is the best bundling tool at the moment.

Hence the goal of this project is to bring the hot reloading functionality to Browserify by honoring its principles: simplicity and modularity.

How it works?

LiveReactload can be used as a normal Browserify plugin. When applied to the bundle, it modifies the Browserify bundling pipeline so that the created bundle becomes "hot-reloadable".

  • LiveReactload starts the reloading server which watches the bundle changes and sends the changed contents to the browser via WebSocket.
  • When the changes arrive to the browser, LiveReactload client (included automatically in the bundle) analyzes the changes and reloads the changed modules

Starting from version 2.0.0 LiveReactload utilizes Dan Abramov's babel-plugin-react-transform and react-proxy, which means that hot-reloading capabilities are same as in Webpack.

And because one photo tells more than a thousand words, see the following video to see LiveReactload in action:

Demo

Other implementations

If you are a Webpack user, you probably want to check react-transform-boilerplate.

Usage

Pre-requirements

LiveReactload requires watchify, babelify and react >= 0.13.x in order to work.

Installation

Install pre-requirements (if not already exist)

npm i --save react
npm i --save-dev watchify babelify

Install React proxying components and LiveReactload

npm i --save-dev livereactload react-proxy babel-plugin-react-transform

Create .babelrc file into project's root directory (or add react-transform extra if the file already exists). More information about .babelrc format and options can be found from babel-plugin-react-transform.

{
  "env": {
    "development": {
      "plugins": [
        "react-transform"
      ],
      "extra": {
        "react-transform": {
          "transforms": [{
            "transform": "livereactload/babel-transform",
            "imports": ["react"]
          }]
        }
      }
    }
  }
}

And finally use LiveReactload as a Browserify plugin with watchify. For example:

node_modules/.bin/watchify site.js -t babelify -p livereactload -o static/bundle.js

That's it! Now just start (live) coding! For more detailed example, please see the basic usage example.

Reacting to reload events

Ideally your client code should be completely unaware of the reloading. However, some libraries like redux require a little hack for hot-reloading. That's why LiveReactload provides module.onReload(..) hook.

By using this hook, you can add your own custom functionality that is executed in the browser only when the module reload occurs:

if (module.onReload) {
  module.onReload(() => {
    ... do something ...
    // returning true indicates that this module was updated correctly and
    // reloading should not propagate to the parent components (if non-true
    // value is returned, then parent module gets reloaded too)
    return true
  });
}

For more details, please see the redux example.

How about build systems?

LiveReactload is build system agnostic. It means that you can use LiveReactload with all build systems having Browserify and Watchify support. Please see build systems example for more information.

When does it not work?

Well... if you hide your state inside the modules then the reloading will lose the state. For example the following code will not work:

// counter.js
const React = require('react')

let totalClicks = 0

export default React.createClass({

  getInitialState() {
    return {clickCount: totalClicks}
  },

  handleClick() {
    totalClicks += 1
    this.setState({clickCount: totalClicks})
  },


  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Increment</button>
        <div>{this.state.clickCount}</div>
      </div>
    )
  }
})

Configuration options

You can configure the LiveReactload Browserify plugin by passing some options to it (-p [ livereatload <options...> ], see Browserify docs for more information about config format).

Available options

LiveReactload supports the following configuration options

--no-server

Prevents reload server startup. If you are using LiveReactload plugin with Browserify (instead of watchify), you may want to enable this so that the process won't hang after bundling. This is not set by default.

--port <number>

Starts reload server to the given port and configures the bundle's client to connect to the server using this port. Default value is 4474

--host <hostname>

Configures the reload client to use the given hostname when connecting to the reload server. You may need this if you are running the bundle in an another device. Default value is localhost

License

MIT

Contributing

Please create a Github issue if problems occur. Pull request are also welcome and they can be created to the development branch.

livereactload's People

Contributors

alexeyraspopov avatar bassjacob avatar iamdoron avatar liolaarc avatar mattdesl avatar milankinen avatar tec27 avatar tkurki avatar weiland avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rubythonode

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.