Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Aug 16, 2024
1 parent 4175b6c commit 7aa9f43
Show file tree
Hide file tree
Showing 23 changed files with 832 additions and 848 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/types/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import type { PageBase, PageData, PageFrontmatter } from '@vuepress/shared'
* Vuepress Page
*/
export type Page<
ExtraPageData extends Record<any, any> = Record<never, never>,
ExtraPageFrontmatter extends Record<any, any> = Record<string, unknown>,
ExtraPageFields extends Record<any, any> = Record<never, never>,
> = PageBase<ExtraPageFrontmatter> &
ExtraPageFields & {
ExtraPageData extends Record<string, unknown> = Record<string, unknown>,
ExtraPageFrontmatter extends Record<string, unknown> = Record<
string,
unknown
>,
ExtraPageFields extends Record<string, unknown> = Record<string, unknown>,
> = ExtraPageFields &
PageBase<ExtraPageFrontmatter> & {
/**
* Data of the page, which will be available in client code
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type { HooksExposed } from './pluginApi/index.js'
* A plugin package should have a `Plugin` as the default export
*/
export type Plugin<T extends PluginObject = PluginObject> =
| T
| PluginFunction<T>
| T

/**
* Vuepress plugin function
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/types/pluginApi/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ interface Closable {
export interface Hook<
Exposed,
Normalized = Exposed,
Result = Normalized extends (...args: any) => infer U
Result = Normalized extends (...args: unknown[]) => infer U
? U extends Promise<infer V>
? V
: U
: void,
: never,
> {
exposed: Exposed
normalized: Normalized
Expand All @@ -41,9 +41,9 @@ export type ClientConfigFileHook = Hook<

// alias and define hook
export type AliasDefineHook = Hook<
| Record<string, any>
| ((app: App, isServer: boolean) => PromiseOrNot<Record<string, any>>),
(app: App, isServer: boolean) => Promise<Record<string, any>>
| Record<string, unknown>
| ((app: App, isServer: boolean) => PromiseOrNot<Record<string, unknown>>),
(app: App, isServer: boolean) => Promise<Record<string, unknown>>
>

/**
Expand All @@ -58,7 +58,7 @@ export interface Hooks {
extendsMarkdown: ExtendsHook<Markdown>
extendsPageOptions: ExtendsHook<PageOptions>
extendsPage: ExtendsHook<Page>
extendsBundlerOptions: ExtendsHook<any>
extendsBundlerOptions: ExtendsHook<Record<string, unknown>>
clientConfigFile: ClientConfigFileHook
alias: AliasDefineHook
define: AliasDefineHook
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/app/resolvePluginObject.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { path } from '@vuepress/utils'
import { describe, expect, it, vi } from 'vitest'
import type { Bundler, PluginFunction, PluginObject } from '../../src/index.js'
import { createBaseApp, resolvePluginObject } from '../../src/index.js'
import type { PluginFunction, PluginObject } from '../../src/index.js'

const app = createBaseApp({
source: path.resolve(__dirname, 'fake-source'),
theme: { name: 'test' },
bundler: {} as any,
bundler: {} as Bundler,
})

describe('core > app > resolvePluginObject', () => {
Expand All @@ -20,7 +20,7 @@ describe('core > app > resolvePluginObject', () => {
})

it('should work with plugin function', () => {
const pluginFunction: PluginFunction = vi.fn((app) => ({
const pluginFunction: PluginFunction = vi.fn(() => ({
name: 'plugin-function',
}))

Expand Down
23 changes: 13 additions & 10 deletions packages/core/tests/page/createPage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { path } from '@vuepress/utils'
import { beforeAll, describe, expect, it, vi } from 'vitest'
import type { Bundler } from '../../src/index.js'
import { createBaseApp, createPage } from '../../src/index.js'

const app = createBaseApp({
source: path.resolve(__dirname, 'fake-source'),
theme: { name: 'test' },
bundler: {} as any,
})
describe('should work without plugins', () => {
const app = createBaseApp({
source: path.resolve(__dirname, 'fake-source'),
theme: { name: 'test' },
bundler: {} as Bundler,
})

beforeAll(async () => {
await app.init()
})
beforeAll(async () => {
await app.init()
})

describe('core > page > createPage', () => {
it('should throw an error', async () => {
const consoleError = console.error
console.error = vi.fn()
Expand Down Expand Up @@ -86,12 +87,14 @@ describe('core > page > createPage', () => {
)
expect(page.chunkName).toBeTruthy()
})
})

describe('should work with plugins', () => {
it('should be extended by plugin correctly', async () => {
const app = createBaseApp({
source: path.resolve(__dirname, 'fake-source'),
theme: { name: 'test' },
bundler: {} as any,
bundler: {} as Bundler,
})
app.use({
name: 'foo',
Expand Down
60 changes: 29 additions & 31 deletions packages/core/tests/page/inferPagePath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,39 @@ const TEST_CASES: [string, ReturnType<typeof inferPagePath>][] = [
],
]

describe('core > page > inferPagePath', () => {
describe('should infer page path according to relative path of page file', () => {
TEST_CASES.forEach(([source, expected]) => {
it(JSON.stringify(source), () => {
expect(
inferPagePath({
app,
filePathRelative: source,
}),
).toEqual(expected)
})
describe('should infer page path according to relative path of page file', () => {
TEST_CASES.forEach(([source, expected]) => {
it(JSON.stringify(source), () => {
expect(
inferPagePath({
app,
filePathRelative: source,
}),
).toEqual(expected)
})
})
})

it('should use `/` as the default locale path', () => {
expect(
inferPagePath({
app: appWithoutLocales,
filePathRelative: 'en/foo/bar.md',
}),
).toEqual({
pathInferred: '/en/foo/bar.html',
pathLocale: '/',
})
it('should use `/` as the default locale path', () => {
expect(
inferPagePath({
app: appWithoutLocales,
filePathRelative: 'en/foo/bar.md',
}),
).toEqual({
pathInferred: '/en/foo/bar.html',
pathLocale: '/',
})
})

it('should handle empty file relative path', () => {
expect(
inferPagePath({
app,
filePathRelative: null,
}),
).toEqual({
pathInferred: null,
pathLocale: '/',
})
it('should handle empty file relative path', () => {
expect(
inferPagePath({
app,
filePathRelative: null,
}),
).toEqual({
pathInferred: null,
pathLocale: '/',
})
})
Loading

0 comments on commit 7aa9f43

Please sign in to comment.