From be249ec9ccd6de1438662b980b23ebe61a3954aa Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Fri, 13 Sep 2024 01:03:20 +0800 Subject: [PATCH] refactor(core): improve app types and app creation --- packages/core/src/app/createBaseApp.ts | 13 +++-- packages/core/src/app/createBuildApp.ts | 6 ++- packages/core/src/app/createDevApp.ts | 6 ++- packages/core/src/app/resolveAppEnv.ts | 9 ++-- packages/core/src/types/app/app.ts | 48 ++++++++++++------- packages/core/tests/app/resolveAppEnv.spec.ts | 21 +------- 6 files changed, 54 insertions(+), 49 deletions(-) diff --git a/packages/core/src/app/createBaseApp.ts b/packages/core/src/app/createBaseApp.ts index 3e91a42eff..dc2b2ecaf4 100644 --- a/packages/core/src/app/createBaseApp.ts +++ b/packages/core/src/app/createBaseApp.ts @@ -1,5 +1,10 @@ import { createPluginApi } from '../pluginApi/index.js' -import type { App, AppConfig, Plugin } from '../types/index.js' +import type { + App, + AppConfig, + AppPropertiesBase, + Plugin, +} from '../types/index.js' import { appInit } from './appInit.js' import { appPrepare } from './appPrepare.js' import { appUse } from './appUse.js' @@ -14,10 +19,10 @@ import { setupAppThemeAndPlugins } from './setupAppThemeAndPlugins.js' /** * Create vuepress app */ -export const createBaseApp = (config: AppConfig, isBuild = false): App => { +export const createBaseApp = (config: AppConfig): App => { const options = resolveAppOptions(config) const dir = resolveAppDir(options) - const env = resolveAppEnv(options, isBuild) + const env = resolveAppEnv(options) const pluginApi = createPluginApi() const siteData = resolveAppSiteData(options) const version = resolveAppVersion() @@ -38,7 +43,7 @@ export const createBaseApp = (config: AppConfig, isBuild = false): App => { use: (plugin: Plugin) => appUse(app, plugin), init: async () => appInit(app), prepare: async () => appPrepare(app), - } as App + } satisfies AppPropertiesBase as App // setup theme and plugins // notice that we setup theme before plugins, diff --git a/packages/core/src/app/createBuildApp.ts b/packages/core/src/app/createBuildApp.ts index 36830cbdf8..2bc29eddff 100644 --- a/packages/core/src/app/createBuildApp.ts +++ b/packages/core/src/app/createBuildApp.ts @@ -5,7 +5,11 @@ import { createBaseApp } from './createBaseApp.js' * Create vuepress build app */ export const createBuildApp = (config: AppConfig): BuildApp => { - const app = createBaseApp(config, true) as BuildApp + const app = createBaseApp(config) as BuildApp + + // set env flag and add build method + app.env.isBuild = true app.build = async () => app.options.bundler.build(app) + return app } diff --git a/packages/core/src/app/createDevApp.ts b/packages/core/src/app/createDevApp.ts index 59fe0f2ea0..3a0f4003e6 100644 --- a/packages/core/src/app/createDevApp.ts +++ b/packages/core/src/app/createDevApp.ts @@ -5,7 +5,11 @@ import { createBaseApp } from './createBaseApp.js' * Create vuepress dev app */ export const createDevApp = (config: AppConfig): DevApp => { - const app = createBaseApp(config, false) as DevApp + const app = createBaseApp(config) as DevApp + + // set env flag and add dev method + app.env.isDev = true app.dev = async () => app.options.bundler.dev(app) + return app } diff --git a/packages/core/src/app/resolveAppEnv.ts b/packages/core/src/app/resolveAppEnv.ts index dd7da3011d..7ec4bf96c0 100644 --- a/packages/core/src/app/resolveAppEnv.ts +++ b/packages/core/src/app/resolveAppEnv.ts @@ -3,11 +3,8 @@ import type { AppEnv, AppOptions } from '../types/index.js' /** * Resolve environment flags for vuepress app */ -export const resolveAppEnv = ( - options: AppOptions, - isBuild: boolean, -): AppEnv => ({ - isBuild, - isDev: !isBuild, +export const resolveAppEnv = (options: AppOptions): AppEnv => ({ + isBuild: false, + isDev: false, isDebug: options.debug, }) diff --git a/packages/core/src/types/app/app.ts b/packages/core/src/types/app/app.ts index e943950ec9..a99e8d88bf 100644 --- a/packages/core/src/types/app/app.ts +++ b/packages/core/src/types/app/app.ts @@ -8,49 +8,39 @@ import type { AppOptions } from './options.js' import type { AppDir, AppEnv, AppWriteTemp } from './utils.js' /** - * Vuepress app + * App base properties, will be available after creation, even before initialization */ -export interface App { +export interface AppPropertiesBase { /** - * Directory utils + * Directory utils. */ dir: AppDir /** - * Environment flags + * Environment flags. */ env: AppEnv /** - * Options that filled all optional fields with a default value + * Options that filled all optional fields with a default value. */ options: AppOptions /** - * Plugin system + * Plugin system. */ pluginApi: PluginApi /** - * Site data, which will be used in client side + * Site data, which will be used in client side. */ siteData: SiteData /** - * Version of vuepress core + * Version of vuepress core. */ version: string - /** - * Write temp file - */ - writeTemp: AppWriteTemp - - /** - * Use a plugin - */ - use: (plugin: Plugin) => this - /** * Initialize app. * @@ -66,6 +56,23 @@ export interface App { */ prepare: () => Promise + /** + * Use a plugin. + * + * Should be called before `app.init()`. + */ + use: (plugin: Plugin) => this + + /** + * Util to write temp file + */ + writeTemp: AppWriteTemp +} + +/** + * App initialized properties, will only be available after initialization + */ +export interface AppPropertiesInitialized { /** * Markdown-it instance. * @@ -81,6 +88,11 @@ export interface App { pages: Page[] } +/** + * Vuepress app instance + */ +export type App = AppPropertiesBase & AppPropertiesInitialized + /** * Vuepress dev app */ diff --git a/packages/core/tests/app/resolveAppEnv.spec.ts b/packages/core/tests/app/resolveAppEnv.spec.ts index 6ba685baa6..8f0a8e2010 100644 --- a/packages/core/tests/app/resolveAppEnv.spec.ts +++ b/packages/core/tests/app/resolveAppEnv.spec.ts @@ -13,11 +13,10 @@ const TEST_CASES: [ theme: { name: 'test' }, bundler: {} as Bundler, }), - false, ], { isBuild: false, - isDev: true, + isDev: false, isDebug: false, }, ], @@ -29,27 +28,11 @@ const TEST_CASES: [ bundler: {} as Bundler, debug: true, }), - false, ], { isBuild: false, - isDev: true, - isDebug: true, - }, - ], - [ - [ - resolveAppOptions({ - source: '/foo', - theme: { name: 'test' }, - bundler: {} as Bundler, - }), - true, - ], - { - isBuild: true, isDev: false, - isDebug: false, + isDebug: true, }, ], ]