How to make create-react-app auto build?

后端 未结 5 1907
傲寒
傲寒 2020-12-14 09:21

I have been using create react app for a while. \'npm start\' or \'yarn start\' autoreloads works fine by itself but now I have an another problem. Currently I run the app o

5条回答
  •  醉话见心
    2020-12-14 09:57

    Unfortunately this is something you will have to do yourself. You can use a tool like npm-watch to accomplish what you want though:

    Install npm-watch

    npm i --save-dev npm-watch
    

    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"
      },
      "watch": {
        "build": "src/"
      }
    }
    

    Afterwards, just use npm run watch to start up npm-watch so it can rebuild your assets on changes.

    Update:

    React-scripts now includes a proxy option that proxies requests to a different host/port. For example, if your backend is running on localhost at port 9000 under the /api route, then you would add this line to your package.json: "proxy": "localhost:9000/api". You could then make requests as you normally would in production. (source: https://create-react-app.dev/docs/proxying-api-requests-in-development)

提交回复
热议问题