Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(schema, vite)!: enable vite-node by default #6217

Merged
merged 14 commits into from
Aug 13, 2022
24 changes: 18 additions & 6 deletions packages/schema/src/config/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ export default {
},

/**
* Use `vite-node` for on-demand server chunk loading.
*/
viteNode: process.env.EXPERIMENTAL_VITE_NODE ? true : false,

/**
* Enable Vue's reactivity transform.
* Enable Vue's reactivity transform
* @see https://vuejs.org/guide/extras/reactivity-transform.html
*/
reactivityTransform: false,
Expand All @@ -31,6 +26,23 @@ export default {
*/
treeshakeClientOnly: false,

/**
* Use vite-node for on-demand server chunk loading
*
* @deprecated use `vite.serverBundler: 'vite-node'`
*/
viteNode: {
$resolve: (val) => {
val = process.env.EXPERIMENTAL_VITE_NODE ? true : val
if (val === true) {
console.warn('`vite-node` is now enabled by default. You can safely remove `experimental.viteNode` from your config.')
antfu marked this conversation as resolved.
Show resolved Hide resolved
} else if (val === false) {
console.warn('`vite-node` is now enabled by default. To disable it, set `vite.serverBundler` to `legacy` instead.')
}
return val ?? true
}
},

/**
* Split server bundle into multiple chunks and dynamically import them.
*
Expand Down
5 changes: 5 additions & 0 deletions packages/schema/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ export interface ViteConfig extends ViteUserConfig {
* @see https://github.com/vitejs/vite/tree/main/packages/plugin-vue
*/
vue?: VuePluginOptions
/**
* Bundler for dev time server-side rendering.
* @default 'vite-node'
*/
serverBundler?: 'vite-node' | 'legacy'
pi0 marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 2 additions & 3 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { wpfs } from './utils/wpfs'
import type { ViteBuildContext, ViteOptions } from './vite'
import { writeManifest } from './manifest'
import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
import { viteNodePlugin } from './vite-node'

export async function buildClient (ctx: ViteBuildContext) {
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
Expand Down Expand Up @@ -48,9 +49,7 @@ export async function buildClient (ctx: ViteBuildContext) {
srcDir: ctx.nuxt.options.srcDir,
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir)
}),
ctx.nuxt.options.experimental.viteNode
? await import('./vite-node').then(r => r.viteNodePlugin(ctx))
: undefined
viteNodePlugin(ctx)
],
appType: 'custom',
server: {
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo'
import { ViteBuildContext, ViteOptions } from './vite'
import { wpfs } from './utils/wpfs'
import { cacheDirPlugin } from './plugins/cache-dir'
import { initViteNodeServer } from './vite-node'

export async function buildServer (ctx: ViteBuildContext) {
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
Expand Down Expand Up @@ -141,10 +142,10 @@ export async function buildServer (ctx: ViteBuildContext) {
// Initialize plugins
await viteServer.pluginContainer.buildStart({})

if (ctx.nuxt.options.experimental.viteNode) {
logger.info('Vite server using experimental `vite-node`...')
await import('./vite-node').then(r => r.initViteNodeServer(ctx))
if (ctx.config.serverBundler !== 'legacy') {
await initViteNodeServer(ctx)
} else {
logger.info('Vite server using legacy server bundler...')
await import('./dev-bundler').then(r => r.initViteDevBundler(ctx, onBuild))
}
}
1 change: 1 addition & 0 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { composableKeysPlugin } from './plugins/composable-keys'
export interface ViteOptions extends InlineConfig {
vue?: Options
ssr?: SSROptions
serverBundler?: 'vite-node' | 'legacy'
}

export interface ViteBuildContext {
Expand Down