Skip to content

Commit

Permalink
feat(warn): warn defineAsyncComponent usage in routes (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
leemove committed Jan 9, 2021
1 parent 60a9015 commit 9520d66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
24 changes: 23 additions & 1 deletion __tests__/warnings.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { mockWarn } from 'jest-mock-warn'
import { createMemoryHistory, createRouter } from '../src'
import { defineComponent, FunctionalComponent, h } from 'vue'
import {
defineAsyncComponent,
defineComponent,
FunctionalComponent,
h,
} from 'vue'

let component = defineComponent({})

Expand Down Expand Up @@ -177,6 +182,23 @@ describe('warnings', () => {
expect('"/foo" is a Promise instead of a function').toHaveBeenWarned()
})

it('warns if use defineAsyncComponent in routes', async () => {
const router = createRouter({
history: createMemoryHistory(),
// simulates import('./component.vue')
routes: [
{
path: '/foo',
component: defineAsyncComponent(() => Promise.resolve({})),
},
],
})
await router.push('/foo')
expect(
`Component "default" in record with path "/foo" is defined using "defineAsyncComponent()". Write "() => import('./MyPage.vue')" instead of "defineAsyncComponent(() => import('./MyPage.vue'))"`
).toHaveBeenWarned()
})

it('warns if no route matched', async () => {
const router = createRouter({
history: createMemoryHistory(),
Expand Down
10 changes: 10 additions & 0 deletions src/navigationGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ export function extractComponentsGuards(
)
let promise = rawComponent
rawComponent = () => promise
} else if (
(rawComponent as any).__asyncLoader &&
guardType === 'beforeRouteEnter'
) {
warn(
`Component "${name}" in record with path "${record.path}" is defined ` +
`using "defineAsyncComponent()". ` +
`Write "() => import('./MyPage.vue')" instead of ` +
`"defineAsyncComponent(() => import('./MyPage.vue'))".`
)
}
}

Expand Down

0 comments on commit 9520d66

Please sign in to comment.