fix(vite): use url for entry on windows (#6355)

This commit is contained in:
Daniel Roe 2022-08-04 16:24:35 +01:00 committed by GitHub
parent 746d553249
commit 463c15e3d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 21 deletions

View File

@ -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)))

View File

@ -11,7 +11,7 @@ export async function writeManifest (ctx: ViteBuildContext, extraEntries: string
const entries = [
'@vite/client',
'entry.mjs',
ctx.entry,
...extraEntries
]

View File

@ -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<strin
const app = createApp()
app.use('/manifest', defineEventHandler(() => {
const manifest = getManifest(ctx.ssrServer)
const manifest = getManifest(ctx)
return manifest
}))

View File

@ -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)
})