Skip to content

Commit

Permalink
fix(options): make all options optional
Browse files Browse the repository at this point in the history
Fix #13
  • Loading branch information
posva committed Jul 4, 2022
1 parent 17196c7 commit 9a573dd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chokidar from 'chokidar'
import { Options } from '../options'
import { ResolvedOptions } from '../options'
import { createPrefixTree } from './tree'
import { promises as fs } from 'fs'
import { logTree, throttle } from './utils'
Expand All @@ -11,7 +11,7 @@ import { resolve } from 'pathe'
import { ServerContext } from '../options'
import { getRouteBlock } from './customBlock'

export function createRoutesContext(options: Required<Options>) {
export function createRoutesContext(options: ResolvedOptions) {
const { dts: preferDTS, root } = options
const dts =
preferDTS === false
Expand Down
6 changes: 3 additions & 3 deletions src/core/customBlock.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SFCBlock, parse } from '@vue/compiler-sfc'
import { promises as fs } from 'fs'
import { Options } from '../options'
import { ResolvedOptions } from '../options'
import JSON5 from 'json5'
import { parse as YAMLParser } from 'yaml'
import { RouteRecordRaw } from 'vue-router'

export async function getRouteBlock(path: string, options: Required<Options>) {
export async function getRouteBlock(path: string, options: ResolvedOptions) {
const content = await fs.readFile(path, 'utf8')

const parsedSFC = await parse(content, { pad: 'space' }).descriptor
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface CustomRouteBlock
function parseCustomBlock(
block: SFCBlock,
filePath: string,
options: Required<Options>
options: ResolvedOptions
): CustomRouteBlock | undefined {
const lang = block.lang ?? options.routeBlockLang

Expand Down
8 changes: 4 additions & 4 deletions src/core/tree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Options } from '../options'
import type { ResolvedOptions } from '../options'
import { createTreeLeafValue } from './treeLeafValue'
import type { TreeLeafValue } from './treeLeafValue'
import { trimExtension } from './utils'
Expand All @@ -22,9 +22,9 @@ export class TreeLeaf {
/**
* Plugin options taken into account by the tree.
*/
options: Options
options: ResolvedOptions

constructor(options: Options, filePath: string, parent?: TreeLeaf) {
constructor(options: ResolvedOptions, filePath: string, parent?: TreeLeaf) {
this.options = options
this.parent = parent
this.value = createTreeLeafValue(filePath, parent?.value)
Expand Down Expand Up @@ -122,7 +122,7 @@ export class TreeLeaf {
}
}

export function createPrefixTree(options: Options) {
export function createPrefixTree(options: ResolvedOptions) {
return new TreeLeaf(options, '')
}

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
getVirtualId as _getVirtualId,
asVirtualId as _asVirtualId,
} from './core/moduleConstants'
import { DEFAULT_OPTIONS, Options } from './options'
import { DEFAULT_OPTIONS, Options, ResolvedOptions } from './options'
import { createViteContext } from './core/vite'

export default createUnplugin<Options>((opt, meta) => {
const options: Required<Options> = { ...DEFAULT_OPTIONS, ...opt }
const options: ResolvedOptions = { ...DEFAULT_OPTIONS, ...opt }
const ctx = createRoutesContext(options)

function getVirtualId(id: string) {
Expand Down
22 changes: 12 additions & 10 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { isPackageExists } from 'local-pkg'
import { getFileBasedRouteName } from './core/utils'
import type { TreeLeaf } from './core/tree'

export interface Options {
export interface ResolvedOptions {
/**
* Extensions of files to be considered as pages. Defaults to `['.vue']`. Cannot be empty.
*/
extensions?: string[]
extensions: string[]
/**
* Folder containing the components that should be used for routes.
*
* @default "src/routes"
*/
routesFolder?: string
routesFolder: string
// TODO: add support for multiple routes folders and prepending a path segment

/**
Expand All @@ -29,30 +29,32 @@ export interface Options {
/**
* Array of file globs to ignore. Defaults to `[]`.
*/
exclude?: string[]
exclude: string[]

root?: string
root: string

routeBlockLang?: string
routeBlockLang: string

/**
* Should generate d.ts files. Defaults to `true` if `typescript` is installed.
*/
dts?: boolean | string
dts: boolean | string

/**
* Allows inspection by vite-plugin-inspect by not adding the leading `\0` to the id of virtual modules.
* @internal
*/
_inspect?: boolean
_inspect: boolean

/**
* Activates debug logs.
*/
logs?: boolean
logs: boolean
}

export const DEFAULT_OPTIONS: Required<Options> = {
export type Options = Partial<ResolvedOptions>

export const DEFAULT_OPTIONS: ResolvedOptions = {
extensions: ['.vue'],
exclude: [],
routesFolder: 'src/routes',
Expand Down

0 comments on commit 9a573dd

Please sign in to comment.