Created by: tomvalorsa
This PR adds support for any package.json
keys in a template.json
file. It uses a blacklist of specific keys that shouldn’t be touched, and explicitly lists which keys will be merged with existing config - any other keys will be replaced as discussed in #8082 (closed).
Some notes:
- explicit merging of
"scripts"
and"dependencies"
kept intact -
templatePackage
keys that aren’t blacklisted or specified as merge keys will be added toappPackage
- this also replaces existing entries in
appPackage
with the new values specified intemplatePackage
- this also replaces existing entries in
- stock templates have been updated to reflect the change in
template.json
, "kitchensink" fixture also updated - docs have been updated
Some thoughts:
- some of the names used feel a bit chunky, open to suggestions!
- I chose
"browserslist"
as the example for the docs page based on #8209 but can change if there’s a clearer example (a real example felt better than"some key": "some value"
) - I added a couple of deprecation notes as "TODOs" as a I saw this convention elsewhere in
react-scripts/init.js
. These are based on the plan to deprecate the old template style here. Let me know if this should live somewhere else, or if there’s a specific format you follow - not sure how you manage changelogs but if you’re using issue titles the related issue doesn’t really represent this PR anymore so it should be changed
Test plan:
I created a custom template that was a direct copy of cra-template
, and then tested different scenarios. There are various tests split across different branches (one per branch). N.B. the changes here are just related to template.json
on each of these branches. Quick summary:
-
master
- direct copy ofcra-template
-
test-01-package-key
- added a different dependency topackage.dependencies
to ensure it was installed correctly -
test-02-scripts-key
- added scripts to add and replace topackage.scripts
-
test-03-old-deps-scripts
- added deps and scripts in the old format to ensure it still works -
test-04-old-and-new-deps-and-scripts
- added both new and old formats to ensure the new format takes precedence -
test-05-blacklist-keys
- added all of the blacklist keys to ensure they aren’t copied over -
test-06-custom-keys-to-replace-extend
- added some custom, non-blacklisted keys to ensure they are copied over -
test-07-empty-old-config
- tried with an empty config -
test-08-empty-new-config
- tried with an emptypackage
- I also ran with
cra-template-typescript
All of the above worked as expected, I’m happy to run again and provide screenshots if needed. I ran these tests by cloning my custom template and running the following command, changing branches in the custom template dir as necessary:
npx create-react-app test-app --scripts-version=file:path/to/react-scripts --template=file:path/to/custom/template
I wasn’t sure how to run other tests in the repo so I’m pushing this up now to see if CI passes.
Any feedback would be greatly appreciated :)