Vue Demi: Universal Vue Library Helper

0.14.10 · active · verified Sun Apr 19

Vue Demi (French for 'half') is a foundational utility library designed for authors building universal Vue libraries that must operate seamlessly across both Vue 2 and Vue 3 environments. It intelligently abstracts away the underlying Vue version, redirecting common Composition API imports (e.g., `ref`, `reactive`, `defineComponent`) to the appropriate Vue runtime (`vue@2` with `@vue/composition-api` or `vue@3`). The current stable version is `0.14.10`, with a consistent and active release cadence that focuses on compatibility fixes, minor feature enhancements, and addressing edge cases, often seeing several patch releases per month. Its key differentiator is providing a single, unified API surface for Composition API features and core Vue utilities, enabling developers to write code once and deploy it to both major Vue versions without needing conditional imports or complex build setups. It also exposes utilities like `isVue2` and `isVue3` for version-specific logic and a `Vue2` export for safely accessing global Vue 2 APIs (e.g., `Vue.config`, `Vue.set`).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a universal composable function using `vue-demi`'s reactivity APIs and version detection (`isVue2`, `isVue3`), suitable for libraries supporting both Vue 2 and Vue 3.

import { ref, computed, isVue2, isVue3, version as vueVersion, getCurrentInstance } from 'vue-demi';

/**
 * A universal composable that provides a reactive counter.
 * Works across Vue 2 (with Composition API) and Vue 3.
 */
export function useUniversalCounter(initialValue: number = 0) {
  const count = ref(initialValue);
  const double = computed(() => count.value * 2);

  const increment = () => {
    count.value++;
  };

  // Demonstrate version-specific logic
  if (isVue2) {
    console.log(`[Vue Demi] Running in Vue 2 (version: ${vueVersion}).`);
    // Example of accessing Vue 2 instance if needed (caution: not always universal)
    const vm = getCurrentInstance()?.proxy;
    if (vm) {
      console.log('Vue 2 VM detected:', vm.$options.name);
    }
  } else if (isVue3) {
    console.log(`[Vue Demi] Running in Vue 3 (version: ${vueVersion}).`);
    // Example of accessing Vue 3 instance (different structure)
    const instance = getCurrentInstance();
    if (instance) {
      console.log('Vue 3 instance detected:', instance.type.name);
    }
  }

  return {
    count,
    double,
    increment,
    isVue2,
    isVue3,
    vueVersion
  };
}

// To test in a Vue 3 environment:
// npm install vue@3 vue-demi
// npx vue-demi-switch 3
// Then integrate `useUniversalCounter` into a Vue 3 component.

// To test in a Vue 2 environment (requires Composition API plugin):
// npm install vue@2 @vue/composition-api vue-demi
// npx vue-demi-switch 2
// Make sure @vue/composition-api is installed for Vue 2.6.x.
// Then integrate `useUniversalCounter` into a Vue 2 component.

view raw JSON →