create-capacitor-plugin icon indicating copy to clipboard operation
create-capacitor-plugin copied to clipboard

Proposal: Migrate templating engine from Mustache to EJS

Open fabiomartino opened this issue 1 month ago • 0 comments

🚀 Context

Currently, create-capacitor-plugin utilizes Mustache for templating. While Mustache is excellent for simple substitutions, its "logic-less" philosophy becomes a limitation when the scaffolding requirements grow in complexity.

❓ The Problem

As we look to add more advanced features to the generator (e.g., selecting between different Package Managers, handling complex conditional file generation for Android/iOS, or managing dynamic license text), Mustache forces us to pre-calculate heavily in TypeScript to create simple boolean flags. It lacks:

  • Native if/else logic (only existence checks).
  • String comparison (if variable == 'value').
  • Helper functions inside templates.

💡 The Solution

I propose migrating to EJS (Embedded JavaScript templating). EJS allows us to use standard JavaScript logic directly within the templates, making the codebase more maintainable and the generator significantly more powerful.

⚖️ Pros & Cons

Pros (Why EJS?):

  • Conditionals: We can handle complex logic like <% if (locals.packageManager === 'npm') { %> directly in the templates, removing the need for boolean clutter in src/template.ts.
  • Flexibility: It simplifies future additions like supporting multiple languages (Java/Kotlin) or optional features (Logging helpers) without rewriting the core extractor logic.
  • Familiarity: It uses standard JavaScript syntax, reducing the learning curve for contributors compared to learning Mustache-specific patterns.

Cons:

  • Syntax: It is slightly more verbose (<% %> vs {{ }}).
  • Discipline: Because it allows arbitrary JS execution, we must ensure we don't put business logic inside the views (though for a CLI generator, this risk is minimal).

🛠 Implementation Plan

I have already prepared a refactor that:

  1. Replaces mustache dependency with ejs.
  2. Renames all templates from .mustache to .ejs.
  3. Updates src/template.ts to use ejs.render().

Is the team open to this change? I can submit the PR immediately mustache2ejs.

fabiomartino avatar Dec 25 '25 12:12 fabiomartino