Update template gen to use mustache#5126
Conversation
@acoates: There is a different between the current generated experimentalnuget and original version. The .filters file in experimentalnuget was missing: I'm assuming that that was just a missed edit, or mixup. Please let me know if it is significant and I'll make it conditional. Refers to: vnext/local-cli/generator-windows/templates/cs/proj/MyApp.csproj:133 in 5b22f0f. [](commit_id = 5b22f0f, deletion_comment = False) |
|
|
||
| if (replacements.useMustache) { | ||
| content = mustache.render(content, replacements); | ||
| (replacements.regExpPatternsToRemove || []).forEach(regexPattern => { |
There was a problem hiding this comment.
I tried to handle the // clang-format off and // clang-format on cases with template logic, but I could not get a satisfactory line that clang-format was happy with. It would always mangle the conditional line before the off command and the closing conditional after the on command. It was pretty messy...
I therefore opted for a 1-off removal.
I considered a waterfall model as well where when mustach is on, and a tempaltes fields is set it would set the replacements to that value (else undefined) and it would run the old code. In the end I felt that that code would be a lot harder to read and understand vs the simple foreach and replace slighlty duplicated.
@dannyvv can you please add this information to a howto markdown in /docs please? |
…ne place so it just worked :)
Change files Change files
Will do. |
This PR updates the template generation for react-native-windows from regex replace to use the mustache template library.
Benefits
This has the following benefits:
Research
There are several templating scheme's we could have chosen. We had a quick discussion and most favor was for mustache as it Mustashe is a well adtoped, lightweight library dedicated for templating.
Alternatives considered:
Future
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.
Samples
If you want to run diffs before and after, please see the 'before' and 'after' folders in https://github.com/dannyvv/react-native-windows/tree/demo/MustachTemplatesWithDiff
Quick mustache tutorial:
We only use 4 mustach element: You run mustach via
const text = mustache.render(inputText, obj);where obj is a regular javascript object. For example:Hello {{ name }}!will result inHello you!.Hello {{#condition1}}you!{{/condition1}}will result inHello you!Hello {{^condition2}}you!{{/condition2}}will result inHello you!Microsoft Reviewers: Open in CodeFlow