mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
feat(schema, vite)!: enable vite-node
by default (#6217)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
b4bea517df
commit
1b2304b632
@ -5,16 +5,11 @@ export default {
|
|||||||
* Set to true to generate an async entry point for the Vue bundle (for module federation support).
|
* Set to true to generate an async entry point for the Vue bundle (for module federation support).
|
||||||
*/
|
*/
|
||||||
asyncEntry: {
|
asyncEntry: {
|
||||||
$resolve: (val, get) => val ?? (get('dev') && get('experimental.viteNode')) ?? false
|
$resolve: (val, get) => val ?? false
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use `vite-node` for on-demand server chunk loading.
|
* Enable Vue's reactivity transform
|
||||||
*/
|
|
||||||
viteNode: process.env.EXPERIMENTAL_VITE_NODE ? true : false,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enable Vue's reactivity transform.
|
|
||||||
* @see https://vuejs.org/guide/extras/reactivity-transform.html
|
* @see https://vuejs.org/guide/extras/reactivity-transform.html
|
||||||
*/
|
*/
|
||||||
reactivityTransform: false,
|
reactivityTransform: false,
|
||||||
@ -31,6 +26,23 @@ export default {
|
|||||||
*/
|
*/
|
||||||
treeshakeClientOnly: false,
|
treeshakeClientOnly: false,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use vite-node for on-demand server chunk loading
|
||||||
|
*
|
||||||
|
* @deprecated use `vite.devBundler: '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.')
|
||||||
|
} else if (val === false) {
|
||||||
|
console.warn('`vite-node` is now enabled by default. To disable it, set `vite.devBundler` to `legacy` instead.')
|
||||||
|
}
|
||||||
|
return val ?? true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Split server bundle into multiple chunks and dynamically import them.
|
* Split server bundle into multiple chunks and dynamically import them.
|
||||||
*
|
*
|
||||||
|
@ -45,4 +45,9 @@ export interface ViteConfig extends ViteUserConfig {
|
|||||||
* @see https://github.com/vitejs/vite/tree/main/packages/plugin-vue
|
* @see https://github.com/vitejs/vite/tree/main/packages/plugin-vue
|
||||||
*/
|
*/
|
||||||
vue?: VuePluginOptions
|
vue?: VuePluginOptions
|
||||||
|
/**
|
||||||
|
* Bundler for dev time server-side rendering.
|
||||||
|
* @default 'vite-node'
|
||||||
|
*/
|
||||||
|
devBundler?: 'vite-node' | 'legacy'
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,14 @@ import { wpfs } from './utils/wpfs'
|
|||||||
import type { ViteBuildContext, ViteOptions } from './vite'
|
import type { ViteBuildContext, ViteOptions } from './vite'
|
||||||
import { writeManifest } from './manifest'
|
import { writeManifest } from './manifest'
|
||||||
import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
|
import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
|
||||||
|
import { viteNodePlugin } from './vite-node'
|
||||||
|
|
||||||
export async function buildClient (ctx: ViteBuildContext) {
|
export async function buildClient (ctx: ViteBuildContext) {
|
||||||
|
const useAsyncEntry = ctx.nuxt.options.experimental.asyncEntry
|
||||||
|
ctx.entry = resolve(ctx.nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')
|
||||||
|
|
||||||
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
||||||
|
entry: ctx.entry,
|
||||||
experimental: {
|
experimental: {
|
||||||
renderBuiltUrl: (filename, { type, hostType }) => {
|
renderBuiltUrl: (filename, { type, hostType }) => {
|
||||||
if (hostType !== 'js' || type === 'asset') {
|
if (hostType !== 'js' || type === 'asset') {
|
||||||
@ -30,6 +35,9 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
'process.client': true,
|
'process.client': true,
|
||||||
'module.hot': false
|
'module.hot': false
|
||||||
},
|
},
|
||||||
|
optimizeDeps: {
|
||||||
|
entries: [ctx.entry]
|
||||||
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/client'),
|
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/client'),
|
||||||
@ -38,7 +46,10 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
manifest: true,
|
manifest: true,
|
||||||
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client')
|
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client'),
|
||||||
|
rollupOptions: {
|
||||||
|
input: ctx.entry
|
||||||
|
}
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
cacheDirPlugin(ctx.nuxt.options.rootDir, 'client'),
|
cacheDirPlugin(ctx.nuxt.options.rootDir, 'client'),
|
||||||
@ -48,9 +59,7 @@ export async function buildClient (ctx: ViteBuildContext) {
|
|||||||
srcDir: ctx.nuxt.options.srcDir,
|
srcDir: ctx.nuxt.options.srcDir,
|
||||||
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir)
|
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir)
|
||||||
}),
|
}),
|
||||||
ctx.nuxt.options.experimental.viteNode
|
viteNodePlugin(ctx)
|
||||||
? await import('./vite-node').then(r => r.viteNodePlugin(ctx))
|
|
||||||
: undefined
|
|
||||||
],
|
],
|
||||||
appType: 'custom',
|
appType: 'custom',
|
||||||
server: {
|
server: {
|
||||||
|
@ -8,10 +8,16 @@ import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo'
|
|||||||
import { ViteBuildContext, ViteOptions } from './vite'
|
import { ViteBuildContext, ViteOptions } from './vite'
|
||||||
import { wpfs } from './utils/wpfs'
|
import { wpfs } from './utils/wpfs'
|
||||||
import { cacheDirPlugin } from './plugins/cache-dir'
|
import { cacheDirPlugin } from './plugins/cache-dir'
|
||||||
|
import { initViteNodeServer } from './vite-node'
|
||||||
|
|
||||||
export async function buildServer (ctx: ViteBuildContext) {
|
export async function buildServer (ctx: ViteBuildContext) {
|
||||||
|
const useAsyncEntry = ctx.nuxt.options.experimental.asyncEntry ||
|
||||||
|
(ctx.nuxt.options.vite.devBundler === 'vite-node' && ctx.nuxt.options.dev)
|
||||||
|
ctx.entry = resolve(ctx.nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')
|
||||||
|
|
||||||
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
|
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
|
||||||
const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
|
||||||
|
entry: ctx.entry,
|
||||||
base: ctx.nuxt.options.dev
|
base: ctx.nuxt.options.dev
|
||||||
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
|
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
|
||||||
: undefined,
|
: undefined,
|
||||||
@ -39,6 +45,9 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
'typeof location': '"undefined"',
|
'typeof location': '"undefined"',
|
||||||
'typeof XMLHttpRequest': '"undefined"'
|
'typeof XMLHttpRequest': '"undefined"'
|
||||||
},
|
},
|
||||||
|
optimizeDeps: {
|
||||||
|
entries: [ctx.entry]
|
||||||
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/server'),
|
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/server'),
|
||||||
@ -73,6 +82,7 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/server'),
|
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/server'),
|
||||||
ssr: ctx.nuxt.options.ssr ?? true,
|
ssr: ctx.nuxt.options.ssr ?? true,
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
|
input: ctx.entry,
|
||||||
external: ['#internal/nitro', ...ctx.nuxt.options.experimental.externalVue ? ['vue', 'vue-router'] : []],
|
external: ['#internal/nitro', ...ctx.nuxt.options.experimental.externalVue ? ['vue', 'vue-router'] : []],
|
||||||
output: {
|
output: {
|
||||||
entryFileNames: 'server.mjs',
|
entryFileNames: 'server.mjs',
|
||||||
@ -141,10 +151,10 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
// Initialize plugins
|
// Initialize plugins
|
||||||
await viteServer.pluginContainer.buildStart({})
|
await viteServer.pluginContainer.buildStart({})
|
||||||
|
|
||||||
if (ctx.nuxt.options.experimental.viteNode) {
|
if (ctx.config.devBundler !== 'legacy') {
|
||||||
logger.info('Vite server using experimental `vite-node`...')
|
await initViteNodeServer(ctx)
|
||||||
await import('./vite-node').then(r => r.initViteNodeServer(ctx))
|
|
||||||
} else {
|
} else {
|
||||||
|
logger.info('Vite server using legacy server bundler...')
|
||||||
await import('./dev-bundler').then(r => r.initViteDevBundler(ctx, onBuild))
|
await import('./dev-bundler').then(r => r.initViteDevBundler(ctx, onBuild))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as vite from 'vite'
|
import * as vite from 'vite'
|
||||||
import { join, resolve } from 'pathe'
|
import { join } from 'pathe'
|
||||||
import type { Nuxt } from '@nuxt/schema'
|
import type { Nuxt } from '@nuxt/schema'
|
||||||
import type { InlineConfig, SSROptions } from 'vite'
|
import type { InlineConfig, SSROptions } from 'vite'
|
||||||
import { logger, isIgnored } from '@nuxt/kit'
|
import { logger, isIgnored } from '@nuxt/kit'
|
||||||
@ -16,6 +16,7 @@ import { composableKeysPlugin } from './plugins/composable-keys'
|
|||||||
export interface ViteOptions extends InlineConfig {
|
export interface ViteOptions extends InlineConfig {
|
||||||
vue?: Options
|
vue?: Options
|
||||||
ssr?: SSROptions
|
ssr?: SSROptions
|
||||||
|
devBundler?: 'vite-node' | 'legacy'
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ViteBuildContext {
|
export interface ViteBuildContext {
|
||||||
@ -27,10 +28,9 @@ export interface ViteBuildContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function bundle (nuxt: Nuxt) {
|
export async function bundle (nuxt: Nuxt) {
|
||||||
const entry = resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? 'entry.async' : 'entry')
|
|
||||||
const ctx: ViteBuildContext = {
|
const ctx: ViteBuildContext = {
|
||||||
nuxt,
|
nuxt,
|
||||||
entry,
|
entry: null,
|
||||||
config: vite.mergeConfig(
|
config: vite.mergeConfig(
|
||||||
{
|
{
|
||||||
resolve: {
|
resolve: {
|
||||||
@ -47,14 +47,12 @@ export async function bundle (nuxt: Nuxt) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
entries: [entry],
|
|
||||||
include: ['vue']
|
include: ['vue']
|
||||||
},
|
},
|
||||||
css: resolveCSSOptions(nuxt),
|
css: resolveCSSOptions(nuxt),
|
||||||
build: {
|
build: {
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
output: { sanitizeFileName: sanitizeFilePath },
|
output: { sanitizeFileName: sanitizeFilePath }
|
||||||
input: resolve(nuxt.options.appDir, 'entry')
|
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
exclude: nuxt.options.ignore
|
exclude: nuxt.options.ignore
|
||||||
|
Loading…
Reference in New Issue
Block a user