• Stars
    star
    193
  • Rank 201,150 (Top 4 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hot reloading for React components in electron without babel nor webpack

Electron-hot-loader

npm Build Status Build status

Hot reloading for React components in electron without babel nor webpack

This package leverages react-proxy and electron's access to the file system to enable hot reloading on React components at really high speed.

Demo of electron-hot-boilerplate

Demo: electron-hot-boilerplate

Setup

Put the following code at the top of index.js, the javascript entry point of your application in the browser. It is generally included in your index.html.

if (process.env.NODE_ENV === 'development') {
    const electronHot = require('electron-hot-loader');
    electronHot.install();
    electronHot.watchJsx(['src/**/*.jsx']);
    electronHot.watchCss(['src/assets/**/*.css']);
}

// We can now require our jsx files, they will be compiled for us
require('./index.jsx');

// In production you should not rely on the auto-transform.
// Pre-compile your react components with your build system instead.

// But, you can do this if your really want to:
// require('electron-hot-loader').install({doNotInstrument: true});

The index.jsx file is just the classic React initialization:

const React = require('react');
const ReactDOM = require('react-dom');
const App = require('./ui/App.jsx');

ReactDOM.render(<App/>, document.getElementById('root'));

electron-hot-loader will instrument all your React components and wrap them in proxies. When a file is updated on your disk, the proxies will update and a render will be forced on the root component.

This is very similar to what react-transform-hmr does but without dependencies to babel or webpack.

For your tests you can add this to your mocha config, it will compile your jsx without instrumenting them:

-u bdd
--recursive ./test/**/*.jsx
--full-trace
--reporter spec
--require electron-hot-loader/compiler

Higher order components

A higher order component is a function that takes a component and returns another, decorated, component.

Some libraries (like Redux with connect) use higher-order components. With only access to the AST, it is impossible to find out if a function will return a component or not.

So you will need to explicitly register the names of the higher order components when installing electron-hot-loader:

electronHot.install({higherOrderFunctions: ['connect']});

A demo of electron-hot-loader working with redux is available on the redux branch of electron-hot-boilerplate.

Goal

Since electron is both node and a browser, I figured we could try experimenting hot reloading without webpack in this context.

In its latest versions, node has access to a lot of ES2015 features. There seems to be little need for a babel transpilation... If you can live with the lack of es6 modules and spread operators!

In exchange, you will get a much better developer experience. Not much overhead or config and very fast reloads. Also, as soon as those features land in V8, we'll get them for free!

Principle

Installing electron-hot-loader will use require extensions (don't pay attention to the deprecation warning, it's just for development, right?) to compile JSX files as your require them in your application.

Since we have access to all the compiled components, we can use esprima to get the AST of each one.

The ReactDOM.render method has a distinctive signature that we can use to identify the root of our application.

When a user component is included in a JSX file, it is compiled to React.createElement(). We can wrap all those calls in a register() method, keep track of all the components created that way, and wrap them with react-proxy.

Then, it is just a matter of watching the file system to know which components have been updated and force a re-render on them.

The application will keep all its state and you will get unprecedented productivity.

Disclaimer

This is still a POC. I plan to use it in my current project where I was a little upset with the overhead of using webpack in electron so I will be the first to eat my own dog food.

Critiques and ideas are warmly welcomed so do not hesitate to open issues and sumit pull requests.

Roadmap

  • Write some tests
  • Write some more tests
  • Welcome feedback and ideas
  • Support source maps

Example

Have a look at electron-hot-boilerplate for a complete example.

Thanks

Dan Abramov and the other commiters for their incredible work on react-proxy.

Gurdas Nijor for his inspirational talk at ReactJS 2015 and his repo demonstrating esprima tranforms for React.

More Repositories

1

friendly-errors-webpack-plugin

Recognizes certain classes of webpack errors and cleans, aggregates and prioritizes them to provide a better Developer Experience.
JavaScript
748
star
2

boot-react

A starter application with spring boot and react
JavaScript
590
star
3

hibernate-examples

Hibernate examples from my blog http://geowarin.wordpress.com
Java
56
star
4

tarec

The Awesome REact Cli
JavaScript
54
star
5

spring-mvc-examples

Spring MVC examples from my blog - see http://geowarin.wordpress.com/category/spring-mvc-examples
JavaScript
52
star
6

docker-junit-rule

A junit rule to run docker containers
Java
49
star
7

graphql-webflux

GraphQL application using spring 5 reactive framework (webflux)
Kotlin
44
star
8

boot-rethinkdb

Chat example with spring boot and rethinkdb
Java
39
star
9

fluentd-boot

Redirect spring boot logs to elastic search via fluentd
Groovy
30
star
10

boot-social-api

Use Spring to do social authentification
Java
22
star
11

gist-templates-plugin

An intellij plugin to fetch gists from one or several github gists account and use them as templates inside IntelliJ
Java
19
star
12

springboot-jersey

A simple spring-boot & jersey application
Java
18
star
13

generator-phaser-gulp-typescript

Gulp typescript generator for PhaserJs games
JavaScript
13
star
14

electron-hot-boilerplate

Demo project for electron-hot-loader
JavaScript
12
star
15

spring-completable

Example of completable futures with Spring boot
Java
12
star
16

godot-anim-lib-export

Convert a set of mixamo animations to an animation library usable in godot 4.
Python
11
star
17

bearded-jb

TypeScript
8
star
18

mixamo-cli

Batch convert a directory containing fbx mixamo animations files to gltf
JavaScript
7
star
19

spring-spock-mvc

A thin layer over spring-test-mvc to make tests more spock-firendly
Groovy
6
star
20

spring-mvc-security

Simple example of spring security integration with spring mvc 3.2
Java
5
star
21

react-easy-form

Easy forms for react 0.14+.
JavaScript
5
star
22

groovy-tests

How to test your Java code with Groovy
Groovy
5
star
23

boot-jsr310

How to format Java 8 dates correctly with Jackson and Spring boot
Java
5
star
24

tornadofx-jpackage

Example of a tornadofx 2 project with gradle and jpackage
Kotlin
3
star
25

shasta-preview

Data fetching with Shasta and Tahoe
JavaScript
3
star
26

GodotUnitTests

C#
3
star
27

geowarin.github.io

HTML
3
star
28

spring-examples

Spring examples from my blog http://geowarin.wordpress.com/tag/spring
Java
3
star
29

jterm

A friendly java/groovy library to collect user inputs in commandline applications
Groovy
3
star
30

phaser-webpack

TypeScript
3
star
31

blog-src

JavaScript
2
star
32

custom-kapt

Custom annotation processor in kotlin with gradle
Kotlin
2
star
33

algos-in-java

Java
2
star
34

babel-plugin-glob-imports

A babel plugin which allows you to import modules from glob expressions
JavaScript
2
star
35

ts-react

Typescript, react and webpack hot reload
JavaScript
2
star
36

kollectionjs

Implementation of kotlin collections API in typescript
TypeScript
1
star
37

boot-groovy

Groovy
1
star
38

code-golf

Code golf decompiled
Java
1
star
39

sout-chuck-norris

A spring boot application that output chuck norris facts on stdout
Groovy
1
star
40

boot-react-simple

The simplest Spring-boot and React project
JavaScript
1
star
41

fusebox-datefns

JavaScript
1
star
42

JsfWebapp

Jsf 1.2, facelets, richfaces sandbox
Java
1
star
43

godot-terrain

C#
1
star
44

reactive-pacman-light

Poc for a multiplayer pacman game with rsocket
TypeScript
1
star
45

sse-servlet

Tomcat 8 embedded and servlet 3 producing server sent events
Groovy
1
star
46

jsr-revolution

Shooter game. PhaserJS, TypeScript, gulp
TypeScript
1
star
47

redux-tk

Utility functions for redux
JavaScript
1
star
48

phaser-npm-ts

TypeScript
1
star
49

boot-google-auth

OAuth with spring boot and social auth
Groovy
1
star
50

ttytube

CLI to play youtube videos from the console (no API key required)
TypeScript
1
star