facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.58k stars 26.8k forks source link

Support for setting default scripts-version from template.json #8335

Open jamesknelson opened 4 years ago

jamesknelson commented 4 years ago

Is your proposal related to a problem?

I've been maintaining universal-react-scripts, a version of react-scripts which includes SSR support. I've recently been trying to merge in the changes from 3.3.0, but as it stands, it would now require that instead of just asking the user to specify --scripts-version with CRA, they'll also need to specify the --template option.

The reason for this change is that universal-react-scripts was already applying some changes to the template within its init script, with the files stored next to the existing template directories. However, with 3.3.0, the template could be anything -- so it's no longer possible to apply this transformation. Instead, the user will need to specify an SSR template from the start.

Describe the solution you'd like

For something like SSR, the template and scripts version need to work together. To facilitate this, I propose that we add support for a defaultScriptsVersion property to template.json. Then, the create-react-app script should:

  1. Install and unpack the template to get access to template.json.
  2. Check for a defaultScriptsVersion, falling back to react-scripts as the default.
  3. Then install the scripts and other packages as before.

Describe alternatives you've considered

The obvious alternative is to ask people to pass compatible --scripts-version and --template options to create-react-app:

npx create-react-app --scripts-version=universal-react-scripts --template=universal

This is pretty verbose though, and will break if only one of the two options are passed. Ideally, the user could just do this:

npx create-react-app --template=universal

Another alternative would be to somehow allow the scripts version to transform the template name. This has the advantage of allowing the scripts to bail on unsupported templates -- but it sounds far more complicated.

jamesknelson commented 4 years ago

I should add that I'd be happy to put a PR together if there's agreement that this would make sense.