Skip to content

Commit

Permalink
refactor(core): improve app types and app creation
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Sep 12, 2024
1 parent 427e3cc commit be249ec
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 49 deletions.
13 changes: 9 additions & 4 deletions packages/core/src/app/createBaseApp.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/app/createBuildApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion packages/core/src/app/createDevApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 3 additions & 6 deletions packages/core/src/app/resolveAppEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
48 changes: 30 additions & 18 deletions packages/core/src/types/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -66,6 +56,23 @@ export interface App {
*/
prepare: () => Promise<void>

/**
* 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.
*
Expand All @@ -81,6 +88,11 @@ export interface App {
pages: Page[]
}

/**
* Vuepress app instance
*/
export type App = AppPropertiesBase & AppPropertiesInitialized

/**
* Vuepress dev app
*/
Expand Down
21 changes: 2 additions & 19 deletions packages/core/tests/app/resolveAppEnv.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ const TEST_CASES: [
theme: { name: 'test' },
bundler: {} as Bundler,
}),
false,
],
{
isBuild: false,
isDev: true,
isDev: false,
isDebug: false,
},
],
Expand All @@ -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,
},
],
]
Expand Down

0 comments on commit be249ec

Please sign in to comment.