From: Eduardo San Martin Morote Date: Fri, 24 Apr 2020 16:22:15 +0000 (+0200) Subject: refactor: flatten src folder X-Git-Tag: v4.0.0-alpha.8~36 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3251cfd43c46dd0e42a99980eec9758032a52474;p=thirdparty%2Fvuejs%2Frouter.git refactor: flatten src folder --- diff --git a/.github/contributing.md b/.github/contributing.md index b213e748..c2ed3d7d 100644 --- a/.github/contributing.md +++ b/.github/contributing.md @@ -34,7 +34,7 @@ Hi! I'm really excited that you are interested in contributing to Vue Router. Be - Make sure tests pass! -- Commit messages must follow the [commit message convention](./commit-convention.md) so that changelogs can be automatically generated. Commit messages are automatically validated before commit (by invoking [Git Hooks](https://git-scm.com/docs/githooks) via [yorkie](https://github.com/yyx990803/yorkie)). +- Commit messages must follow the [commit message convention](./commit-convention.md) so that the changelog can be automatically generated. Commit messages are automatically validated before commit (by invoking [Git Hooks](https://git-scm.com/docs/githooks) via [yorkie](https://github.com/yyx990803/yorkie)). - No need to worry about code style as long as you have installed the dev dependencies - modified files are automatically formatted with Prettier on commit (by invoking [Git Hooks](https://git-scm.com/docs/githooks) via [yorkie](https://github.com/yyx990803/yorkie)). @@ -90,11 +90,13 @@ $ yarn jest --watch Vue Router source code can be found in the `src` directory: -- `src/components`: RouterLink and RouterView components. - `src/history`: history implementations that are instantiable with `create*History()`. This folder contains code related to using the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API). -- `src/matcher`: RouteMatcher implementation. Contains the code that transforms paths like `/users/:id` into regexes and handle the transformation of locations like `{ name: 'UserDetail', params: { id: '2' } }` to strings. It contains path ranking logic and the part of dynamic routing that concerns matching urls in the right order. -- `src/utils`: contains util functions that are used accross other sections of the router but are not contained by them. +- `src/matcher`: RouteMatcher implementation. Contains the code that transforms paths like `/users/:id` into regexps and handle the transformation of locations like `{ name: 'UserDetail', params: { id: '2' } }` to strings. It contains path ranking logic and the part of dynamic routing that concerns matching urls in the right order. +- `src/utils`: contains small utility functions that are used across other sections of the router but are not contained by them. - `src/router`: contains the router creation, navigation execution, using the matcher, the history implementation. It runs navigation guards. +- `src/location`: helpers related to route location and urls +- `src/encoding`: helpers related to url encoding +- `src/errors`: different internal and external errors with their messages - `src/index`: contains all public API as exports. - `src/types`: contains global types that are used across multiple sections of the router. diff --git a/__tests__/RouterLink.spec.ts b/__tests__/RouterLink.spec.ts index 5afdf43d..5eab0e97 100644 --- a/__tests__/RouterLink.spec.ts +++ b/__tests__/RouterLink.spec.ts @@ -1,7 +1,7 @@ /** * @jest-environment jsdom */ -import { RouterLink } from '../src/components/Link' +import { RouterLink } from '../src/RouterLink' import { START_LOCATION_NORMALIZED, RouteQueryAndHash, @@ -12,7 +12,7 @@ import { createMemoryHistory } from '../src' import { mount, createMockedRoute } from './mount' import { nextTick } from 'vue' import { RouteRecordNormalized } from '../src/matcher/types' -import { routerKey } from '../src/utils/injectionSymbols' +import { routerKey } from '../src/injectionSymbols' const records = { home: {} as RouteRecordNormalized, diff --git a/__tests__/RouterView.spec.ts b/__tests__/RouterView.spec.ts index d150a498..08a53a66 100644 --- a/__tests__/RouterView.spec.ts +++ b/__tests__/RouterView.spec.ts @@ -1,7 +1,7 @@ /** * @jest-environment jsdom */ -import { RouterView } from '../src/components/View' +import { RouterView } from '../src/RouterView' import { components, RouteLocationNormalizedLoose } from './utils' import { START_LOCATION_NORMALIZED } from '../src/types' import { markRaw } from 'vue' diff --git a/__tests__/encoding.spec.ts b/__tests__/encoding.spec.ts index 19883e17..f1e41a38 100644 --- a/__tests__/encoding.spec.ts +++ b/__tests__/encoding.spec.ts @@ -3,7 +3,7 @@ import { encodeParam, encodeQueryProperty, // decode, -} from '../src/utils/encoding' +} from '../src/encoding' describe('Encoding', () => { // all ascii chars with a non ascii char at the beginning diff --git a/__tests__/guards/navigationGuards.spec.ts b/__tests__/guards/guardToPromiseFn.spec.ts similarity index 100% rename from __tests__/guards/navigationGuards.spec.ts rename to __tests__/guards/guardToPromiseFn.spec.ts diff --git a/__tests__/location.spec.ts b/__tests__/location.spec.ts index 07222f32..252f39bb 100644 --- a/__tests__/location.spec.ts +++ b/__tests__/location.spec.ts @@ -1,11 +1,11 @@ import { normalizeHistoryLocation as normalizeLocation } from '../src/history/common' -import { parseQuery, stringifyQuery } from '../src/utils/query' +import { parseQuery, stringifyQuery } from '../src/query' import { parseURL as originalParseURL, stringifyURL as originalStringifyURL, stripBase, isSameLocationObject, -} from '../src/utils/location' +} from '../src/location' describe('parseURL', () => { let parseURL = originalParseURL.bind(null, parseQuery) diff --git a/__tests__/mount.ts b/__tests__/mount.ts index 5d6a6c4b..ce4061d1 100644 --- a/__tests__/mount.ts +++ b/__tests__/mount.ts @@ -17,7 +17,7 @@ import { import { compile } from '@vue/compiler-dom' import * as runtimeDom from '@vue/runtime-dom' import { RouteLocationNormalizedLoose } from './utils' -import { routeLocationKey } from '../src/utils/injectionSymbols' +import { routeLocationKey } from '../src/injectionSymbols' export interface MountOptions { propsData: Record diff --git a/__tests__/parseQuery.spec.ts b/__tests__/parseQuery.spec.ts index 5fc772c4..49909616 100644 --- a/__tests__/parseQuery.spec.ts +++ b/__tests__/parseQuery.spec.ts @@ -1,4 +1,4 @@ -import { parseQuery } from '../src/utils/query' +import { parseQuery } from '../src/query' import { mockWarn } from 'jest-mock-warn' describe('parseQuery', () => { diff --git a/__tests__/stringifyQuery.spec.ts b/__tests__/stringifyQuery.spec.ts index 647db4dd..038e07d4 100644 --- a/__tests__/stringifyQuery.spec.ts +++ b/__tests__/stringifyQuery.spec.ts @@ -1,4 +1,4 @@ -import { stringifyQuery } from '../src/utils/query' +import { stringifyQuery } from '../src/query' import { mockWarn } from 'jest-mock-warn' describe('stringifyQuery', () => { diff --git a/__tests__/urlEncoding.spec.ts b/__tests__/urlEncoding.spec.ts index 085a6989..abf1bccd 100644 --- a/__tests__/urlEncoding.spec.ts +++ b/__tests__/urlEncoding.spec.ts @@ -2,7 +2,7 @@ import { createRouter as newRouter } from '../src/router' import { components } from './utils' import { RouteRecordRaw } from '../src/types' import { createMemoryHistory } from '../src' -import * as encoding from '../src/utils/encoding' +import * as encoding from '../src/encoding' jest.mock('../src/utils/encoding') diff --git a/src/components/Link.ts b/src/RouterLink.ts similarity index 95% rename from src/components/Link.ts rename to src/RouterLink.ts index 08a98efe..b093820b 100644 --- a/src/components/Link.ts +++ b/src/RouterLink.ts @@ -8,10 +8,10 @@ import { unref, Component, } from 'vue' -import { RouteLocationRaw, VueUseOptions, RouteLocation } from '../types' -import { isSameLocationObject, isSameRouteRecord } from '../utils/location' -import { routerKey, routeLocationKey } from '../utils/injectionSymbols' -import { RouteRecord } from '../matcher/types' +import { RouteLocationRaw, VueUseOptions, RouteLocation } from './types' +import { isSameLocationObject, isSameRouteRecord } from './location' +import { routerKey, routeLocationKey } from './injectionSymbols' +import { RouteRecord } from './matcher/types' interface LinkProps { to: RouteLocationRaw diff --git a/src/components/View.ts b/src/RouterView.ts similarity index 96% rename from src/components/View.ts rename to src/RouterView.ts index 4d29de11..d622ed41 100644 --- a/src/components/View.ts +++ b/src/RouterView.ts @@ -9,12 +9,12 @@ import { ComponentPublicInstance, Component, } from 'vue' -import { RouteLocationNormalizedLoaded } from '../types' +import { RouteLocationNormalizedLoaded } from './types' import { matchedRouteKey, viewDepthKey, routeLocationKey, -} from '../utils/injectionSymbols' +} from './injectionSymbols' export const RouterView = (defineComponent({ name: 'RouterView', diff --git a/src/utils/encoding.ts b/src/encoding.ts similarity index 100% rename from src/utils/encoding.ts rename to src/encoding.ts diff --git a/src/history/html5.ts b/src/history/html5.ts index bf523732..0a8b8a14 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -15,7 +15,7 @@ import { ScrollPositionCoordinates, } from '../scrollBehavior' import { warn } from 'vue' -import { stripBase } from '../utils/location' +import { stripBase } from '../location' type PopStateListener = (this: Window, ev: PopStateEvent) => any diff --git a/src/index.ts b/src/index.ts index 9dd78ee9..8a3e46d1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ export { stringifyQuery, LocationQueryRaw, LocationQueryValue, -} from './utils/query' +} from './query' export { RouterHistory } from './history/common' @@ -38,8 +38,8 @@ export { export { NavigationFailureType, NavigationFailure } from './errors' export { onBeforeRouteLeave } from './navigationGuards' -export { RouterLink, useLink } from './components/Link' -export { RouterView } from './components/View' +export { RouterLink, useLink } from './RouterLink' +export { RouterView } from './RouterView' export { createWebHistory, createMemoryHistory, createWebHashHistory } diff --git a/src/utils/injectionSymbols.ts b/src/injectionSymbols.ts similarity index 82% rename from src/utils/injectionSymbols.ts rename to src/injectionSymbols.ts index 7247804c..cac3b0f7 100644 --- a/src/utils/injectionSymbols.ts +++ b/src/injectionSymbols.ts @@ -1,7 +1,7 @@ import { InjectionKey, ComputedRef } from 'vue' -import { RouteLocationNormalizedLoaded } from '../types' -import { Router } from '../router' -import { RouteRecordNormalized } from '../matcher/types' +import { RouteLocationNormalizedLoaded } from './types' +import { Router } from './router' +import { RouteRecordNormalized } from './matcher/types' export const hasSymbol = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol' diff --git a/src/install.ts b/src/install.ts index 1617cebd..d95df979 100644 --- a/src/install.ts +++ b/src/install.ts @@ -1,7 +1,7 @@ import { App, ComputedRef, reactive } from 'vue' import { Router } from './router' -import { RouterLink } from './components/Link' -import { RouterView } from './components/View' +import { RouterLink } from './RouterLink' +import { RouterView } from './RouterView' import { isBrowser } from './utils' import { START_LOCATION_NORMALIZED, @@ -9,7 +9,7 @@ import { NavigationGuardWithThis, NavigationGuard, } from './types' -import { routerKey, routeLocationKey } from './utils/injectionSymbols' +import { routerKey, routeLocationKey } from './injectionSymbols' declare module '@vue/runtime-core' { interface ComponentCustomOptions { diff --git a/src/utils/location.ts b/src/location.ts similarity index 98% rename from src/utils/location.ts rename to src/location.ts index 84f5f98a..a0b9dc7c 100644 --- a/src/utils/location.ts +++ b/src/location.ts @@ -3,8 +3,8 @@ import { RouteLocation, RouteLocationNormalized, RouteParamValue, -} from '../types' -import { RouteRecord } from '../matcher/types' +} from './types' +import { RouteRecord } from './matcher/types' /** * Location object returned by {@link `parseURL`}. diff --git a/src/navigationGuards.ts b/src/navigationGuards.ts index 57348eca..156840a0 100644 --- a/src/navigationGuards.ts +++ b/src/navigationGuards.ts @@ -18,7 +18,7 @@ import { } from './errors' import { ComponentPublicInstance } from 'vue' import { inject, getCurrentInstance, warn } from 'vue' -import { matchedRouteKey } from './utils/injectionSymbols' +import { matchedRouteKey } from './injectionSymbols' import { RouteRecordNormalized } from './matcher/types' import { isESModule } from './utils' @@ -111,7 +111,10 @@ export function extractComponentsGuards( ) guards.push(async () => { const resolved = await componentPromise - if (!resolved) throw new Error('TODO: error while fetching') + if (!resolved) + throw new Error( + `Couldn't resolve component "${name}" for the following record with path "${record.path}"` + ) const resolvedComponent = isESModule(resolved) ? resolved.default : resolved diff --git a/src/utils/query.ts b/src/query.ts similarity index 98% rename from src/utils/query.ts rename to src/query.ts index 2d8c053a..512cd35d 100644 --- a/src/utils/query.ts +++ b/src/query.ts @@ -1,4 +1,4 @@ -import { decode, encodeQueryProperty } from '../utils/encoding' +import { decode, encodeQueryProperty } from './encoding' /** * Possible values in normalized {@link LocationQuery} diff --git a/src/router.ts b/src/router.ts index 8d137340..68226e19 100644 --- a/src/router.ts +++ b/src/router.ts @@ -33,15 +33,15 @@ import { } from './errors' import { applyToParams, isBrowser } from './utils' import { useCallbacks } from './utils/callbacks' -import { encodeParam, decode } from './utils/encoding' +import { encodeParam, decode } from './encoding' import { normalizeQuery, parseQuery as originalParseQuery, stringifyQuery as originalStringifyQuery, -} from './utils/query' +} from './query' import { ref, Ref, markRaw, nextTick, App, warn } from 'vue' import { RouteRecord, RouteRecordNormalized } from './matcher/types' -import { parseURL, stringifyURL, isSameRouteLocation } from './utils/location' +import { parseURL, stringifyURL, isSameRouteLocation } from './location' import { extractComponentsGuards, guardToPromiseFn } from './navigationGuards' import { applyRouterPlugin } from './install' diff --git a/src/types/index.ts b/src/types/index.ts index a8eb7f7d..3943a791 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,4 @@ -import { LocationQuery, LocationQueryRaw } from '../utils/query' +import { LocationQuery, LocationQueryRaw } from '../query' import { PathParserOptions } from '../matcher/pathParserRanker' import { markRaw, Ref, ComputedRef, ComponentOptions } from 'vue' import { RouteRecord, RouteRecordNormalized } from '../matcher/types' diff --git a/src/useApi.ts b/src/useApi.ts index 8d4b90d4..ad00e759 100644 --- a/src/useApi.ts +++ b/src/useApi.ts @@ -1,5 +1,5 @@ import { inject } from 'vue' -import { routerKey, routeLocationKey } from './utils/injectionSymbols' +import { routerKey, routeLocationKey } from './injectionSymbols' import { Router } from './router' import { RouteLocationNormalizedLoaded } from './types' diff --git a/src/utils/index.ts b/src/utils/index.ts index 139e3f56..9b074d02 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,5 @@ import { RouteParams, RouteComponent } from '../types' -import { hasSymbol } from './injectionSymbols' +import { hasSymbol } from '../injectionSymbols' export * from './env'