From 463c15e3d267886a63ecb5da164aaa49d74e0a4a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 Aug 2022 16:24:35 +0100 Subject: [PATCH] fix(vite): use url for entry on windows (#6355) --- packages/vite/src/dev-bundler.ts | 14 +++++--------- packages/vite/src/manifest.ts | 2 +- packages/vite/src/vite-node.ts | 10 +++++----- packages/vite/src/vite.ts | 12 ++++++------ 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/vite/src/dev-bundler.ts b/packages/vite/src/dev-bundler.ts index 81f0be8c3f4..475964c4ab6 100644 --- a/packages/vite/src/dev-bundler.ts +++ b/packages/vite/src/dev-bundler.ts @@ -40,14 +40,7 @@ async function transformRequest (opts: TransformOptions, id: string) { if (id && id.startsWith('/@id/')) { id = id.slice('/@id/'.length) } - if (id && id.startsWith('/@fs/')) { - // Absolute path - id = id.slice('/@fs'.length) - // On Windows, this may be `/C:/my/path` at this point, in which case we want to remove the `/` - if (id.match(/^\/\w:/)) { - id = id.slice(1) - } - } else if (id.startsWith('/') && !(/\/app\/entry(|.mjs)$/.test(id))) { + if (id && !id.startsWith('/@fs/') && id.startsWith('/')) { // Relative to the root directory const resolvedPath = resolve(opts.viteServer.config.root, '.' + id) if (existsSync(resolvedPath)) { @@ -55,6 +48,9 @@ async function transformRequest (opts: TransformOptions, id: string) { } } + // On Windows, we prefix absolute paths with `/@fs/` to skip node resolution algorithm + id = id.replace(/^\/?(?=\w:)/, '/@fs/') + // Vite will add ?v=123 to bypass browser cache // Remove for externals const withoutVersionQuery = id.replace(/\?v=\w+$/, '') @@ -240,7 +236,7 @@ export async function initViteDevBundler (ctx: ViteBuildContext, onBuild: () => // Build and watch const _doBuild = async () => { const start = Date.now() - const { code, ids } = await bundleRequest(options, resolve(ctx.nuxt.options.appDir, 'entry')) + const { code, ids } = await bundleRequest(options, ctx.entry) await fse.writeFile(resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs'), code, 'utf-8') // Have CSS in the manifest to prevent FOUC on dev SSR await writeManifest(ctx, ids.filter(isCSS).map(i => i.slice(1))) diff --git a/packages/vite/src/manifest.ts b/packages/vite/src/manifest.ts index f9236fffada..bb59ac3b1f5 100644 --- a/packages/vite/src/manifest.ts +++ b/packages/vite/src/manifest.ts @@ -11,7 +11,7 @@ export async function writeManifest (ctx: ViteBuildContext, extraEntries: string const entries = [ '@vite/client', - 'entry.mjs', + ctx.entry, ...extraEntries ] diff --git a/packages/vite/src/vite-node.ts b/packages/vite/src/vite-node.ts index 48101a83fdf..89baf2b3de8 100644 --- a/packages/vite/src/vite-node.ts +++ b/packages/vite/src/vite-node.ts @@ -4,7 +4,7 @@ import { ViteNodeServer } from 'vite-node/server' import fse from 'fs-extra' import { resolve } from 'pathe' import { addServerMiddleware } from '@nuxt/kit' -import type { ModuleNode, Plugin as VitePlugin, ViteDevServer } from 'vite' +import type { ModuleNode, Plugin as VitePlugin } from 'vite' import { resolve as resolveModule } from 'mlly' import { distDir } from './dirs' import type { ViteBuildContext } from './vite' @@ -47,13 +47,13 @@ export function registerViteNodeMiddleware (ctx: ViteBuildContext) { }) } -function getManifest (server: ViteDevServer) { - const ids = Array.from(server.moduleGraph.urlToModuleMap.keys()) +function getManifest (ctx: ViteBuildContext) { + const ids = Array.from(ctx.ssrServer.moduleGraph.urlToModuleMap.keys()) .filter(i => isCSS(i)) const entries = [ '@vite/client', - 'entry.mjs', + ctx.entry, ...ids.map(i => i.slice(1)) ] @@ -70,7 +70,7 @@ function createViteNodeMiddleware (ctx: ViteBuildContext, invalidates: Set { - const manifest = getManifest(ctx.ssrServer) + const manifest = getManifest(ctx) return manifest })) diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index 28cc05527ed..c92518dc2e9 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -1,5 +1,5 @@ import * as vite from 'vite' -import { resolve } 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' @@ -21,13 +21,16 @@ export interface ViteOptions extends InlineConfig { export interface ViteBuildContext { nuxt: Nuxt config: ViteOptions + entry: string clientServer?: vite.ViteDevServer ssrServer?: vite.ViteDevServer } export async function bundle (nuxt: Nuxt) { + const entry = resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? 'entry.async' : 'entry') const ctx: ViteBuildContext = { nuxt, + entry, config: vite.mergeConfig( { resolve: { @@ -38,16 +41,13 @@ export async function bundle (nuxt: Nuxt) { // will be filled in client/server configs '#build/plugins': '', '#build': nuxt.options.buildDir, - '/entry.mjs': resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? 'entry.async' : 'entry'), 'web-streams-polyfill/ponyfill/es2018': 'unenv/runtime/mock/empty', // Cannot destructure property 'AbortController' of .. 'abort-controller': 'unenv/runtime/mock/empty' } }, optimizeDeps: { - entries: [ - resolve(nuxt.options.appDir, 'entry.ts') - ], + entries: [entry], include: ['vue'] }, css: resolveCSSOptions(nuxt), @@ -104,7 +104,7 @@ export async function bundle (nuxt: Nuxt) { }) const start = Date.now() - warmupViteServer(server, ['/entry.mjs']) + warmupViteServer(server, [join('/@fs/', ctx.entry)]) .then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`)) .catch(logger.error) })