Skip to content

Commit 248f757

Browse files
committed
feat: vue 3 support via vue-demi
1 parent 887cccc commit 248f757

File tree

11 files changed

+292
-16
lines changed

11 files changed

+292
-16
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
"preinstall": "npx only-allow pnpm",
66
"lint": "eslint .",
77
"lint:fix": "npm run lint -- --fix",
8+
"switch-vue2": "cd packages/core && npm run switch-vue2",
9+
"switch-vue3": "cd packages/core && npm run switch-vue3",
810
"test:core": "cd packages/core && npm run test",
9-
"test:vue2": "cd packages/tests-vue2 && npm run test",
10-
"test:all": "npm run test:core && npm run test:vue2",
11+
"test:vue2": "npm run switch-vue2 && cd packages/tests-vue2 && npm run test",
12+
"test:vue3": "npm run switch-vue3 && cd packages/tests-vue3 && npm run test",
13+
"test:all": "npm run test:core && npm run test:vue3 && npm run test:vue2",
1114
"build": "cd packages/core && npm run build",
1215
"prerelease": "npm run lint && npm run test:all && npm run build",
1316
"release": "standard-version --preset @djaler/standard",

packages/core/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
> A tiny Vue composable function to create a ref synced with vue router query.
77
88
## Install
9+
> Works for Vue 2 and Vue 3 **within a single package** by the power of [vue-demi](https://github.com/vueuse/vue-demi)!
910
1011
```sh
1112
npm install --save vue-use-route-query
@@ -28,7 +29,7 @@ pnpm install vue-use-route-query
2829
Simple example with a string parameter without any transformations
2930

3031
```ts
31-
import { defineComponent } from '@vue/composition-api'
32+
import { defineComponent } from 'vue'
3233
import { useRouteQuery } from 'vue-use-route-query'
3334

3435
export default defineComponent({
@@ -49,7 +50,7 @@ export default defineComponent({
4950
More complex example with a transformer
5051

5152
```ts
52-
import { defineComponent } from '@vue/composition-api'
53+
import { defineComponent } from 'vue'
5354
import { useRouteQuery, RouteQueryTransformer } from 'vue-use-route-query'
5455

5556
export default defineComponent({

packages/core/package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"vue-composition-api"
1717
],
1818
"scripts": {
19+
"switch-vue2": "vue-demi-switch 2",
20+
"switch-vue3": "vue-demi-switch 3 vue3",
1921
"test": "vitest run",
2022
"check-es6": "es-check es2017 dist/index.js",
2123
"check-treeshake": "agadoo dist/index.mjs",
@@ -31,12 +33,18 @@
3133
"types": "dist/index.d.ts",
3234
"sideEffects": false,
3335
"dependencies": {
34-
"async-queue-chain": "^1.0.1"
36+
"async-queue-chain": "^1.0.1",
37+
"vue-demi": "^0.12.5"
3538
},
3639
"peerDependencies": {
3740
"@vue/composition-api": "^1.1.0",
38-
"vue": "^2.6.10",
39-
"vue-router": "^3.0.0"
41+
"vue": "^2.6.10 || ^3.2.0",
42+
"vue-router": "^3.0.0 || ^4.0.0"
43+
},
44+
"peerDependenciesMeta": {
45+
"@vue/composition-api": {
46+
"optional": true
47+
}
4048
},
4149
"devDependencies": {
4250
"@vue/composition-api": "1.4.9",
@@ -47,6 +55,7 @@
4755
"vite-plugin-dts": "1.0.5",
4856
"vitest": "0.9.3",
4957
"vue": "2.6.14",
58+
"vue3": "npm:vue@3",
5059
"vue-router": "3.1.3"
5160
},
5261
"clean-publish": {

packages/core/src/helpers.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { computed, ComputedRef, getCurrentInstance } from '@vue/composition-api';
2-
import type VueRouter from 'vue-router';
3-
import type { Route } from 'vue-router';
1+
import { computed, getCurrentInstance } from 'vue-demi';
42

5-
export function useRouter(): VueRouter {
3+
export function useRouter() {
64
const vm = getCurrentInstance();
75

86
if (!vm) {
@@ -12,7 +10,7 @@ export function useRouter(): VueRouter {
1210
return vm.proxy.$router;
1311
}
1412

15-
export function useRoute(): ComputedRef<Route> {
13+
export function useRoute() {
1614
const vm = getCurrentInstance();
1715

1816
if (!vm) {

packages/core/src/useRouteQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, Ref } from '@vue/composition-api';
1+
import { computed, Ref } from 'vue-demi';
22

33
import { useRoute, useRouter } from './helpers';
44
import { queueQueryUpdate } from './queue-query-update';

packages/tests-vue2/index.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import VueCompositionApi from '@vue/composition-api';
21
import { testUseRouteQuery } from 'tests-base';
32
import { beforeEach, it } from 'vitest';
43
import Vue from 'vue';
@@ -7,7 +6,6 @@ import VueRouter from 'vue-router';
76
import { mountComposition } from './vue-composition-test-utils';
87

98
Vue.use(VueRouter);
10-
Vue.use(VueCompositionApi);
119

1210
let router: VueRouter;
1311

packages/tests-vue3/index.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { testUseRouteQuery } from 'tests-base';
2+
import { beforeEach, it } from 'vitest';
3+
import { createRouter, createWebHistory, Router } from 'vue-router';
4+
5+
import { mountComposition } from './vue-composition-test-utils';
6+
7+
let router: Router;
8+
9+
beforeEach(() => {
10+
router = createRouter({
11+
history: createWebHistory(),
12+
routes: [
13+
{ path: '/', component: { template: '<div/>' } },
14+
],
15+
});
16+
});
17+
18+
testUseRouteQuery({
19+
replaceQuery: async (query) => {
20+
await router.replace({ query });
21+
},
22+
getCurrentQuery: () => router.currentRoute.value.query,
23+
mountComposition: callback => mountComposition(callback, {
24+
global: {
25+
plugins: [router],
26+
},
27+
}),
28+
it,
29+
});

packages/tests-vue3/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "tests-vue3",
3+
"private": true,
4+
"scripts": {
5+
"test": "vitest run"
6+
},
7+
"devDependencies": {
8+
"@vue/test-utils": "2.0.0-rc.21",
9+
"jsdom": "19.0.0",
10+
"tests-base": "workspace:*",
11+
"vitest": "0.9.3",
12+
"vue": "3.2.33",
13+
"vue-router": "4.0.14"
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config';
2+
3+
export default defineConfig({
4+
test: {
5+
environment: 'jsdom',
6+
},
7+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* https://github.com/ariesjia/vue-composition-test-utils with some changes
3+
* TODO: fork
4+
*/
5+
import { shallowMount, VueWrapper } from '@vue/test-utils';
6+
import { h } from 'vue';
7+
8+
type MountingOptions = Parameters<typeof shallowMount>[1] & {
9+
component?: any;
10+
};
11+
12+
export interface MountResult<R> extends VueWrapper {
13+
result: R;
14+
error: unknown;
15+
}
16+
17+
export function mountComposition<R>(callback: () => R, options: MountingOptions = {}): MountResult<R> {
18+
let result: R;
19+
let error: unknown;
20+
const { component = {}, ...other } = options;
21+
const Wrap = {
22+
render: () => h('div'),
23+
...component,
24+
setup() {
25+
try {
26+
result = callback();
27+
} catch (e) {
28+
error = e;
29+
}
30+
return {
31+
result,
32+
error,
33+
};
34+
},
35+
};
36+
37+
const vueWrapper = shallowMount(Wrap, other);
38+
39+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
40+
// @ts-ignore
41+
return Object.assign(vueWrapper, { result, error });
42+
}

0 commit comments

Comments
 (0)