0.0.2 • Published 5 months ago

single-pinia v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Single Pinia

version downloads license

Write pinia at top level with export syntax. Powered by TS Macro and Unplugin.

Install

pnpm i -D single-pinia

Usage

  1. TS Macro:

    // tsm.config.ts
    import singlePinia from "single-pinia/ts-macro";
    
    export default {
     plugins: [
       singlePinia()
     ]
    };
  2. Unplugin, like Vite:

    // vite.config.ts
    import singlePinia from "single-pinia/vite";
    import { defineOptions } from "vite";
    
    export default defineOptions({
     plugins: [
       singlePinia()
     ]
    });

Then you can using the following syntax in the file that matches the stores/.*?\.(j|t)sx? pattern by default:

defineStore("counter");

export const count = ref(0);

export function increment() {
  count.value++;
}

export function decrement() {
  count.value--;
}

It will be transformed into:

export const useCounterStore = defineStore("counter", () => {
  const count = ref(0);

  function increment() {
    count.value++;
  }

  function decrement() {
    count.value--;
  }

  return {
    count,
    increment,
    decrement
  };
});

This plugin mainly performs the following transformations:

  • Infers the name of the return value from file name.

  • Uses the variables or functions exported under the defineStore statement as the setup return values for this store.