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

Commit 1b2304b

Browse files
antfupi0
andauthored
feat(schema, vite)!: enable vite-node by default (#6217)
Co-authored-by: Pooya Parsa <[email protected]>
1 parent b4bea51 commit 1b2304b

File tree

5 files changed

+54
-20
lines changed

5 files changed

+54
-20
lines changed

packages/schema/src/config/experimental.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ export default {
55
* Set to true to generate an async entry point for the Vue bundle (for module federation support).
66
*/
77
asyncEntry: {
8-
$resolve: (val, get) => val ?? (get('dev') && get('experimental.viteNode')) ?? false
8+
$resolve: (val, get) => val ?? false
99
},
1010

1111
/**
12-
* Use `vite-node` for on-demand server chunk loading.
13-
*/
14-
viteNode: process.env.EXPERIMENTAL_VITE_NODE ? true : false,
15-
16-
/**
17-
* Enable Vue's reactivity transform.
12+
* Enable Vue's reactivity transform
1813
* @see https://vuejs.org/guide/extras/reactivity-transform.html
1914
*/
2015
reactivityTransform: false,
@@ -31,6 +26,23 @@ export default {
3126
*/
3227
treeshakeClientOnly: false,
3328

29+
/**
30+
* Use vite-node for on-demand server chunk loading
31+
*
32+
* @deprecated use `vite.devBundler: 'vite-node'`
33+
*/
34+
viteNode: {
35+
$resolve: (val) => {
36+
val = process.env.EXPERIMENTAL_VITE_NODE ? true : val
37+
if (val === true) {
38+
console.warn('`vite-node` is now enabled by default. You can safely remove `experimental.viteNode` from your config.')
39+
} else if (val === false) {
40+
console.warn('`vite-node` is now enabled by default. To disable it, set `vite.devBundler` to `legacy` instead.')
41+
}
42+
return val ?? true
43+
}
44+
},
45+
3446
/**
3547
* Split server bundle into multiple chunks and dynamically import them.
3648
*

packages/schema/src/types/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ export interface ViteConfig extends ViteUserConfig {
4545
* @see https://github.com/vitejs/vite/tree/main/packages/plugin-vue
4646
*/
4747
vue?: VuePluginOptions
48+
/**
49+
* Bundler for dev time server-side rendering.
50+
* @default 'vite-node'
51+
*/
52+
devBundler?: 'vite-node' | 'legacy'
4853
}

packages/vite/src/client.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import { wpfs } from './utils/wpfs'
1313
import type { ViteBuildContext, ViteOptions } from './vite'
1414
import { writeManifest } from './manifest'
1515
import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
16+
import { viteNodePlugin } from './vite-node'
1617

1718
export async function buildClient (ctx: ViteBuildContext) {
19+
const useAsyncEntry = ctx.nuxt.options.experimental.asyncEntry
20+
ctx.entry = resolve(ctx.nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')
21+
1822
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
23+
entry: ctx.entry,
1924
experimental: {
2025
renderBuiltUrl: (filename, { type, hostType }) => {
2126
if (hostType !== 'js' || type === 'asset') {
@@ -30,6 +35,9 @@ export async function buildClient (ctx: ViteBuildContext) {
3035
'process.client': true,
3136
'module.hot': false
3237
},
38+
optimizeDeps: {
39+
entries: [ctx.entry]
40+
},
3341
resolve: {
3442
alias: {
3543
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/client'),
@@ -38,7 +46,10 @@ export async function buildClient (ctx: ViteBuildContext) {
3846
},
3947
build: {
4048
manifest: true,
41-
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client')
49+
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client'),
50+
rollupOptions: {
51+
input: ctx.entry
52+
}
4253
},
4354
plugins: [
4455
cacheDirPlugin(ctx.nuxt.options.rootDir, 'client'),
@@ -48,9 +59,7 @@ export async function buildClient (ctx: ViteBuildContext) {
4859
srcDir: ctx.nuxt.options.srcDir,
4960
buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir)
5061
}),
51-
ctx.nuxt.options.experimental.viteNode
52-
? await import('./vite-node').then(r => r.viteNodePlugin(ctx))
53-
: undefined
62+
viteNodePlugin(ctx)
5463
],
5564
appType: 'custom',
5665
server: {

packages/vite/src/server.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo'
88
import { ViteBuildContext, ViteOptions } from './vite'
99
import { wpfs } from './utils/wpfs'
1010
import { cacheDirPlugin } from './plugins/cache-dir'
11+
import { initViteNodeServer } from './vite-node'
1112

1213
export async function buildServer (ctx: ViteBuildContext) {
14+
const useAsyncEntry = ctx.nuxt.options.experimental.asyncEntry ||
15+
(ctx.nuxt.options.vite.devBundler === 'vite-node' && ctx.nuxt.options.dev)
16+
ctx.entry = resolve(ctx.nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')
17+
1318
const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir })
1419
const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
20+
entry: ctx.entry,
1521
base: ctx.nuxt.options.dev
1622
? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, '/') || '/', ctx.nuxt.options.app.buildAssetsDir)
1723
: undefined,
@@ -39,6 +45,9 @@ export async function buildServer (ctx: ViteBuildContext) {
3945
'typeof location': '"undefined"',
4046
'typeof XMLHttpRequest': '"undefined"'
4147
},
48+
optimizeDeps: {
49+
entries: [ctx.entry]
50+
},
4251
resolve: {
4352
alias: {
4453
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/server'),
@@ -73,6 +82,7 @@ export async function buildServer (ctx: ViteBuildContext) {
7382
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/server'),
7483
ssr: ctx.nuxt.options.ssr ?? true,
7584
rollupOptions: {
85+
input: ctx.entry,
7686
external: ['#internal/nitro', ...ctx.nuxt.options.experimental.externalVue ? ['vue', 'vue-router'] : []],
7787
output: {
7888
entryFileNames: 'server.mjs',
@@ -141,10 +151,10 @@ export async function buildServer (ctx: ViteBuildContext) {
141151
// Initialize plugins
142152
await viteServer.pluginContainer.buildStart({})
143153

144-
if (ctx.nuxt.options.experimental.viteNode) {
145-
logger.info('Vite server using experimental `vite-node`...')
146-
await import('./vite-node').then(r => r.initViteNodeServer(ctx))
154+
if (ctx.config.devBundler !== 'legacy') {
155+
await initViteNodeServer(ctx)
147156
} else {
157+
logger.info('Vite server using legacy server bundler...')
148158
await import('./dev-bundler').then(r => r.initViteDevBundler(ctx, onBuild))
149159
}
150160
}

packages/vite/src/vite.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vite from 'vite'
2-
import { join, resolve } from 'pathe'
2+
import { join } from 'pathe'
33
import type { Nuxt } from '@nuxt/schema'
44
import type { InlineConfig, SSROptions } from 'vite'
55
import { logger, isIgnored } from '@nuxt/kit'
@@ -16,6 +16,7 @@ import { composableKeysPlugin } from './plugins/composable-keys'
1616
export interface ViteOptions extends InlineConfig {
1717
vue?: Options
1818
ssr?: SSROptions
19+
devBundler?: 'vite-node' | 'legacy'
1920
}
2021

2122
export interface ViteBuildContext {
@@ -27,10 +28,9 @@ export interface ViteBuildContext {
2728
}
2829

2930
export async function bundle (nuxt: Nuxt) {
30-
const entry = resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? 'entry.async' : 'entry')
3131
const ctx: ViteBuildContext = {
3232
nuxt,
33-
entry,
33+
entry: null,
3434
config: vite.mergeConfig(
3535
{
3636
resolve: {
@@ -47,14 +47,12 @@ export async function bundle (nuxt: Nuxt) {
4747
}
4848
},
4949
optimizeDeps: {
50-
entries: [entry],
5150
include: ['vue']
5251
},
5352
css: resolveCSSOptions(nuxt),
5453
build: {
5554
rollupOptions: {
56-
output: { sanitizeFileName: sanitizeFilePath },
57-
input: resolve(nuxt.options.appDir, 'entry')
55+
output: { sanitizeFileName: sanitizeFilePath }
5856
},
5957
watch: {
6058
exclude: nuxt.options.ignore

0 commit comments

Comments
 (0)