Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support templates in scoped packages #7991

Merged
merged 4 commits into from
Nov 18, 2019

Conversation

klasbj
Copy link
Contributor

@klasbj klasbj commented Nov 17, 2019

Fixing issue #7985.

I learned a bit about some... interesting... NPM behavior while testing. With only a regex check that the package name started with cra-template-, it still fell back to cra-template-@scope/package. If scope/package happened to be an accessible Github project, npm would download and install some version (I'm not quite sure how it decided which version...) of that repository as the template.

The solution I came up with was adding the prefix to the package name, while leaving the scope part unchanged. Maybe not 100% intuitive, but consistent with the behaviour of non-scoped packages.

Test cases

Plain, uses cra-template

CRA_INTERNAL_TEST=1 yarn create-react-app my-app1
# [...]
# Creating a new React app in /home/klasse/projects/create-react-app/my-app1.
#
# Installing packages. This might take a couple of minutes.
# Installing react, react-dom, and react-scripts with cra-template...
# [...]

Template without prefix (typescript), uses cra-template-typescript

CRA_INTERNAL_TEST=1 yarn create-react-app --template typescript my-app2
# [...]
# Creating a new React app in /home/klasse/projects/create-react-app/my-app2.
#
# Installing packages. This might take a couple of minutes.
# Installing react, react-dom, and react-scripts with cra-template-typescript...
# [...]

Template with prefix (cra-template-typescript)

CRA_INTERNAL_TEST=1 yarn create-react-app --template cra-template-typescript my-app3
# [...]
# Creating a new React app in /home/klasse/projects/create-react-app/my-app3.
#
# Installing packages. This might take a couple of minutes.
# Installing react, react-dom, and react-scripts with cra-template-typescript...
# [...]

Template in scoped npm package with prefix (@klasbj/cra-template-scoped)

CRA_INTERNAL_TEST=1 yarn create-react-app --template "@klasbj/cra-template-scoped" my-app4
# [...]
# Creating a new React app in /home/klasse/projects/create-react-app/my-app4.
#
# Installing packages. This might take a couple of minutes.
# Installing react, react-dom, and react-scripts with @klasbj/cra-template-scoped...
# [...]

Template in scoped npm package without prefix (@klasbj/scoped), uses @klasbj/cra-template-scoped

CRA_INTERNAL_TEST=1 yarn create-react-app --template "@klasbj/scoped" my-app5
# [...]
# Creating a new React app in /home/klasse/projects/create-react-app/my-app5.
#
# Installing packages. This might take a couple of minutes.
# Installing react, react-dom, and react-scripts with @klasbj/cra-template-scoped...
# [...]

Scoped template with matching Github project, attempts to use @klasbj/cra-template-scoped-cra-template, and not my Github project klasbj/scoped-cra-template

CRA_INTERNAL_TEST=1 yarn create-react-app --template "@klasbj/scoped-cra-template" my-app6
# [...]
# Creating a new React app in /home/klasse/projects/create-react-app/my-app6.
#
# Installing packages. This might take a couple of minutes.
# Installing react, react-dom, and react-scripts with @klasbj/cra-template-scoped-cra-template...
#
# [1/4] Resolving packages...
# error An unexpected error occurred: "https://registry.yarnpkg.com/@klasbj%2fcra-template-scoped-cra-template: Not found".
# info If you think this is a bug, please open a bug report with the information provided in "/home/klasse/projects/create-react-app/my-app6/yarn-error.log".
# info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
#
# Aborting installation.
#   yarnpkg add --exact react react-dom /home/klasse/projects/create-react-app/packages/react-scripts/react-scripts-3.2.0.tgz @klasbj/cra-template-scoped-cra-template --cwd /home/klasse/projects/create-react-app/my-app6 has failed.
#
# Deleting generated file... package.json
# Deleting generated file... yarn.lock
# Done.
# [...]

@facebook-github-bot
Copy link

Hi klasbj! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file.In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@mrmckeb
Copy link
Contributor

mrmckeb commented Nov 18, 2019

Thanks @klasbj, this looks great.

@mrmckeb mrmckeb merged commit e7cdde6 into facebook:master Nov 18, 2019
@mrmckeb
Copy link
Contributor

mrmckeb commented Nov 18, 2019

Thanks again!

@klasbj klasbj deleted the templates-in-scoped-packages branch November 18, 2019 08:21
@lock lock bot locked and limited conversation to collaborators Nov 23, 2019
@ianschmitz ianschmitz added this to the 3.3 milestone Nov 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants