docs/react-native-windows-init.md
This document describes how the template initialization works for react-native-windows init.
The main logic that determines the template values and which files to apply is function copyProjectTemplateAndReplace in packages/@react-native-windows/cli/src/generator-windows/index.ts.
The parameters for init is located in variable argv in packages/react-native-windows-init/src/Cli.ts
The template files can be found in vnext/template
To debug the template, see Contributing to the CLI on our wiki.
PR #5126 updated the template generation for react-native-windows from regex replace to use the mustache template library.
This has the following benefits:
foreach in the template allowing for better expressivityregex.replaceThere are several templating schemes we could have chosen. We had a quick discussion and chose mustache as it is a well-adopted, lightweight library dedicated for templating. Alternatives considered:
For proper docs see the manual of mustache
But in short:
You run mustache via const text = mustache.render(inputText, obj); where obj is a regular JavaScript object.
For example:
{
name: 'you',
condition1: true,
condition2: false,
myIds: [
{ id: 'abc' },
{ id: 'xyz' }
]
}
We only use 4 mustache elements replacement patterns:
Hello {{ name }}! will result in Hello you!.Hello {{#condition1}}you!{{/condition1}} will result in Hello you!Hello {{^condition2}}you!{{/condition2}} will result in Hello you!MyIds:
{{#myIds}}
Id: {{ id }}
{{/myIds}}
MyIds
Id: abc
Id: xyz
In the future we hope this to be integrated with the react-native CLI templating scheme. At the moment there is only a very basic replacement scheme for a set of well-defined variables, so at the moment it is not suitable. When we get to that point we'll see if we can leverage that or contribute some improvements in the templating for react-native-init.