loreanvictor / tmplr

Automate Code Scaffolding
MIT License
26 stars 0 forks source link

Add a way to conditionally render parts of a file #27

Open tommy-mitchell opened 1 year ago

tommy-mitchell commented 1 year ago

I'm trying to create a vite template that supports conditionally adding certain plugins/dependencies. It would be nice if files could specify blocks to render based on a given variable:

tmplr.yml

- read: include_vite_sass_dts
  prompt: 'Include vite-plugin-sass-dts?'
  choices: ['Yes', 'No']

vite.config.js

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import checker from "vite-plugin-checker";
{{ #if tmplr.include_vite_sass_dts }}
    import sassDts from "vite-plugin-sass-dts";
{{ #endif }}

export default defineConfig({
    base: "/vite-template/",
    plugins: [
        react(),
        checker({
            typescript: true,
        }),
        {{ #if tmplr.include_vite_sass_dts }}
            sassDts(),
        {{ #endif }}
    ],
});
loreanvictor commented 1 year ago

this already is possible, though not particularly convenient:

# .tmplr.yml
steps:
  - if:
      prompt: Include vite-plugin-sass-dts?
      choices:
        - Yes
        - No: ''
    steps:
      - read: vite_sass_dts_import
        eval: 'import sassDts from "vite-plugin-sass-dts"'
      - read: vite_sass_dts_plugin
        eval: 'sassDts(),'
    else:
      steps:
        - read: vite_sass_dts_import
          eval: ''
        - read: vite_sass_dts_plugin
          eval: ''
  - update: vite.config.js
// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import checker from "vite-plugin-checker";
{{ tmplr.vite_sass_dts_import }}

export default defineConfig({
    base: "/vite-template/",
    plugins: [
        react(),
        checker({
            typescript: true,
        }),
                {{ tmplr.vite_sass_dts_plugin }}
    ],
});

Though it would be pretty nice to implement conditionals inside template files as well, I have a few reservations about this:

Subsequently, I would like to try to come up with a solution that makes the conditional rendering a bit more convenient, but also keep the simplicity of the template engine. I suspect an "if" pipe would do something like that, though it would require further contemplation.

# .tmplr.yml
steps:
  - read: include_vite_sass_dts
    prompt: Include vite-plugin-sass-dts?
    choices:
      - Yes
      - No: ''
  - read: vite_sass_dts_import
    eval: 'import sassDts from "vite-plugin-sass-dts";'
  - read: vite_sass_dts_plugin
    eval: 'sassDts(),'
  - update: vite.config.js
// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import checker from "vite-plugin-checker";
{{ tmplr.vite_sass_dts_import | if: include_vite_sass_dts }}

export default defineConfig({
    base: "/vite-template/",
    plugins: [
        react(),
        checker({
            typescript: true,
        }),
                {{ tmplr.vite_sass_dts_plugin | if: include_vite_sass_dts }}
    ],
});