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/')) { if (id && id.startsWith('/@id/')) {
id = id.slice('/@id/'.length) id = id.slice('/@id/'.length)
} }
if (id && id.startsWith('/@fs/')) { if (id && !id.startsWith('/@fs/') && id.startsWith('/')) {
// 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))) {
// Relative to the root directory // Relative to the root directory
const resolvedPath = resolve(opts.viteServer.config.root, '.' + id) const resolvedPath = resolve(opts.viteServer.config.root, '.' + id)
if (existsSync(resolvedPath)) { 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 // Vite will add ?v=123 to bypass browser cache
// Remove for externals // Remove for externals
const withoutVersionQuery = id.replace(/\?v=\w+$/, '') const withoutVersionQuery = id.replace(/\?v=\w+$/, '')
@ -240,7 +236,7 @@ export async function initViteDevBundler (ctx: ViteBuildContext, onBuild: () =>
// Build and watch // Build and watch
const _doBuild = async () => { const _doBuild = async () => {
const start = Date.now() 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') 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 // Have CSS in the manifest to prevent FOUC on dev SSR
await writeManifest(ctx, ids.filter(isCSS).map(i => i.slice(1))) 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 = [ const entries = [
'@vite/client', '@vite/client',
'entry.mjs', ctx.entry,
...extraEntries ...extraEntries
] ]

View File

@ -4,7 +4,7 @@ import { ViteNodeServer } from 'vite-node/server'
import fse from 'fs-extra' import fse from 'fs-extra'
import { resolve } from 'pathe' import { resolve } from 'pathe'
import { addServerMiddleware } from '@nuxt/kit' 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 { resolve as resolveModule } from 'mlly'
import { distDir } from './dirs' import { distDir } from './dirs'
import type { ViteBuildContext } from './vite' import type { ViteBuildContext } from './vite'
@ -47,13 +47,13 @@ export function registerViteNodeMiddleware (ctx: ViteBuildContext) {
}) })
} }
function getManifest (server: ViteDevServer) { function getManifest (ctx: ViteBuildContext) {
const ids = Array.from(server.moduleGraph.urlToModuleMap.keys()) const ids = Array.from(ctx.ssrServer.moduleGraph.urlToModuleMap.keys())
.filter(i => isCSS(i)) .filter(i => isCSS(i))
const entries = [ const entries = [
'@vite/client', '@vite/client',
'entry.mjs', ctx.entry,
...ids.map(i => i.slice(1)) ...ids.map(i => i.slice(1))
] ]
@ -70,7 +70,7 @@ function createViteNodeMiddleware (ctx: ViteBuildContext, invalidates: Set<strin
const app = createApp() const app = createApp()
app.use('/manifest', defineEventHandler(() => { app.use('/manifest', defineEventHandler(() => {
const manifest = getManifest(ctx.ssrServer) const manifest = getManifest(ctx)
return manifest return manifest
})) }))

View File

@ -1,5 +1,5 @@
import * as vite from 'vite' import * as vite from 'vite'
import { resolve } from 'pathe' import { join, resolve } 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'
@ -21,13 +21,16 @@ export interface ViteOptions extends InlineConfig {
export interface ViteBuildContext { export interface ViteBuildContext {
nuxt: Nuxt nuxt: Nuxt
config: ViteOptions config: ViteOptions
entry: string
clientServer?: vite.ViteDevServer clientServer?: vite.ViteDevServer
ssrServer?: vite.ViteDevServer ssrServer?: vite.ViteDevServer
} }
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,
config: vite.mergeConfig( config: vite.mergeConfig(
{ {
resolve: { resolve: {
@ -38,16 +41,13 @@ export async function bundle (nuxt: Nuxt) {
// will be filled in client/server configs // will be filled in client/server configs
'#build/plugins': '', '#build/plugins': '',
'#build': nuxt.options.buildDir, '#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', 'web-streams-polyfill/ponyfill/es2018': 'unenv/runtime/mock/empty',
// Cannot destructure property 'AbortController' of .. // Cannot destructure property 'AbortController' of ..
'abort-controller': 'unenv/runtime/mock/empty' 'abort-controller': 'unenv/runtime/mock/empty'
} }
}, },
optimizeDeps: { optimizeDeps: {
entries: [ entries: [entry],
resolve(nuxt.options.appDir, 'entry.ts')
],
include: ['vue'] include: ['vue']
}, },
css: resolveCSSOptions(nuxt), css: resolveCSSOptions(nuxt),
@ -104,7 +104,7 @@ export async function bundle (nuxt: Nuxt) {
}) })
const start = Date.now() 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`)) .then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`))
.catch(logger.error) .catch(logger.error)
}) })