From e4b8c2dc880dba90df621906fb4e77cae5bbd89b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 9 Sep 2022 10:54:20 +0100 Subject: [PATCH] fix(vite): warmup improvements (#7377) --- packages/vite/src/client.ts | 3 --- packages/vite/src/runtime/vite-node.mjs | 2 ++ packages/vite/src/server.ts | 6 +----- packages/vite/src/utils/warmup.ts | 4 ++-- packages/vite/src/vite.ts | 9 ++++++--- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index 307851a395..8db4f7149b 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -18,9 +18,6 @@ import { devStyleSSRPlugin } from './plugins/dev-ssr-css' import { viteNodePlugin } from './vite-node' 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, { entry: ctx.entry, base: ctx.nuxt.options.dev diff --git a/packages/vite/src/runtime/vite-node.mjs b/packages/vite/src/runtime/vite-node.mjs index 898da3055f..8bc19332cc 100644 --- a/packages/vite/src/runtime/vite-node.mjs +++ b/packages/vite/src/runtime/vite-node.mjs @@ -37,6 +37,8 @@ function createRunner () { root: viteNodeOptions.root, // Equals to Nuxt `srcDir` base: viteNodeOptions.base, async fetchModule (id) { + // TODO: fix in vite-node + id = id.replace(/\/\//g, '/') return await $fetch('/module/' + encodeURI(id), { baseURL: viteNodeOptions.baseURL }).catch((err) => { diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index f482b8a7cf..f21beb3581 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -3,7 +3,7 @@ import { resolve } from 'pathe' import * as vite from 'vite' import vuePlugin from '@vitejs/plugin-vue' import viteJsxPlugin from '@vitejs/plugin-vue-jsx' -import { logger, resolveModule, resolvePath } from '@nuxt/kit' +import { logger, resolveModule } from '@nuxt/kit' import { joinURL, withoutLeadingSlash, withTrailingSlash } from 'ufo' import { ViteBuildContext, ViteOptions } from './vite' import { wpfs } from './utils/wpfs' @@ -13,10 +13,6 @@ import { ssrStylesPlugin } from './plugins/ssr-styles' import { writeManifest } from './manifest' 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 = await resolvePath(resolve(ctx.nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')) - const _resolve = (id: string) => resolveModule(id, { paths: ctx.nuxt.options.modulesDir }) const serverConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, { entry: ctx.entry, diff --git a/packages/vite/src/utils/warmup.ts b/packages/vite/src/utils/warmup.ts index 6257debcb7..ac98838a82 100644 --- a/packages/vite/src/utils/warmup.ts +++ b/packages/vite/src/utils/warmup.ts @@ -19,8 +19,8 @@ export async function warmupViteServer ( logger.debug('Warmup for %s failed with: %s', url, e) } const mod = await server.moduleGraph.getModuleByUrl(url, isServer) - const deps = Array.from(mod?.importedModules || []) - await Promise.all(deps.map(m => warmup(m.url.replace('/@id/__x00__', '\0')))) + const deps = mod?.ssrTransformResult?.deps /* server */ || Array.from(mod?.importedModules /* client */ || []).map(m => m.url) + await Promise.all(deps.map(m => warmup(m.replace('/@id/__x00__', '\0')))) } await Promise.all(entries.map(entry => warmup(entry))) diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index 664dafbf26..b6452248bb 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -1,8 +1,8 @@ import * as vite from 'vite' -import { join } from 'pathe' +import { join, resolve } from 'pathe' import type { Nuxt } from '@nuxt/schema' import type { InlineConfig, SSROptions } from 'vite' -import { logger, isIgnored } from '@nuxt/kit' +import { logger, isIgnored, resolvePath } from '@nuxt/kit' import type { Options } from '@vitejs/plugin-vue' import replace from '@rollup/plugin-replace' import { sanitizeFilePath } from 'mlly' @@ -28,9 +28,12 @@ export interface ViteBuildContext { } export async function bundle (nuxt: Nuxt) { + const useAsyncEntry = nuxt.options.experimental.asyncEntry || + (nuxt.options.vite.devBundler === 'vite-node' && nuxt.options.dev) + const entry = await resolvePath(resolve(nuxt.options.appDir, useAsyncEntry ? 'entry.async' : 'entry')) const ctx: ViteBuildContext = { nuxt, - entry: null!, + entry, config: vite.mergeConfig( { resolve: {