Create-react-app: Dynamically fill in "homepage" in package.json with environment varaiables

Created on 1 Nov 2016  路  2Comments  路  Source: facebook/create-react-app

Hi everyone,
I have this use case where I need to deploy my react app to both root url and a subdirectory.
I use subdirectories to deploy demo instances of the app.

For example:

http://dev.box/                          <-- this is for master branch
http://dev.box/branch-demo/branch-1      <-- CI systems will build and deploy branches to subdirectories
http://dev.box/branch-demo/branch-2

From previous discussions [1], I can use "homepage" field to adjust css and js paths in generated index.html. But for my use case, I'll need to change the "homepage" base on which location I'm deploying to.

I imagine solutions like:

HOMEPAGE='branch-demo/branch-1' npm run build

or like this in package.json (if possible):

{
  "homepage": "${HOMEPAGE}"
}

Any suggestions on how I can achieve dynamic values for homepage base on env variables?
Thanks!

Most helpful comment

Once https://github.com/facebookincubator/create-react-app/pull/937 (or similar) will land it would be possible via the PUBLIC_URL environment variable.

All 2 comments

Once https://github.com/facebookincubator/create-react-app/pull/937 (or similar) will land it would be possible via the PUBLIC_URL environment variable.

Closing in favor of #937, let's track that instead.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stopachka picture stopachka  路  3Comments

fson picture fson  路  3Comments

fson picture fson  路  3Comments

DaveLindberg picture DaveLindberg  路  3Comments

ap13p picture ap13p  路  3Comments