Skip to content

Commit fa85f03

Browse files
kevin940726mrmckeb
authored andcommitted
Support shorthand scoped templates (#8298)
1 parent c03bb36 commit fa85f03

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

docusaurus/docs/custom-templates.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Custom Templates enable you to select a template to create your project from, wh
99

1010
You'll notice that Custom Templates are always named in the format `cra-template-[template-name]`, however you only need to provide the `[template-name]` to the creation command.
1111

12+
Scoped templates are also supported, under the name `@[scope-name]/cra-template` or `@[scope-name]/cra-template-[template-name]`, which can be installed via `@[scope]` and `@[scope]/[template-name]` respectively.
13+
1214
### npm
1315

1416
```sh

packages/create-react-app/createReactApp.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,25 @@ function getTemplateInstallPackage(template, originalDirectory) {
640640
const scope = packageMatch[1] || '';
641641
const templateName = packageMatch[2];
642642

643-
const name = templateName.startsWith(templateToInstall)
644-
? templateName
645-
: `${templateToInstall}-${templateName}`;
646-
templateToInstall = `${scope}${name}`;
643+
if (
644+
templateName === templateToInstall ||
645+
templateName.startsWith(`${templateToInstall}-`)
646+
) {
647+
// Covers:
648+
// - cra-template
649+
// - @SCOPE/cra-template
650+
// - cra-template-NAME
651+
// - @SCOPE/cra-template-NAME
652+
templateToInstall = `${scope}${templateName}`;
653+
} else if (templateName.startsWith('@')) {
654+
// Covers using @SCOPE only
655+
templateToInstall = `${templateName}/${templateToInstall}`;
656+
} else {
657+
// Covers templates without the `cra-template` prefix:
658+
// - NAME
659+
// - @SCOPE/NAME
660+
templateToInstall = `${scope}${templateToInstall}-${templateName}`;
661+
}
647662
}
648663
}
649664

0 commit comments

Comments
 (0)