Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension + create-react-app live reload

Any advise on how to achieve live-reloading when implementing a Chrome Extension with create-react-app? Currently I yarn run build every time there is a change.

like image 573
João Souza Avatar asked Sep 06 '17 18:09

João Souza


People also ask

What is live reload in React?

React Hot Loader is a plugin that allows React components to be live reloaded without the loss of state. It works with Webpack and other bundlers that support both Hot Module Replacement (HMR) and Babel plugins.

Can you build a Chrome extension With React?

The extension was built with React as a view engine to render a popup. So I thought to document it to make anyone getting started on this to get along. In this blog, We will go through the process of building a Chrome extension using React. After this, you can add more functionalities to it very conveniently.


2 Answers

I managed to do that using create-react-app by:

npm i npm-watch --save-dev

Then in the package.json

{
    "name": "react-app",
    "version": "0.1.0",
    "private": false,
    "devDependencies": {
        "npm-watch": "^0.1.8",
        "react-scripts": "0.9.5",
    },
    "dependencies": {
        "react": "^15.4.2",
        "react-dom": "^15.4.2"
    },
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject",
        "watch": "npm-watch" //add this to the script
    },
    "watch": {  //add this outside the script
        "build": "src/"
    }
}
like image 111
Hugo Wesley Avatar answered Oct 17 '22 01:10

Hugo Wesley


I got this working with fswatch on Mac (had to brew install fswatch):

fswatch -o ~/$PATH_TO_YOUR_PROJECT/src | xargs -n1 -I{} npm run build

This will run npm run build anytime the src directory changes (which is what I was doing manually beforehand anyways)

Note: my manifest is pointing to the build directory for my popup.

like image 1
Mike Sallese Avatar answered Oct 17 '22 01:10

Mike Sallese