mars / create-react-app-buildpack

⚛️ Heroku Buildpack for create-react-app: static hosting for React.js web apps
MIT License
3.29k stars 651 forks source link

SOURCE_VERSION #159

Closed firec0der closed 5 years ago

firec0der commented 5 years ago

Hi there! First of all, thanks for the buildpack, it makes life easier ❤️

I tried to find a related issue but seems like there's no. So, Heroku provides env variable SOURCE_VERSION with the latest commit SHA. In my case, I need to use it during the Heroku's compile-time. I tried to inject it in a few hacky ways, but no success. Seems like, it's a quite common need. Is there any recipe about how to get that during compile-time? Is it a good idea to inject it somehow as REACT_APP_SOURCE_VERSION?

jmorrell commented 5 years ago

Hi @firec0der! IIUC the CRA build will only inline variables with the REACT_APP_ prefix, but the env var should be available to the build process.

Is it a good idea to inject it somehow as REACT_APP_SOURCE_VERSION?

While you should be careful what data you provide to the frontend, I don't see any immediate issue with providing a commit SHA. You should be able to add something like:

export REACT_APP_SOURCE_VERSION=$SOURCE_VERSION

or use the heroku-postbuild script in package.json like:

"build": "react-scripts build",
"heroku-postbuild": "REACT_APP_SOURCE_VERSION=$SOURCE_VERSION react-scripts build",
firec0der commented 5 years ago

@jmorrell thanks for the quick response, and I want to clarify. In the first case

export REACT_APP_SOURCE_VERSION=$SOURCE_VERSION

both prebuild and postbuild scripts aren't applicable, seems like they are 2 different processes and this variable won't be exported to the actual build. I tried this several time, no success.

In the second case

"build": "react-scripts build",
"heroku-postbuild": "REACT_APP_SOURCE_VERSION=$SOURCE_VERSION react-scripts build"

Won't the build be executed twice?

Seems like, the only solution is to modify the build script.

"build": "REACT_APP_SOURCE_VERSION=$SOURCE_VERSION react-scripts build"
jmorrell commented 5 years ago

Won't the build be executed twice?

As of this week that is no longer true: https://devcenter.heroku.com/changelog-items/1573

If your app defines a build script that you don’t want to execute on Heroku, you can specify a heroku-postbuild script in your package.json file. If present, this script is executed instead of the build script.

heroku-postbuild should probably be called heroku-build. We will likely silently rename it and undocument heroku-postbuild in the future, but it should do what you want here.

Seems like, the only solution is to modify the build script.

This would also work, but sets an empty ENV var when you run it locally unless you set SOURCE_VERSION yourself.

firec0der commented 5 years ago

Got it, thank you 👍

firec0der commented 5 years ago

Ouch, I've just tried to add heroku-postbuild and Heroku runs both heroku-postbuild and build scripts.

jmorrell commented 5 years ago

@firec0der that should definitely not be the case unless you are pointing to old buildpacks somehow. What's the app name?