Skip to content

Commit

Permalink
chore: soft Vite 6 API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Feb 6, 2025
1 parent eaae085 commit ff0b99d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 4 additions & 1 deletion vike/node/api/build.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export { build }

import { prepareViteApiCall } from './prepareViteApiCall.js'
import { build as buildVite, createBuilder } from 'vite'
import { build as buildVite, version } from 'vite'
import type { APIOptions } from './types.js'
import assert from 'assert'
import { isVikeCli } from '../cli/context.js'
import { isPrerendering } from '../prerender/context.js'
import { assertVersion } from '../../utils/assertVersion.js'

/**
* Programmatically trigger `$ vike build`
Expand All @@ -26,6 +27,8 @@ async function build(options: APIOptions = {}): Promise<{}> {
// > We purposely don't start the pre-rendering in this `build()` function but in a Rollup hook instead.
// > Rationale: https://github.com/vikejs/vike/issues/2123
if (vikeConfig.global.config.viteEnvironmentAPI) {
assertVersion('Vite', version, '6.0.0')
const { createBuilder } = await import('vite')
const builder = await createBuilder(viteConfigEnhanced)
await builder.buildApp()
} else {
Expand Down
4 changes: 2 additions & 2 deletions vike/node/plugin/plugins/buildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { prependEntriesDir } from '../../shared/prependEntriesDir.js'
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 { vite6IsSSR, viteIsSSR } from '../shared/viteIsSSR.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 @@ -109,7 +109,7 @@ function buildConfig(): Plugin[] {
order: 'pre',
sequential: true,
async handler(options, bundle) {
if (isSsrBuild || this.environment.name === 'ssr') {
if (isSsrBuild || vite6IsSSR(this)) {
console.log('FIXING SERVER ASSETS')
// Ideally we'd move dist/_temp_manifest.json to dist/server/client-assets.json instead of dist/assets.json
// - But we can't because there is no guarentee whether dist/server/ is generated before or after dist/client/ (generating dist/server/ after dist/client/ erases dist/server/client-assets.json)
Expand Down
5 changes: 5 additions & 0 deletions vike/node/plugin/shared/viteIsSSR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { assert } from '../../../utils/assert.js'
import { isObject } from '../../../utils/isObject.js'

export { viteIsSSR }
export { vite6IsSSR }
export { viteIsSSR_options }

function viteIsSSR(config: { build?: { ssr?: boolean | string } }): boolean {
return !!config?.build?.ssr
}

function vite6IsSSR<T extends { environment: { name: string } }>(self: T): boolean {
return 'environment' in self && self.environment.name === 'ssr'
}

type Options = undefined | boolean | { ssr?: boolean }
// https://github.com/vitejs/vite/discussions/5109#discussioncomment-1450726
function viteIsSSR_options(options: Options): boolean {
Expand Down

0 comments on commit ff0b99d

Please sign in to comment.