Skip to content

Commit

Permalink
feat: getVikeConfig() (#2194)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout authored Feb 18, 2025
1 parent 69f80f4 commit 5a81b60
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 23 deletions.
4 changes: 4 additions & 0 deletions docs/headingsDetached.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ function api(): HeadingDetachedDefinition[] {
title: '`getPageContextClient()`',
url: '/getPageContextClient'
},
{
title: '`getVikeConfig()`',
url: '/getVikeConfig'
},
{
title: 'HTTP Headers',
url: '/headers'
Expand Down
4 changes: 3 additions & 1 deletion docs/pages/getGlobalContext/+Page.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Warning, Link } from '@brillout/docpress'

<Warning>`getGlobalContextSync()` and `getGlobalContextAsync()` are beta features: expect breaking changes in any minor version update.</Warning>
Environment: server.

`getGlobalContextSync()` and `getGlobalContextAsync()` allow you to access global information about your app.

<Warning>It's a beta feature: expect breaking changes in any minor version update.</Warning>

Most notably the <Link href="/preloading#assets-manifest">assets manifest</Link>.

```js
Expand Down
47 changes: 47 additions & 0 deletions docs/pages/getVikeConfig/+Page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Warning, Link } from '@brillout/docpress'

Environment: Vite.

Get all the information Vike knows about the app in your Vite plugin.

<Warning>It's a beta feature: expect breaking changes in any minor version update.</Warning>

> It provides all configurations loaded at build-time set by <Link href="/config#files">`+` files</Link> and <Link href="/extensions">Vike extensions</Link>.
>
> You can also use `globalContext.config` and `globalContext.pages` to get all configurations loaded at runtime, see <Link href="/getGlobalContext" />.
```js
// vite.config.js

import { getVikeConfig } from 'vike/plugin'

export default {
plugins: [myVitePlugin()]
}

function myVitePlugin() {
return {
name: 'myVitePlugin',
configResolved(config) {
const vike = getVikeConfig(config)
console.log(vike.config.prerender)
console.log(vike.config.baseAssets)
console.log(vike.config.baseServer)
console.log(vike.pages['/pages/index'].config)
console.log(vike.pages['/pages/product'].route)
// ...
},
config(config) {
// Also available here
const vike = getVikeConfig(config)
}
}
}
```

## See also

- <Link href="/config#files" />
- <Link href="/getGlobalContext" />
- [Vite > Plugin API > `configResolved`](https://vite.dev/guide/api-plugin.html#configresolved)
- [Vite > Plugin API > `config`](https://vite.dev/guide/api-plugin.html#config)
14 changes: 8 additions & 6 deletions test/playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import react from '@vitejs/plugin-react'
import assert from 'node:assert'
import { getVikeConfig } from 'vike/plugin'
import type { PluginOption } from 'vite'

export default {
Expand All @@ -14,12 +15,13 @@ export default {
function testPlugin(): PluginOption {
return {
name: 'testPlugin',
configResolved(config: any) {
assert(config.vike.config)
assert(config.vike.config.prerender[0].noExtraDir)
assert(config.vike.pages)
assert(config.vike.pages['/pages/index'].config.prerender[0] === false)
assert(config.vike.pages['/pages/markdown'].config.prerender[0])
configResolved(config) {
const vike = getVikeConfig(config as any)
assert(typeof vike.config.prerender![0] === 'object')
assert(vike.config.prerender![0].noExtraDir)
assert(vike.pages)
assert(vike.pages['/pages/index']!.config.prerender![0] === false)
assert(vike.pages['/pages/markdown']!.config.prerender![0])
}
}
}
3 changes: 2 additions & 1 deletion vike/node/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ export default plugin
export { plugin }
// TODO/v1-release: remove
export { plugin as ssr }
export { getVikeConfigPublic as getVikeConfig } from './plugins/commonConfig.js'
export { PROJECT_VERSION as version } from './utils.js'
export type { VikeVitePluginOptions as UserConfig }
export type { VikeVitePluginOptions }
export { PROJECT_VERSION as version } from './utils.js'

import type { Plugin } from 'vite'
import { assertUsage } from './utils.js'
Expand Down
8 changes: 3 additions & 5 deletions vike/node/plugin/plugins/baseUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Plugin, ResolvedConfig } from 'vite'
import { resolveBase } from '../../shared/resolveBase.js'
import { assert } from '../utils.js'
import { getVikeConfig } from './importUserCode/v1-design/getVikeConfig.js'
import { getVikeConfigPublic } from './commonConfig.js'

function baseUrls(): Plugin {
let basesResolved: ReturnType<typeof resolveBase>
Expand All @@ -14,11 +15,8 @@ function baseUrls(): Plugin {
const isDev = config._isDev
assert(typeof isDev === 'boolean')
const baseViteOriginal = config.base ?? '/__UNSET__' // '/__UNSET__' because Vite resolves `_baseViteOriginal: null` to `undefined`
basesResolved = resolveBase(
baseViteOriginal,
config.vike!.config.baseServer ?? null,
config.vike!.config.baseAssets ?? null
)
const vike = getVikeConfigPublic(config)
basesResolved = resolveBase(baseViteOriginal, vike.config.baseServer ?? null, vike.config.baseAssets ?? null)
// We cannot define these in configResolved() because Vite picks up the env variables before any configResolved() hook is called
process.env.BASE_SERVER = basesResolved.baseServer
process.env.BASE_ASSETS = basesResolved.baseAssets
Expand Down
7 changes: 5 additions & 2 deletions vike/node/plugin/plugins/buildApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { resolveOutDir } from '../shared/getOutDirs.js'
import { assert } from '../utils.js'
import { getVikeConfig } from './importUserCode/v1-design/getVikeConfig.js'
import { getFullBuildInlineConfig } from '../shared/getFullBuildInlineConfig.js'
import { getVikeConfigPublic } from './commonConfig.js'

function buildApp(): Plugin[] {
let config: ResolvedConfig
Expand All @@ -20,7 +21,8 @@ function buildApp(): Plugin[] {
name: 'vike:buildApp',
apply: 'build',
config(config) {
if (!config.vike!.config.viteEnvironmentAPI) return
const vike = getVikeConfigPublic(config)
if (!vike.config.viteEnvironmentAPI) return

return {
builder: {
Expand Down Expand Up @@ -66,7 +68,8 @@ function buildApp(): Plugin[] {
config = _config
},
async writeBundle() {
if (!config.vike!.config.viteEnvironmentAPI) return
const vike = getVikeConfigPublic(config)
if (!vike.config.viteEnvironmentAPI) return

const vikeConfig = await getVikeConfig(config)
if (!isPrerenderAutoRunEnabled(vikeConfig)) return
Expand Down
4 changes: 3 additions & 1 deletion vike/node/plugin/plugins/buildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { getFilePathResolved } from '../shared/getFilePath.js'
import { getConfigValueBuildTime } from '../../../shared/page-configs/getConfigValueBuildTime.js'
import { getOutDirs, type OutDirs, resolveOutDir } from '../shared/getOutDirs.js'
import { viteIsSSR } from '../shared/viteIsSSR.js'
import { getVikeConfigPublic } from './commonConfig.js'
// @ts-ignore Shimmed by dist-cjs-fixup.js for CJS build.
const importMetaUrl: string = import.meta.url
const require_ = createRequire(importMetaUrl)
Expand Down Expand Up @@ -77,11 +78,12 @@ function buildConfig(): Plugin[] {
order: 'post',
handler(config) {
onSetupBuild()
const vike = getVikeConfigPublic(config)
return {
build: {
outDir: resolveOutDir(config),
manifest: manifestTempFile,
copyPublicDir: config.vike!.config.viteEnvironmentAPI
copyPublicDir: vike.config.viteEnvironmentAPI
? // Already set by buildApp() plugin
undefined
: !viteIsSSR(config)
Expand Down
31 changes: 25 additions & 6 deletions vike/node/plugin/plugins/commonConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { commonConfig }
export { getVikeConfigPublic }

import { type InlineConfig, mergeConfig, type Plugin, type ResolvedConfig, type UserConfig } from 'vite'
import {
Expand Down Expand Up @@ -33,14 +34,16 @@ declare module 'vite' {
_baseViteOriginal?: string
// We'll be able to remove once we have one Rolldown build instead of two Rollup builds
_viteConfigEnhanced?: InlineConfig
vike?: {
config: VikeConfigObject['global']['config']
pages: VikeConfigObject['pages']
prerenderContext?: PrerenderContextPublic
}
_vike?: VikeConfigPublic
}
}

type VikeConfigPublic = {
config: VikeConfigObject['global']['config']
pages: VikeConfigObject['pages']
prerenderContext?: PrerenderContextPublic
}

function commonConfig(vikeVitePluginOptions: unknown): Plugin[] {
return [
{
Expand All @@ -58,7 +61,7 @@ function commonConfig(vikeVitePluginOptions: unknown): Plugin[] {
_isDev: isDev,
_root: root,
_vikeVitePluginOptions: vikeVitePluginOptions,
vike: {
_vike: {
pages: vikeConfig.pages,
config: vikeConfig.global.config
},
Expand Down Expand Up @@ -217,3 +220,19 @@ function temp_supportOldInterface(config: ResolvedConfig) {
}
assert(false)
}

// TODO/soon rename:
// - `getVikeConfig()` => `resolveVikeConfig()` ?
// - `getVikeConfigPublic()` => `getVikeConfig()`
// - `VikeConfigPublic` => `VikeConfig` ?
// - `VikeConfigObject` => `VikeConfigInternal` ?
/**
* Get all the information Vike knows about the app in your Vite plugin.
*
* https://vike.dev/getVikeConfig
*/
function getVikeConfigPublic(config: ResolvedConfig | UserConfig): VikeConfigPublic {
const vikeConfig = config._vike
assert(vikeConfig)
return vikeConfig
}
4 changes: 3 additions & 1 deletion vike/node/prerender/runPrerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { resolvePrerenderConfig, resolvePrerenderConfigLocal } from './resolvePr
import { getOutDirs } from '../plugin/shared/getOutDirs.js'
import { isVikeCli } from '../cli/context.js'
import { isViteCliCall } from '../plugin/shared/isViteCliCall.js'
import { getVikeConfigPublic } from '../plugin/plugins/commonConfig.js'

type HtmlFile = {
urlOriginal: string
Expand Down Expand Up @@ -191,7 +192,8 @@ async function runPrerenderFromAutoRun(
logErrorHint(err)
process.exit(1)
}
config.vike!.prerenderContext = prerenderContextPublic
const vike = getVikeConfigPublic(config)
vike.prerenderContext = prerenderContextPublic
const forceExit = isVikeCli() || isViteCliCall()
return { forceExit }
}
Expand Down

0 comments on commit 5a81b60

Please sign in to comment.