Skip to content

Commit

Permalink
fix(core): fix app env flag regression (close #1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Oct 10, 2024
1 parent 0f91675 commit e693cdc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
6 changes: 0 additions & 6 deletions packages/core/src/app/createBaseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions packages/core/src/app/createBuildApp.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions packages/core/src/app/createDevApp.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
29 changes: 29 additions & 0 deletions packages/core/tests/app/createBuildApp.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
29 changes: 29 additions & 0 deletions packages/core/tests/app/createDevApp.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit e693cdc

Please sign in to comment.