Skip to content

Commit

Permalink
fix: remove the need to install @pinia/colada
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Feb 23, 2024
1 parent 87c1d88 commit 8d45669
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 51 deletions.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@
"require": "./dist/types.js",
"import": "./dist/types.mjs"
},
"./data-fetching/entries/basic": {
"types": {
"require": "./dist/data-fetching/entries/basic.d.ts",
"import": "./dist/data-fetching/entries/basic.d.mts"
},
"require": "./dist/data-fetching/entries/basic.js",
"import": "./dist/data-fetching/entries/basic.mjs"
},
"./data-fetching/entries/pinia-colada": {
"types": {
"require": "./dist/data-fetching/entries/pinia-colada.d.ts",
"import": "./dist/data-fetching/entries/pinia-colada.d.mts"
},
"require": "./dist/data-fetching/entries/pinia-colada.js",
"import": "./dist/data-fetching/entries/pinia-colada.mjs"
},
"./client": {
"types": "./client.d.ts"
}
Expand Down
5 changes: 3 additions & 2 deletions src/codegen/vueRouterModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export {
definePage,
// new data fetching
DataLoaderPlugin,
defineBasicLoader,
defineColadaLoader,
// FIXME: automatically add?
// defineBasicLoader,
// defineColadaLoader,
NavigationResult,
} from 'unplugin-vue-router/runtime'
Expand Down
9 changes: 1 addition & 8 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { ResolvedOptions } from '../options'
import { TreeNode, PrefixTree } from './tree'
import { promises as fs } from 'fs'
import {
appendExtensionListToPattern,
asRoutePath,
ImportsMap,
logTree,
throttle,
} from './utils'
import { asRoutePath, ImportsMap, logTree, throttle } from './utils'
import { generateRouteNamedMap } from '../codegen/generateRouteMap'
import { MODULE_ROUTES_PATH, MODULE_VUE_ROUTER } from './moduleConstants'
import { generateRouteRecord } from '../codegen/generateRouteRecords'
Expand Down Expand Up @@ -209,7 +203,6 @@ export function createRoutesContext(options: ResolvedOptions) {
}

let lastDTS: string | undefined
let lastTypesConfigDTS: string | undefined
async function _writeConfigFiles() {
logger.time('writeConfigFiles')

Expand Down
4 changes: 0 additions & 4 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import type { RouteRecordOverride, TreeRouteParam } from './treeNodeValue'
import { pascalCase } from 'scule'
import { ResolvedOptions, RoutesFolderOption } from '../options'

export type LiteralStringUnion<LiteralType, BaseType extends string = string> =
| LiteralType
| (BaseType & Record<never, never>)

export function warn(
msg: string,
type: 'warn' | 'error' | 'debug' = 'warn'
Expand Down
2 changes: 1 addition & 1 deletion src/data-fetching/createDataLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ShallowRef } from 'vue'
import { IS_USE_DATA_LOADER_KEY, STAGED_NO_VALUE } from './meta-extensions'
import type { IS_USE_DATA_LOADER_KEY, STAGED_NO_VALUE } from './meta-extensions'
import { type _PromiseMerged } from './utils'
import { type NavigationResult } from './navigation-guard'
import type {
Expand Down
10 changes: 5 additions & 5 deletions src/data-fetching/defineColadaLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
RouteRecordName,
Router,
} from 'unplugin-vue-router/types'
import {
import type {
DataLoaderContextBase,
DataLoaderEntryBase,
DefineDataLoaderOptionsBase,
Expand All @@ -30,7 +30,7 @@ import {
} from './meta-extensions'
import {
IS_CLIENT,
_PromiseMerged,
type _PromiseMerged,
assign,
getCurrentContext,
isSubsetOf,
Expand All @@ -40,9 +40,9 @@ import {
import { type ShallowRef, shallowRef, watch } from 'vue'
import { NavigationResult } from './navigation-guard'
import {
UseQueryKey,
UseQueryOptions,
UseQueryReturn,
type UseQueryKey,
type UseQueryOptions,
type UseQueryReturn,
useQuery,
} from '@pinia/colada'

Expand Down
15 changes: 9 additions & 6 deletions src/data-fetching/defineLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
RouteRecordName,
Router,
} from 'unplugin-vue-router/types'
import {
import type {
DataLoaderContextBase,
DataLoaderEntryBase,
DefineDataLoaderOptionsBase,
Expand Down Expand Up @@ -49,23 +49,23 @@ export function defineBasicLoader<
RouteLocationNormalizedLoaded<Name>
>,
options?: DefineDataLoaderOptions<isLazy>
): UseDataLoader<isLazy, Data>
): UseDataLoaderBasic<isLazy, Data>
export function defineBasicLoader<Data, isLazy extends boolean>(
loader: DefineLoaderFn<
Data,
DataLoaderContext,
RouteLocationNormalizedLoaded
>,
options?: DefineDataLoaderOptions<isLazy>
): UseDataLoader<isLazy, Data>
): UseDataLoaderBasic<isLazy, Data>

export function defineBasicLoader<Data, isLazy extends boolean>(
nameOrLoader: RouteRecordName | DefineLoaderFn<Data, DataLoaderContext>,
_loaderOrOptions?:
| DefineDataLoaderOptions<isLazy>
| DefineLoaderFn<Data, DataLoaderContext>,
opts?: DefineDataLoaderOptions<isLazy>
): UseDataLoader<isLazy, Data> {
): UseDataLoaderBasic<isLazy, Data> {
// TODO: make it DEV only and remove the first argument in production mode
// resolve option overrides
const loader =
Expand Down Expand Up @@ -268,7 +268,7 @@ export function defineBasicLoader<Data, isLazy extends boolean>(

// @ts-expect-error: requires the internals and symbol that are added later
const useDataLoader: // for ts
UseDataLoader<isLazy, Data> = () => {
UseDataLoaderBasic<isLazy, Data> = () => {
// work with nested data loaders
const [parentEntry, _router, _route] = getCurrentContext()
// fallback to the global router and routes for useDataLoaders used within components
Expand Down Expand Up @@ -378,7 +378,7 @@ export interface DataLoaderContext extends DataLoaderContextBase {}
const DEFAULT_DEFINE_LOADER_OPTIONS = {
lazy: false as boolean,
server: true,
commit: 'immediate',
commit: 'after-load',
} satisfies DefineDataLoaderOptions<boolean>

/**
Expand All @@ -405,3 +405,6 @@ declare module 'vue-router' {
[INITIAL_DATA_KEY]?: Record<string, unknown> | false
}
}

export interface UseDataLoaderBasic<isLazy extends boolean, Data>
extends UseDataLoader<isLazy, Data> {}
19 changes: 19 additions & 0 deletions src/data-fetching/entries/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export { defineBasicLoader } from '../defineLoader'
export type {
UseDataLoaderBasic,
DataLoaderContext,
DefineDataLoaderOptions,
} from '../defineLoader'

export type {
UseDataLoader,
UseDataLoaderInternals,
UseDataLoaderResult,
DataLoaderContextBase,
DataLoaderEntryBase,
DefineDataLoaderOptionsBase,
DefineLoaderFn,
_DataMaybeLazy,
DefineDataLoader,
DefineDataLoaderCommit,
} from '../createDataLoader'
20 changes: 20 additions & 0 deletions src/data-fetching/entries/pinia-colada.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export { defineColadaLoader } from '../defineColadaLoader'
export type {
DataLoaderColadaEntry,
DataColadaLoaderContext,
DefineDataColadaLoaderOptions,
UseDataLoaderColada,
UseDataLoaderColadaResult,
} from '../defineColadaLoader.ts'
export type {
UseDataLoader,
UseDataLoaderInternals,
UseDataLoaderResult,
DataLoaderContextBase,
DataLoaderEntryBase,
DefineDataLoaderOptionsBase,
DefineLoaderFn,
_DataMaybeLazy,
DefineDataLoader,
DefineDataLoaderCommit,
} from '../createDataLoader'
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export * from './types'

export { DEFAULT_OPTIONS }

export default createUnplugin<Options | undefined>((opt = {}, meta) => {
export default createUnplugin<Options | undefined>((opt = {}, _meta) => {
const options = resolveOptions(opt)
const ctx = createRoutesContext(options)

Expand Down Expand Up @@ -71,6 +71,7 @@ export default createUnplugin<Options | undefined>((opt = {}, meta) => {
if (routeBlockQueryRE.test(id)) {
return ROUTE_BLOCK_ID
}
return undefined // ok TS...
},

buildStart() {
Expand Down Expand Up @@ -124,6 +125,8 @@ export default createUnplugin<Options | undefined>((opt = {}, meta) => {
if (resolvedId === MODULE_VUE_ROUTER) {
return ctx.generateVueRouterProxy()
}

return undefined // ok TS...
},

// improves DX
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { EditableTreeNode } from './core/extendRoutes'
import { type ParseSegmentOptions } from './core/treeNodeValue'
import { type _Awaitable } from './utils'

// TODO: remove from exports and move export to src/index?

/**
* Options for a routes folder.
*/
Expand Down
14 changes: 0 additions & 14 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ export type {
} from './data-fetching/createDataLoader'

// new data fetching
export { defineBasicLoader } from './data-fetching/defineLoader'
export type {
DataLoaderContext,
DefineDataLoaderOptions,
} from './data-fetching/defineLoader'
export {
DataLoaderPlugin,
NavigationResult,
Expand All @@ -27,15 +22,6 @@ export type {
_DataLoaderRedirectResult,
} from './data-fetching/navigation-guard'

export type {
DataLoaderColadaEntry,
DataColadaLoaderContext,
DefineDataColadaLoaderOptions,
UseDataLoaderColada,
UseDataLoaderColadaResult,
} from './data-fetching/defineColadaLoader'
export { defineColadaLoader } from './data-fetching/defineColadaLoader'

// NOTE: for tests only
// export * from './data-fetching/defineQueryLoader'

Expand Down
3 changes: 2 additions & 1 deletion src/type-extensions/routeLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {
RouteRecordName,
} from 'vue-router'
import type { _RouteMapGeneric } from '../codegen/generateRouteMap'
import type { LiteralStringUnion } from '../core/utils'
import type { LiteralStringUnion } from '../utils'
// inlining the type as it avoids code splitting issues
import type { RouteNamedMap } from './types-config'

export type _RouteRecordName = keyof RouteNamedMap
Expand Down
8 changes: 0 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* https://github.com/posva/unplugin-vue-router/issues/136
*/

export type { Options } from './options'

export type {
_RouteMapGeneric,
RouteRecordInfo,
Expand Down Expand Up @@ -66,9 +64,3 @@ export type {
ParamValueZeroOrMore,
ParamValueZeroOrOne,
} from './codegen/generateRouteParams'

export type { TreeNode } from './core/tree'
export type {
TreeNodeValueParam,
TreeNodeValueStatic,
} from './core/treeNodeValue'
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
* @internal
*/
export type _Awaitable<T> = T | PromiseLike<T>

/**
* Creates a union type that still allows autocompletion for strings.
*@internal
*/
export type LiteralStringUnion<LiteralType, BaseType extends string = string> =
| LiteralType
| (BaseType & Record<never, never>)
2 changes: 1 addition & 1 deletion tsup-runtime.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig([
{
...commonOptions,
clean: false,
entry: ['./src/runtime.ts'],
entry: ['./src/runtime.ts', './src/data-fetching/entries/*'],
external: [...commonOptions.external, 'unplugin-vue-router/types'],
},
])
2 changes: 2 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default defineConfig([
{
...commonOptions,
entry: [
'./src/index.ts',
'./src/options.ts',
'./src/esbuild.ts',
'./src/rollup.ts',
'./src/vite.ts',
Expand Down

0 comments on commit 8d45669

Please sign in to comment.