From e693cdc6892aa977dc92c37c4869ad86fa5a8d69 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Thu, 10 Oct 2024 09:26:25 +0800 Subject: [PATCH] fix(core): fix app env flag regression (close #1612) --- packages/core/src/app/createBaseApp.ts | 6 ---- packages/core/src/app/createBuildApp.ts | 4 +++ packages/core/src/app/createDevApp.ts | 4 +++ .../core/tests/app/createBuildApp.spec.ts | 29 +++++++++++++++++++ packages/core/tests/app/createDevApp.spec.ts | 29 +++++++++++++++++++ 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 packages/core/tests/app/createBuildApp.spec.ts create mode 100644 packages/core/tests/app/createDevApp.spec.ts diff --git a/packages/core/src/app/createBaseApp.ts b/packages/core/src/app/createBaseApp.ts index dc8b18750d..fd961f3138 100644 --- a/packages/core/src/app/createBaseApp.ts +++ b/packages/core/src/app/createBaseApp.ts @@ -14,7 +14,6 @@ import { resolveAppOptions } from './resolveAppOptions.js' import { resolveAppSiteData } from './resolveAppSiteData.js' import { resolveAppVersion } from './resolveAppVersion.js' import { resolveAppWriteTemp } from './resolveAppWriteTemp.js' -import { setupAppThemeAndPlugins } from './setupAppThemeAndPlugins.js' /** * Create base vuepress app. @@ -49,10 +48,5 @@ export const createBaseApp = (config: AppConfig): App => { prepare: async () => appPrepare(app), } satisfies AppPropertiesBase as App - // setup theme and plugins - // notice that we setup theme before plugins, - // so user plugins could override theme plugins - setupAppThemeAndPlugins(app, config) - return app } diff --git a/packages/core/src/app/createBuildApp.ts b/packages/core/src/app/createBuildApp.ts index 13e1651b8e..4f359595c8 100644 --- a/packages/core/src/app/createBuildApp.ts +++ b/packages/core/src/app/createBuildApp.ts @@ -1,5 +1,6 @@ import type { AppConfig, BuildApp } from '../types/index.js' import { createBaseApp } from './createBaseApp.js' +import { setupAppThemeAndPlugins } from './setupAppThemeAndPlugins.js' /** * Create vuepress build app. @@ -11,5 +12,8 @@ export const createBuildApp = (config: AppConfig): BuildApp => { app.env.isBuild = true app.build = async () => app.options.bundler.build(app) + // setup theme and plugins + setupAppThemeAndPlugins(app, config) + return app } diff --git a/packages/core/src/app/createDevApp.ts b/packages/core/src/app/createDevApp.ts index 3d21ea35ed..104cb9f01a 100644 --- a/packages/core/src/app/createDevApp.ts +++ b/packages/core/src/app/createDevApp.ts @@ -1,5 +1,6 @@ import type { AppConfig, DevApp } from '../types/index.js' import { createBaseApp } from './createBaseApp.js' +import { setupAppThemeAndPlugins } from './setupAppThemeAndPlugins.js' /** * Create vuepress dev app. @@ -11,5 +12,8 @@ export const createDevApp = (config: AppConfig): DevApp => { app.env.isDev = true app.dev = async () => app.options.bundler.dev(app) + // setup theme and plugins + setupAppThemeAndPlugins(app, config) + return app } diff --git a/packages/core/tests/app/createBuildApp.spec.ts b/packages/core/tests/app/createBuildApp.spec.ts new file mode 100644 index 0000000000..4ddca78046 --- /dev/null +++ b/packages/core/tests/app/createBuildApp.spec.ts @@ -0,0 +1,29 @@ +import { expect, it } from 'vitest' +import type { App, BuildApp, Bundler } from '../../src/index.js' +import { createBuildApp } from '../../src/index.js' + +it('should create build app correctly', () => { + const checkApp = (app: App): void => { + expect(app.env.isDev).toBe(false) + expect((app as unknown as { dev: never }).dev).toBeUndefined() + + expect(app.env.isBuild).toBe(true) + expect(typeof (app as BuildApp).build).toBe('function') + } + + const buildApp = createBuildApp({ + source: '/foo', + theme: (appInTheme) => { + checkApp(appInTheme) + return { name: 'test-theme' } + }, + bundler: {} as Bundler, + plugins: [ + (appInPlugin) => { + checkApp(appInPlugin) + return { name: 'test-plugin' } + }, + ], + }) + checkApp(buildApp) +}) diff --git a/packages/core/tests/app/createDevApp.spec.ts b/packages/core/tests/app/createDevApp.spec.ts new file mode 100644 index 0000000000..a932ddf3b6 --- /dev/null +++ b/packages/core/tests/app/createDevApp.spec.ts @@ -0,0 +1,29 @@ +import { expect, it } from 'vitest' +import type { App, Bundler, DevApp } from '../../src/index.js' +import { createDevApp } from '../../src/index.js' + +it('should create dev app correctly', () => { + const checkApp = (app: App): void => { + expect(app.env.isDev).toBe(true) + expect(typeof (app as DevApp).dev).toBe('function') + + expect(app.env.isBuild).toBe(false) + expect((app as unknown as { build: never }).build).toBeUndefined() + } + + const devApp = createDevApp({ + source: '/foo', + theme: (appInTheme) => { + checkApp(appInTheme) + return { name: 'test-theme' } + }, + bundler: {} as Bundler, + plugins: [ + (appInPlugin) => { + checkApp(appInPlugin) + return { name: 'test-plugin' } + }, + ], + }) + checkApp(devApp) +})