vue-demi

<p align="center"> <img src="https://github.com/vueuse/vue-demi/blob/main/assets/banner.png?raw=true" width="600"/> <br> <a href='https://www.npmjs.com/package/vue-demi'><img src='https://img.shields.io/npm/v/vue-demi?color=42b883' alt='npm'></a> </p>

MIT 88 个版本
安装
npm install vue-demi
yarn add vue-demi
pnpm add vue-demi
bun add vue-demi
README


npm

Vue Demi (half in French) is a developing utility
allows you to write Universal Vue Libraries for Vue 2 & 3
See more details in this blog post



Strategies

  • <=2.6: exports from vue + @vue/composition-api with plugin auto installing.
  • 2.7: exports from vue (Composition API is built-in in Vue 2.7).
  • >=3.0: exports from vue, with polyfill of Vue 2's set and del API.

Usage

Install this as your plugin's dependency:

npm i vue-demi
# or
yarn add vue-demi
# or 
pnpm i vue-demi

Add vue and @vue/composition-api to your plugin's peer dependencies to specify what versions you support.

{
  "dependencies": {
    "vue-demi": "latest"
  },
  "peerDependencies": {
    "@vue/composition-api": "^1.0.0-rc.1",
    "vue": "^2.0.0 || >=3.0.0"
  },
  "peerDependenciesMeta": {
    "@vue/composition-api": {
      "optional": true
    }
  },
  "devDependencies": {
    "vue": "^3.0.0" // or "^2.6.0" base on your preferred working environment
  },
}

Import everything related to Vue from it, it will redirect to vue@2 + @vue/composition-api or vue@3 based on users' environments.

import { ref, reactive, defineComponent } from 'vue-demi'

Publish your plugin and all is done!

When using with Vite, you will need to opt-out the pre-bundling to get vue-demi work properly by

// vite.config.js
export default defineConfig({
  optimizeDeps: {
    exclude: ['vue-demi']
 }
})

Extra APIs

Vue Demi provides extra APIs to help distinguish users' environments and to do some version-specific logic.

isVue2 isVue3

import { isVue2, isVue3 } from 'vue-demi'

if (isVue2) {
  // Vue 2 only
} else {
  // Vue 3 only
}

Vue2

To avoid bringing in all the tree-shakable modules, we provide a Vue2 export to support access to Vue 2's global API. (See #41.)

import { Vue2 } from 'vue-demi'

if (Vue2) {
  Vue2.config.ignoredElements.push('x-foo')
}

install()

Composition API in Vue 2 is provided as a plugin and needs to be installed on the Vue instance before using. Normally, vue-demi will try to install it automatically. For some usages where you might need to ensure the plugin gets installed correctly, the install() API is exposed to as a safe version of Vue.use(CompositionAPI). install() in the Vue 3 environment will be an empty function (no-op).

import { install } from 'vue-demi'

install()

CLI

Manually Switch Versions

To explicitly switch the redirecting version, you can use these commands in your project's root.

npx vue-demi-switch 2
# or
npx vue-demi-switch 3

Package Aliasing

If you would like to import vue under an alias, you can use the following command

npx vue-demi-switch 2 vue2
# or
npx vue-demi-switch 3 vue3

Then vue-demi will redirect APIs from the alias name you specified, for example:

import * as Vue from 'vue3'

var isVue2 = false
var isVue3 = true
var Vue2 = undefined

export * from 'vue3'
export {
  Vue,
  Vue2,
  isVue2,
  isVue3,
}

Auto Fix

If the postinstall hook doesn't get triggered or you have updated the Vue version, try to run the following command to resolve the redirecting.

npx vue-demi-fix

Isomorphic Testings

You can support testing for both versions by adding npm alias in your dev dependencies. For example:

{
  "scripts": {
    "test:2": "vue-demi-switch 2 vue2 && jest",
    "test:3": "vue-demi-switch 3 && jest",
  },
  "devDependencies": {
    "vue": "^3.0.0",
    "vue2": "npm:vue@2"
  },
}

or

{
  "scripts": {
    "test:2": "vue-demi-switch 2 && jest",
    "test:3": "vue-demi-switch 3 vue3 && jest",
  },
  "devDependencies": {
    "vue": "^2.6.0",
    "vue3": "npm:vue@3"
  },
}

Examples

See examples.

Who is using this?

open a PR to add your library ;)

Underhood

See the blog post.

License

MIT License © 2020 Anthony Fu

版本列表
0.14.10 2024-07-25
0.14.9 2024-07-23
0.14.8 2024-05-30
0.14.7 2024-02-01
0.14.6 2023-08-28
0.14.5 2023-05-18
0.14.4 2023-05-17
0.14.3 2023-05-17
0.14.2 2023-05-17
0.14.1 2023-05-09
0.14.0 2023-04-13
0.13.11 2022-08-25
0.13.10 2022-08-24
0.13.9 2022-08-23
0.13.8 2022-08-16
0.13.7 2022-08-10
0.13.6 2022-07-26
0.13.5 2022-07-16
0.13.4 2022-07-12
0.13.3 2022-07-12
0.13.2 2022-07-05
0.13.1 2022-06-01
0.13.0 2022-05-31
0.12.5 2022-03-30
0.12.4 2022-03-16
0.12.3 2022-03-15
0.12.2 2022-03-15
0.12.1 2021-11-03
0.12.0 2021-10-27
0.11.4 2021-09-05
0.11.3 2021-08-14
0.11.2 2021-07-15
0.11.1 2021-07-15
0.11.0-beta.1 2021-07-15
0.11.0 2021-07-15
0.10.1 2021-07-09
0.10.0 2021-07-05
0.9.1 2021-05-27
0.9.0 2021-05-06
0.8.1 2021-05-05
0.8.0 2021-05-05
0.7.5 2021-04-18
0.7.4 2021-03-22
0.7.3 2021-03-13
0.7.2 2021-03-12
0.7.1 2021-03-01
0.7.0 2021-02-27
0.6.2 2021-02-26
0.6.1 2021-02-25
0.6.0-alpha.1 2021-01-08
0.6.0 2021-01-08
0.5.4 2021-01-05
0.5.3 2020-12-17
0.5.2 2020-12-17
0.5.1 2020-12-17
0.5.0 2020-12-16
0.4.5 2020-11-20
0.4.4 2020-11-19
0.4.3 2020-11-09
0.4.2 2020-11-09
0.4.1 2020-10-23
0.4.0 2020-10-02
0.3.3 2020-08-26
0.3.2 2020-08-24
0.3.1 2020-08-23
0.3.0 2020-08-23
0.2.0-alpha.2 2020-08-04
0.2.0-alpha.1 2020-08-04
0.2.0 2020-08-04
0.1.11 2020-08-04
0.1.10 2020-08-04
0.1.9 2020-08-04
0.1.8 2020-08-04
0.1.7 2020-08-04
0.1.6 2020-08-04
0.1.5 2020-08-04
0.1.4 2020-07-04
0.1.3 2020-07-03
0.1.2 2020-07-02
0.1.1 2020-07-01
0.1.0 2020-07-01
0.0.7 2020-07-01
0.0.6 2020-07-01
0.0.5 2020-07-01
0.0.4 2020-07-01
0.0.3 2020-07-01
0.0.2 2020-07-01
0.0.1 2020-07-01