Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add compatibility with Vite 6 Environment API #2146

Merged
merged 41 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a1d829b
test: env API test files
magne4000 Feb 6, 2025
9445ff3
feat: experimental support for Vite 6 Environment API
magne4000 Feb 6, 2025
7a99b22
chore: improve compat with env API
magne4000 Feb 6, 2025
0255dde
chore: improve compat with env API
magne4000 Feb 6, 2025
7068cfb
chore: include pre-rendering with Env API
magne4000 Feb 6, 2025
318057e
test: simplify tests
magne4000 Feb 6, 2025
4658f91
test: force NODE_ENV
magne4000 Feb 6, 2025
0da19cb
refactor: rename useEnvironmentAPI to viteEnvironmentAPI
magne4000 Feb 6, 2025
12d7307
chore: undo import reordering
magne4000 Feb 6, 2025
eaae085
chore: format
magne4000 Feb 6, 2025
ff0b99d
chore: soft Vite 6 API usage
magne4000 Feb 6, 2025
2dd7a69
chore: pnpm-lock
magne4000 Feb 6, 2025
962da77
chore: biome ignore vercel outdir
magne4000 Feb 6, 2025
bc93c68
Merge remote-tracking branch 'origin/main' into magne4000/environment…
brillout Feb 6, 2025
13b7b37
update to `config.vike!.config`
brillout Feb 6, 2025
0c32b30
refactor: cleanup
magne4000 Feb 7, 2025
542327f
refactor: cleanup
magne4000 Feb 7, 2025
05f7a9c
Merge remote-tracking branch 'origin/main' into magne4000/environment…
brillout Feb 9, 2025
d67bd95
dedupe
brillout Feb 9, 2025
0e84554
minor refactor: getOutDirs
brillout Feb 9, 2025
82ef00b
minor refactor: getOutDirsAll()
brillout Feb 9, 2025
acdccff
minor refactor: normalizeOutDir
brillout Feb 9, 2025
0956607
fix `isSSR` option of `resolveOutDir()`
brillout Feb 9, 2025
1240430
fix: dedupe pre-render chaining
brillout Feb 9, 2025
60d65b9
remove superfluous fallback
brillout Feb 9, 2025
8419bdc
minor refactor: remove `let isSsrBuild: boolean`
brillout Feb 9, 2025
475b7c4
minor refactor: viteIsSSR()
brillout Feb 9, 2025
9a21f2b
refactor vite6IsSSR() implementation
brillout Feb 9, 2025
cb0c3e2
debug
brillout Feb 9, 2025
56f30ce
Revert "debug"
brillout Feb 9, 2025
d86892d
Revert "refactor vite6IsSSR() implementation"
brillout Feb 9, 2025
d296d91
remove superfluous vite6IsSSR()
brillout Feb 9, 2025
77b5951
fmt manual
brillout Feb 9, 2025
05075ac
minor refactor
brillout Feb 9, 2025
24fb039
comment
brillout Feb 9, 2025
46bdd9e
minor refactor
brillout Feb 9, 2025
52a82d0
minor refactor
brillout Feb 9, 2025
54e1409
minor refactor
brillout Feb 9, 2025
4a2b22f
dedupe
brillout Feb 9, 2025
96f2006
minor refactor
brillout Feb 9, 2025
904feac
comment
brillout Feb 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"files": {
"ignore": ["dist/", "package.json"]
"ignore": ["dist/", "package.json", ".vercel/"]
},
"formatter": {
"indentWidth": 2,
Expand Down
82 changes: 29 additions & 53 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/environment-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
1 change: 1 addition & 0 deletions test/environment-api/.testCiJob.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "name": "Unit Tests E2E" }
16 changes: 16 additions & 0 deletions test/environment-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"scripts": {
"dev": "vike dev",
"build": "vike build"
},
"dependencies": {
"@types/react": "^18.0.8",
"@vitejs/plugin-react": "^4.3.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"vike": "0.4.220",
"vike-react": "^0.5.12",
"vite": "^6.0.5"
},
"type": "module"
}
2 changes: 2 additions & 0 deletions test/environment-api/pages/+config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import vikeReact from 'vike-react/config'
export default { viteEnvironmentAPI: true, prerender: true, extends: vikeReact }
13 changes: 13 additions & 0 deletions test/environment-api/pages/index/+Page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default Page

import React from 'react'
import { Counter } from './Counter.jsx'

function Page() {
return (
<>
<h1>Test page</h1>
<Counter />
</>
)
}
9 changes: 9 additions & 0 deletions test/environment-api/pages/index/Counter.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { useState } from 'react'

export { Counter }

function Counter() {
const [count, setCount] = useState(0)

return <button onClick={() => setCount((count) => count + 1)}>Counter {count}</button>
}
20 changes: 20 additions & 0 deletions test/environment-api/test-build.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { build } from 'vike/api'
import { describe, expect, test } from 'vitest'

describe('build', () => {
test('prevented', { timeout: 20 * 1000 }, async () => {
await buildApp()
expect('success').toBe('success')
})
})

async function buildApp() {
process.env.NODE_ENV = 'production'
await build({
viteConfig: {
logLevel: 'warn',
root: __dirname,
configFile: __dirname + '/vite.config.js'
}
})
}
10 changes: 10 additions & 0 deletions test/environment-api/test-dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, getServerUrl, page, run, test } from '@brillout/test-e2e'
import { testCounter } from '../../test/utils'

run('npm run dev', { doNotFailOnWarning: true })

test('forbidden import', async () => {
await page.goto(getServerUrl() + '/')
await testCounter()
expect('success').toBe('success')
})
3 changes: 3 additions & 0 deletions test/environment-api/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import react from '@vitejs/plugin-react'
import vike from 'vike/plugin'
export default { plugins: [react(), vike()] }
34 changes: 21 additions & 13 deletions vike/node/api/build.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
export { build }

import { prepareViteApiCall } from './prepareViteApiCall.js'
import { build as buildVite } 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.js'

/**
* Programmatically trigger `$ vike build`
*
* https://vike.dev/api#build
*/
async function build(options: APIOptions = {}): Promise<{}> {
const { viteConfigEnhanced } = await prepareViteApiCall(options.viteConfig, 'build')
const { viteConfigEnhanced, vikeConfig } = await prepareViteApiCall(options.viteConfig, 'build')

// Pass it to autoFullBuild()
if (viteConfigEnhanced) viteConfigEnhanced._viteConfigEnhanced = viteConfigEnhanced

// 1. Build client-side
// 2. Build server-side
// > See: https://github.com/vikejs/vike/blob/c6c7533a56b3a16fc43ed644fc5c10c02d0ff375/vike/node/plugin/plugins/autoFullBuild.ts#L90
// 3. Pre-render (if enabled)
// > See: https://github.com/vikejs/vike/blob/c6c7533a56b3a16fc43ed644fc5c10c02d0ff375/vike/node/plugin/plugins/autoFullBuild.ts#L98
// > 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
await buildVite(viteConfigEnhanced)

// When using the Vike CLI with pre-rendering the process is forcefully exited at the end of the buildVite() call above
assert(!(isVikeCli() && isPrerendering()))
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 {
// This buildVite() call does everything:
// 1. Build client-side
// 2. Build server-side
// > See: https://github.com/vikejs/vike/blob/c6c7533a56b3a16fc43ed644fc5c10c02d0ff375/vike/node/plugin/plugins/autoFullBuild.ts#L90
// 3. Pre-render (if enabled)
// > See: https://github.com/vikejs/vike/blob/c6c7533a56b3a16fc43ed644fc5c10c02d0ff375/vike/node/plugin/plugins/autoFullBuild.ts#L98
// > 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
await buildVite(viteConfigEnhanced)
// After pre-rendering, when using the Vike CLI, the process is forcefully exited at the end of the buildVite() call above.
if (isVikeCli() && isPrerendering()) assert(false)
}

return {
/* We don't return `viteConfig` because `viteConfigEnhanced` is `InlineConfig` not `ResolvedConfig`
Expand Down
1 change: 1 addition & 0 deletions vike/node/api/prepareViteApiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async function enhanceViteConfig(viteConfig: InlineConfig | undefined, operation
const vikeConfig = await getVikeConfig2(viteInfo.root, operation === 'dev', viteInfo.vikeVitePluginOptions)
const viteConfigEnhanced = addViteSettingsSetByVikeConfig(viteInfo.viteConfigEnhanced, vikeConfig)
return {
vikeConfig,
viteConfigEnhanced
}
}
Expand Down
1 change: 1 addition & 0 deletions vike/node/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from '../../utils/assert.js'
export * from '../../utils/getGlobalObject.js'
export * from '../../utils/path.js'
export * from '../../utils/isObject.js'
export * from '../../utils/assertVersion.js'
2 changes: 2 additions & 0 deletions vike/node/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { setResolveClientEntriesDev } from '../runtime/renderPage/getPageAssets.
import { resolveClientEntriesDev } from './shared/resolveClientEntriesDev.js'
import { workaroundCssModuleHmr } from './plugins/workaroundCssModuleHmr.js'
import { workaroundVite6HmrRegression } from './plugins/workaroundVite6HmrRegression.js'
import { buildApp } from './plugins/buildApp.js'

// We don't call this in ./onLoad.ts to avoid a cyclic dependency with utils.ts
setResolveClientEntriesDev(resolveClientEntriesDev)
Expand All @@ -41,6 +42,7 @@ function plugin(vikeVitePluginOptions: VikeVitePluginOptions = {}): any {
importUserCode(),
...devConfig(),
...buildConfig(),
...buildApp(),
previewConfig(),
...autoFullBuild(),
packageJsonFile(),
Expand Down
Loading