Aller au contenu

Vous lisez la documentation de la version 0.6, qui n'est pas la dernière publiée.

Recettes par framework

Les extraits ci-dessous ne montrent que les sites d'appel pertinents — déposez-les dans n'importe quel projet basé sur Vite après avoir câblé mtDebug() dans vite.config.ts (voir Utilisation).

Vue 3 — Composition API (validateur de propriétés)

vue
<script setup lang="ts">
defineProps({
  variant: {
    type: String,
    required: true,
    validator(value: string) {
      const ok = ["primary", "secondary", "ghost"].includes(value);
      if (!ok) {
        $debug("warning", new Error(`Invalid variant "${value}"`), {
          tag: "PropsValidator",
          showSource: true,
        });
      }
      return ok;
    },
  },
});
</script>

React — Redux Toolkit (createSlice)

ts
import { createSlice } from "@reduxjs/toolkit";

const counterSlice = createSlice({
  name: "counter",
  initialState: { count: 0 },
  reducers: {
    increment(state) {
      state.count++;
      $debug("info", `counter incremented to ${state.count}`, {
        tag: "store/counter",
      });
    },
    reset(state) {
      $debug("warning", "counter reset", { tag: "store/counter" });
      state.count = 0;
    },
  },
});

Angular (Analog) — réducteur NgRx

ts
import { createReducer, on } from "@ngrx/store";

export const counterReducer = createReducer(
  { count: 0 },
  on(increment, (state) => {
    const next = state.count + 1;
    $debug("info", `counter incremented to ${next}`, { tag: "store/counter" });
    return { ...state, count: next };
  }),
);

Nuxt 4 — Pinia

ts
// app/stores/counter.ts
import { defineStore } from "pinia";

export const useCounterStore = defineStore("counter", {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++;
      $debug("info", `counter incremented to ${this.count}`, {
        tag: "store/counter",
      });
    },
  },
});
ts
// nuxt.config.ts
import mtDebug from "@mars-tech/vite-debug-plugin";

export default defineNuxtConfig({
  modules: ["@pinia/nuxt"],
  vite: { plugins: [mtDebug()] },
});