|
| 1 | +--- |
| 2 | +id: custom-templates |
| 3 | +title: Custom Templates |
| 4 | +--- |
| 5 | + |
| 6 | +> Note: this feature is available with `[email protected]` and higher. |
| 7 | +
|
| 8 | +Custom Templates enable you to select a template to create your project from, while still retaining all of the features of Create React App. |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +### npm |
| 13 | + |
| 14 | +```sh |
| 15 | +npm init react-app my-app --template [template-name] |
| 16 | +``` |
| 17 | + |
| 18 | +### Yarn |
| 19 | + |
| 20 | +```sh |
| 21 | +yarn create react-app my-app --template [template-name] |
| 22 | +``` |
| 23 | + |
| 24 | +## Finding custom templates |
| 25 | + |
| 26 | +We ship two templates by default: |
| 27 | + |
| 28 | +- [`cra-template`](https://github.com/facebook/create-react-app/tree/master/packages/cra-template) |
| 29 | +- [`cra-template-typescript`](https://github.com/facebook/create-react-app/tree/master/packages/cra-template-typescript) |
| 30 | + |
| 31 | +However, you can find many great community templates by searching for ["cra-template-\*"](https://www.npmjs.com/search?q=cra-template-*) on npm. |
| 32 | + |
| 33 | +## Building a template |
| 34 | + |
| 35 | +If you're interested in building a custom template, first take a look at how we've built [`cra-template`](https://github.com/facebook/create-react-app/tree/master/packages/cra-template). |
| 36 | + |
| 37 | +A template must have the following structure: |
| 38 | + |
| 39 | +``` |
| 40 | +my-app/ |
| 41 | + README.md (for npm) |
| 42 | + template.json |
| 43 | + package.json |
| 44 | + template/ |
| 45 | + README.md (for projects created from this template) |
| 46 | + gitignore |
| 47 | + public/ |
| 48 | + index.html |
| 49 | + src/ |
| 50 | + index.js (or index.tsx) |
| 51 | +``` |
| 52 | + |
| 53 | +### The `template` folder |
| 54 | + |
| 55 | +This folder is copied to the user's app directory as Create React App installs. During this process, the file `gitignore` is renamed to `.gitignore`. |
| 56 | + |
| 57 | +You can add whatever files you want in here, but you must have at least the files specified above. |
| 58 | + |
| 59 | +### The `template.json` file |
| 60 | + |
| 61 | +This is where you can define dependencies (only dependencies are supported for now), as well as any custom scripts that your template relies on. |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "dependencies": { |
| 66 | + "serve": "^11.2.0" |
| 67 | + }, |
| 68 | + "scripts": { |
| 69 | + "serve": "serve -s build", |
| 70 | + "build-and-serve": "npm run build && npm run serve" |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +For convenience, we always replace `npm run` with `yarn` in your custom `"scripts"`, as well as in your `README` when projects are initialized with yarn. |
0 commit comments