Skip to content

Commit

Permalink
feat: expose auto imports array
Browse files Browse the repository at this point in the history
Close #24
  • Loading branch information
posva committed Jul 6, 2022
1 parent 03bba1a commit 3e71a23
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 12 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ createRouter({
})
```

If you are using [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import), make sure to remove the `vue-router` preset and use the one exported by `unplugin-vue-router`:

```diff
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
+import { VueRouterExports } from 'unplugin-vue-router'

export default defineConfig({
plugins: [
// other plugins
AutoImport({
imports: [
- 'vue-router',
+ { '@vue-router': VueRouterExports },
],
}),
],
})
```

Make sure to also check and follow [the TypeScript section](#typescript) below **if you are using TypeScript or have a `jsconfig.json` file**.

## Configuration
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"ts-expect": "^1.3.0",
"tsup": "^6.1.3",
"typescript": "^4.7.4",
"unplugin-auto-import": "^0.9.2",
"vite": "^2.9.13",
"vitest": "^0.17.0",
"vue": "^3.2.37",
Expand Down
6 changes: 6 additions & 0 deletions playground/auto-imports.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Generated by 'unplugin-auto-import'
export {}
declare global {
const useRoute: typeof import('@vue-router')['useRoute']
const useRouter: typeof import('@vue-router')['useRouter']
}
7 changes: 1 addition & 6 deletions playground/src/routes/[name].vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<script lang="ts" setup>
import {
useRoute,
useRouter,
onBeforeRouteLeave,
onBeforeRouteUpdate,
} from '@vue-router'
import { onBeforeRouteLeave, onBeforeRouteUpdate } from '@vue-router'
import type { RouterTyped, RouteRecordRaw } from '@vue-router'
// const $route = useRoute()
Expand Down
8 changes: 7 additions & 1 deletion playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"extends": "@vue/tsconfig/tsconfig.web.json",
"include": ["env.d.ts", "src/**/*", "src/**/*.vue", "typed-router.d.ts"],
"include": [
"env.d.ts",
"src/**/*",
"src/**/*.vue",
"typed-router.d.ts",
"./auto-imports.d.ts"
],
"compilerOptions": {
"baseUrl": ".",
"paths": {
Expand Down
14 changes: 13 additions & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { defineConfig } from 'vite'
import Inspect from 'vite-plugin-inspect'
// @ts-ignore: the plugin should not be checked in the playground
import VueRouter from '../src/vite'
import { getFileBasedRouteName, getPascalCaseRouteName } from '../src'
import {
getFileBasedRouteName,
getPascalCaseRouteName,
VueRouterExports,
} from '../src'
import Vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'

export default defineConfig({
clearScreen: false,
Expand All @@ -21,6 +26,13 @@ export default defineConfig({
// './src/routes/**/*.spec.ts',
],
}),
AutoImport({
imports: [
{
'@vue-router': VueRouterExports,
},
],
}),
Inspect(),
],
resolve: {
Expand Down
89 changes: 85 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ export { TreeLeaf, createPrefixTree } from './core/tree'
// TreeLeafValueParam,
// TreeLeafValueStatic,
// } from './core/treeLeafValue'

export const VueRouterExports = ['useRoute', 'useRouter']

0 comments on commit 3e71a23

Please sign in to comment.