Skip to content

Commit be249ec

Browse files
committed
refactor(core): improve app types and app creation
1 parent 427e3cc commit be249ec

File tree

6 files changed

+54
-49
lines changed

6 files changed

+54
-49
lines changed

packages/core/src/app/createBaseApp.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { createPluginApi } from '../pluginApi/index.js'
2-
import type { App, AppConfig, Plugin } from '../types/index.js'
2+
import type {
3+
App,
4+
AppConfig,
5+
AppPropertiesBase,
6+
Plugin,
7+
} from '../types/index.js'
38
import { appInit } from './appInit.js'
49
import { appPrepare } from './appPrepare.js'
510
import { appUse } from './appUse.js'
@@ -14,10 +19,10 @@ import { setupAppThemeAndPlugins } from './setupAppThemeAndPlugins.js'
1419
/**
1520
* Create vuepress app
1621
*/
17-
export const createBaseApp = (config: AppConfig, isBuild = false): App => {
22+
export const createBaseApp = (config: AppConfig): App => {
1823
const options = resolveAppOptions(config)
1924
const dir = resolveAppDir(options)
20-
const env = resolveAppEnv(options, isBuild)
25+
const env = resolveAppEnv(options)
2126
const pluginApi = createPluginApi()
2227
const siteData = resolveAppSiteData(options)
2328
const version = resolveAppVersion()
@@ -38,7 +43,7 @@ export const createBaseApp = (config: AppConfig, isBuild = false): App => {
3843
use: (plugin: Plugin) => appUse(app, plugin),
3944
init: async () => appInit(app),
4045
prepare: async () => appPrepare(app),
41-
} as App
46+
} satisfies AppPropertiesBase as App
4247

4348
// setup theme and plugins
4449
// notice that we setup theme before plugins,

packages/core/src/app/createBuildApp.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { createBaseApp } from './createBaseApp.js'
55
* Create vuepress build app
66
*/
77
export const createBuildApp = (config: AppConfig): BuildApp => {
8-
const app = createBaseApp(config, true) as BuildApp
8+
const app = createBaseApp(config) as BuildApp
9+
10+
// set env flag and add build method
11+
app.env.isBuild = true
912
app.build = async () => app.options.bundler.build(app)
13+
1014
return app
1115
}

packages/core/src/app/createDevApp.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { createBaseApp } from './createBaseApp.js'
55
* Create vuepress dev app
66
*/
77
export const createDevApp = (config: AppConfig): DevApp => {
8-
const app = createBaseApp(config, false) as DevApp
8+
const app = createBaseApp(config) as DevApp
9+
10+
// set env flag and add dev method
11+
app.env.isDev = true
912
app.dev = async () => app.options.bundler.dev(app)
13+
1014
return app
1115
}

packages/core/src/app/resolveAppEnv.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import type { AppEnv, AppOptions } from '../types/index.js'
33
/**
44
* Resolve environment flags for vuepress app
55
*/
6-
export const resolveAppEnv = (
7-
options: AppOptions,
8-
isBuild: boolean,
9-
): AppEnv => ({
10-
isBuild,
11-
isDev: !isBuild,
6+
export const resolveAppEnv = (options: AppOptions): AppEnv => ({
7+
isBuild: false,
8+
isDev: false,
129
isDebug: options.debug,
1310
})

packages/core/src/types/app/app.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,39 @@ import type { AppOptions } from './options.js'
88
import type { AppDir, AppEnv, AppWriteTemp } from './utils.js'
99

1010
/**
11-
* Vuepress app
11+
* App base properties, will be available after creation, even before initialization
1212
*/
13-
export interface App {
13+
export interface AppPropertiesBase {
1414
/**
15-
* Directory utils
15+
* Directory utils.
1616
*/
1717
dir: AppDir
1818

1919
/**
20-
* Environment flags
20+
* Environment flags.
2121
*/
2222
env: AppEnv
2323

2424
/**
25-
* Options that filled all optional fields with a default value
25+
* Options that filled all optional fields with a default value.
2626
*/
2727
options: AppOptions
2828

2929
/**
30-
* Plugin system
30+
* Plugin system.
3131
*/
3232
pluginApi: PluginApi
3333

3434
/**
35-
* Site data, which will be used in client side
35+
* Site data, which will be used in client side.
3636
*/
3737
siteData: SiteData
3838

3939
/**
40-
* Version of vuepress core
40+
* Version of vuepress core.
4141
*/
4242
version: string
4343

44-
/**
45-
* Write temp file
46-
*/
47-
writeTemp: AppWriteTemp
48-
49-
/**
50-
* Use a plugin
51-
*/
52-
use: (plugin: Plugin) => this
53-
5444
/**
5545
* Initialize app.
5646
*
@@ -66,6 +56,23 @@ export interface App {
6656
*/
6757
prepare: () => Promise<void>
6858

59+
/**
60+
* Use a plugin.
61+
*
62+
* Should be called before `app.init()`.
63+
*/
64+
use: (plugin: Plugin) => this
65+
66+
/**
67+
* Util to write temp file
68+
*/
69+
writeTemp: AppWriteTemp
70+
}
71+
72+
/**
73+
* App initialized properties, will only be available after initialization
74+
*/
75+
export interface AppPropertiesInitialized {
6976
/**
7077
* Markdown-it instance.
7178
*
@@ -81,6 +88,11 @@ export interface App {
8188
pages: Page[]
8289
}
8390

91+
/**
92+
* Vuepress app instance
93+
*/
94+
export type App = AppPropertiesBase & AppPropertiesInitialized
95+
8496
/**
8597
* Vuepress dev app
8698
*/

packages/core/tests/app/resolveAppEnv.spec.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ const TEST_CASES: [
1313
theme: { name: 'test' },
1414
bundler: {} as Bundler,
1515
}),
16-
false,
1716
],
1817
{
1918
isBuild: false,
20-
isDev: true,
19+
isDev: false,
2120
isDebug: false,
2221
},
2322
],
@@ -29,27 +28,11 @@ const TEST_CASES: [
2928
bundler: {} as Bundler,
3029
debug: true,
3130
}),
32-
false,
3331
],
3432
{
3533
isBuild: false,
36-
isDev: true,
37-
isDebug: true,
38-
},
39-
],
40-
[
41-
[
42-
resolveAppOptions({
43-
source: '/foo',
44-
theme: { name: 'test' },
45-
bundler: {} as Bundler,
46-
}),
47-
true,
48-
],
49-
{
50-
isBuild: true,
5134
isDev: false,
52-
isDebug: false,
35+
isDebug: true,
5336
},
5437
],
5538
]

0 commit comments

Comments
 (0)