Skip to content

Commit

Permalink
feat!: rename filePattern to filePatterns and allow arrays
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `filePattern` is now named `filePatterns` because it
allows arrays. This is only a naming change.
  • Loading branch information
posva committed Mar 2, 2023
1 parent 5dccd2b commit 8902778
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -131,6 +131,7 @@
"typescript": "^4.9.5",
"unplugin-auto-import": "^0.15.0",
"vite": "^4.1.4",
"vite-plugin-vue-markdown": "^0.22.4",
"vitest": "^0.29.1",
"vue": "^3.2.47",
"vue-router": "^4.1.6",
Expand Down
1 change: 1 addition & 0 deletions playground/src/docs/real/index.md
@@ -0,0 +1 @@
# Only some pages should be included
3 changes: 3 additions & 0 deletions playground/src/docs/should-be-ignored.md
@@ -0,0 +1,3 @@
# Ignored

This file is at the root so it will get ignored
3 changes: 1 addition & 2 deletions playground/typed-router.d.ts
Expand Up @@ -53,8 +53,7 @@ declare module 'vue-router/auto/routes' {
'deep the most rebel': RouteRecordInfo<'deep the most rebel', '/deep-most-rebel', Record<never, never>, Record<never, never>>,
'/deep/nesting/works/custom-path': RouteRecordInfo<'/deep/nesting/works/custom-path', '/deep-surprise-:id(\d+)', Record<never, never>, Record<never, never>>,
'deep a rebel': RouteRecordInfo<'deep a rebel', '/deep/nesting/works/custom-name', Record<never, never>, Record<never, never>>,
'/docs/:lang/': RouteRecordInfo<'/docs/:lang/', '/docs/:lang', Record<never, never>, Record<never, never>>,
'/docs/:lang/about': RouteRecordInfo<'/docs/:lang/about', '/docs/:lang/about', Record<never, never>, Record<never, never>>,
'/docs/:lang/real/': RouteRecordInfo<'/docs/:lang/real/', '/docs/:lang/real', Record<never, never>, Record<never, never>>,
'/from-root': RouteRecordInfo<'/from-root', '/from-root', Record<never, never>, Record<never, never>>,
'the most rebel': RouteRecordInfo<'the most rebel', '/most-rebel', Record<never, never>, Record<never, never>>,
'/multiple-[a]-[b]-params': RouteRecordInfo<'/multiple-[a]-[b]-params', '/multiple-:a-:b-params', { a: ParamValue<true>, b: ParamValue<true> }, { a: ParamValue<false>, b: ParamValue<false> }>,
Expand Down
8 changes: 7 additions & 1 deletion playground/vite.config.ts
@@ -1,6 +1,7 @@
import { fileURLToPath, URL } from 'url'
import { defineConfig } from 'vite'
import Inspect from 'vite-plugin-inspect'
import Markdown from 'vite-plugin-vue-markdown'
// @ts-ignore: the plugin should not be checked in the playground
import VueRouter from '../src/vite'
import {
Expand Down Expand Up @@ -81,6 +82,8 @@ export default defineConfig({
{
src: 'src/docs',
path: 'docs/:lang/',
filePatterns: ['*/**/*'],
extensions: ['.md'],
},
// {
// src: 'src/features/',
Expand All @@ -99,7 +102,10 @@ export default defineConfig({
// './src/pages/**/*.spec.ts',
],
}),
Vue({}),
Vue({
include: [/\.vue$/, /\.md$/]
}),
Markdown(),
AutoImport({
imports: [VueRouterAutoImports],
}),
Expand Down
147 changes: 147 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions src/core/context.ts
Expand Up @@ -55,7 +55,7 @@ export function createRoutesContext(options: ResolvedOptions) {
}

const globalPattern = appendExtensionListToPattern(
options.filePattern,
options.filePatterns,
options.extensions
)

Expand All @@ -67,9 +67,9 @@ export function createRoutesContext(options: ResolvedOptions) {
}

// override the pattern if the folder has a custom pattern
const pattern = folder.filePattern
const pattern = folder.filePatterns
? appendExtensionListToPattern(
folder.filePattern,
folder.filePatterns,
// also override the extensions if the folder has a custom extensions
folder.extensions || options.extensions
)
Expand Down
16 changes: 9 additions & 7 deletions src/core/utils.ts
Expand Up @@ -260,15 +260,17 @@ export function asRoutePath(
* @returns
*/
export function appendExtensionListToPattern(
filePattern: string,
filePatterns: string | string[],
extensions: string[]
) {
return (
filePattern +
(extensions.length === 1
): string[] | string {
const extensionPattern =
extensions.length === 1
? extensions[0]
: `.{${extensions
.map((extension) => extension.replace('.', ''))
.join(',')}}`)
)
.join(',')}}`

return Array.isArray(filePatterns)
? filePatterns.map((filePattern) => `${filePattern}${extensionPattern}`)
: `${filePatterns}${extensionPattern}`
}

0 comments on commit 8902778

Please sign in to comment.