Coder Social home page Coder Social logo

gulp-hot-reload's Introduction

gulp-hot-reload

Gulp plugin to reload Express/React.js application without server restart.

To get started, see complete gulp-hot-reload-boilerplate.

Goals

  • hot reload React.js components using babel-plugin-react-transform and webpack-hot-middleware
  • reload Express application without process restart so React hot reloading keeps working. Any modification (adding/changing routes, changes in the entry server.js file) should be possible.
  • bundle both client and server with webpack
  • during development building takes place in-memory
  • add hot-reload capabilities to existing Express/React.js application with minimal effort

Usage

  • Plug gulp-hot-reload is used by putting it as a final step in gulp build pipe line, instead of gulp.dest call:
gulp.task('build-backend', () => {
  gulp
    .src('./src/server.js')
    .pipe(webpackStream(serverConfig, webpack, buildDone))
    .pipe(reload({
      port: 1337,
      react: true,
      config: path.join(__dirname, 'webpack.config.js')
    }))
})
  • Create webpack configuration for client and server
  • Add .babelrc file to your project

How it Works

  • the pipeline starts with running gulp.src on application entry point
  • webpack-stream is a stream-enabled webpack compiler, that can be easily plugged into gulp pipeline. It runs webpack internally, sending the file from gulp.src as an entry point
  • webpack bundles server application. It is possible with minor tweaks to webpack configuration and offers several benefits
  • application, as a single chunk, comes into gulp-hot-reload. Having whole application in a single entity makes hot reload easy
  • gulp-hot-reload starts development server if it has not started already. If the development server is configured to be started on the same port as the application server, the application server will not start and only one server instance will be running
  • gulp-hot-reload runs eval on bundles application
  • server.js module.exports the application, hence it is returned
  • gulp-hot-reload configures webpack-dev-middleware and webpack-hot-middleware on it
  • development server routes all the requests to the applciation returned from eval
  • gulp watches for files changes and runs the process if any file is modified.

Options

Options are passed to the plugin function in gulpfile:

    .pipe(reload({
      port: 1337,
      react: true,
      config: path.join(__dirname, 'webpack.config.js')
    }))
  • port - default: 1337. Port at which dev server starts. It might be the same port as used by the application - in such a case gulp-hot-reload will start development server and block the application server from starting. If the ports differ, both development and application servers start.
  • react - default: true. If true, hot module replacement of React components is enabled. Set it to false to benefit from server reload without having React application in frontend.
  • config - path to client webpack configuration. For sample see boilerplate webpack.config.js.

Inspirations and references

gulp-hot-reload's People

Contributors

mmieszek avatar gone369 avatar

Watchers

 avatar  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.