From 7aa9f43c6fc9e83f83b8d62888904bf97838c7d5 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Fri, 16 Aug 2024 10:11:34 +0800 Subject: [PATCH] chore: updates --- packages/core/src/types/page.ts | 13 +- packages/core/src/types/plugin.ts | 2 +- packages/core/src/types/pluginApi/hooks.ts | 12 +- .../tests/app/resolvePluginObject.spec.ts | 6 +- packages/core/tests/page/createPage.spec.ts | 23 +- .../core/tests/page/inferPagePath.spec.ts | 60 ++- .../core/tests/page/renderPageContent.spec.ts | 205 ++++---- .../tests/page/resolvePageChunkInfo.spec.ts | 22 +- .../page/resolvePageComponentInfo.spec.ts | 23 +- .../core/tests/page/resolvePageDate.spec.ts | 10 +- .../tests/page/resolvePageFileContent.spec.ts | 58 ++- .../tests/page/resolvePageFilePath.spec.ts | 77 ++- .../tests/page/resolvePageHtmlInfo.spec.ts | 24 +- .../core/tests/page/resolvePageLang.spec.ts | 68 ++- .../core/tests/page/resolvePagePath.spec.ts | 36 +- .../tests/page/resolvePagePermalink.spec.ts | 317 ++++++------ .../tests/page/resolvePageRouteMeta.spec.ts | 34 +- .../core/tests/page/resolvePageSlug.spec.ts | 34 +- .../tests/pluginApi/createHookQueue.spec.ts | 466 +++++++++--------- .../tests/pluginApi/createPluginApi.spec.ts | 74 ++- .../normalizeAliasDefineHook.spec.ts | 42 +- .../normalizeClientFilesHook.spec.ts | 72 ++- .../src/plugins/assetsPlugin/assetsPlugin.ts | 2 +- 23 files changed, 832 insertions(+), 848 deletions(-) diff --git a/packages/core/src/types/page.ts b/packages/core/src/types/page.ts index 5aadd8d7ae..178fec9782 100644 --- a/packages/core/src/types/page.ts +++ b/packages/core/src/types/page.ts @@ -5,11 +5,14 @@ import type { PageBase, PageData, PageFrontmatter } from '@vuepress/shared' * Vuepress Page */ export type Page< - ExtraPageData extends Record = Record, - ExtraPageFrontmatter extends Record = Record, - ExtraPageFields extends Record = Record, -> = PageBase & - ExtraPageFields & { + ExtraPageData extends Record = Record, + ExtraPageFrontmatter extends Record = Record< + string, + unknown + >, + ExtraPageFields extends Record = Record, +> = ExtraPageFields & + PageBase & { /** * Data of the page, which will be available in client code */ diff --git a/packages/core/src/types/plugin.ts b/packages/core/src/types/plugin.ts index c7f6bc39ca..ae52a6b334 100644 --- a/packages/core/src/types/plugin.ts +++ b/packages/core/src/types/plugin.ts @@ -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 | PluginFunction + | T /** * Vuepress plugin function diff --git a/packages/core/src/types/pluginApi/hooks.ts b/packages/core/src/types/pluginApi/hooks.ts index 4e88851256..f2b2388f87 100644 --- a/packages/core/src/types/pluginApi/hooks.ts +++ b/packages/core/src/types/pluginApi/hooks.ts @@ -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 ? V : U - : void, + : never, > { exposed: Exposed normalized: Normalized @@ -41,9 +41,9 @@ export type ClientConfigFileHook = Hook< // alias and define hook export type AliasDefineHook = Hook< - | Record - | ((app: App, isServer: boolean) => PromiseOrNot>), - (app: App, isServer: boolean) => Promise> + | Record + | ((app: App, isServer: boolean) => PromiseOrNot>), + (app: App, isServer: boolean) => Promise> > /** @@ -58,7 +58,7 @@ export interface Hooks { extendsMarkdown: ExtendsHook extendsPageOptions: ExtendsHook extendsPage: ExtendsHook - extendsBundlerOptions: ExtendsHook + extendsBundlerOptions: ExtendsHook> clientConfigFile: ClientConfigFileHook alias: AliasDefineHook define: AliasDefineHook diff --git a/packages/core/tests/app/resolvePluginObject.spec.ts b/packages/core/tests/app/resolvePluginObject.spec.ts index 02d2441e81..d359120252 100644 --- a/packages/core/tests/app/resolvePluginObject.spec.ts +++ b/packages/core/tests/app/resolvePluginObject.spec.ts @@ -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', () => { @@ -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', })) diff --git a/packages/core/tests/page/createPage.spec.ts b/packages/core/tests/page/createPage.spec.ts index e2f832b2ab..313fed3876 100644 --- a/packages/core/tests/page/createPage.spec.ts +++ b/packages/core/tests/page/createPage.spec.ts @@ -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() @@ -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', diff --git a/packages/core/tests/page/inferPagePath.spec.ts b/packages/core/tests/page/inferPagePath.spec.ts index 42eef99279..f65aced2ad 100644 --- a/packages/core/tests/page/inferPagePath.spec.ts +++ b/packages/core/tests/page/inferPagePath.spec.ts @@ -51,41 +51,39 @@ const TEST_CASES: [string, ReturnType][] = [ ], ] -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: '/', }) }) diff --git a/packages/core/tests/page/renderPageContent.spec.ts b/packages/core/tests/page/renderPageContent.spec.ts index 055beaf0a1..c4c3e756fd 100644 --- a/packages/core/tests/page/renderPageContent.spec.ts +++ b/packages/core/tests/page/renderPageContent.spec.ts @@ -1,155 +1,154 @@ import { createMarkdown } from '@vuepress/markdown' import { path } from '@vuepress/utils' import { describe, expect, it } from 'vitest' +import type { Bundler } from '../../src/index.js' import { createBaseApp, renderPageContent } from '../../src/index.js' const app = createBaseApp({ source: path.resolve(__dirname, 'fake-source'), theme: { name: 'test' }, - bundler: {} as any, + bundler: {} as Bundler, }) app.markdown = createMarkdown() -describe('core > page > renderPageContent', () => { - it('should render page content correctly', () => { - const resolved = renderPageContent({ - app, - content: `\ +it('should render page content correctly', () => { + const resolved = renderPageContent({ + app, + content: `\ foobar `, - filePath: app.dir.source('foo.md'), - filePathRelative: 'foo.md', - options: {}, - }) + filePath: app.dir.source('foo.md'), + filePathRelative: 'foo.md', + options: {}, + }) - expect(resolved).toEqual({ - contentRendered: '

foobar

\n', - deps: [], - frontmatter: {}, - headers: [], - links: [], - markdownEnv: { excerpt: '' }, - sfcBlocks: { - template: { - type: 'template', - content: '', - contentStripped: '

foobar

\n', - tagClose: '', - tagOpen: '', + tagOpen: '