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

Commit 463c15e

Browse files
authored
fix(vite): use url for entry on windows (#6355)
1 parent 746d553 commit 463c15e

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

packages/vite/src/dev-bundler.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,17 @@ async function transformRequest (opts: TransformOptions, id: string) {
4040
if (id && id.startsWith('/@id/')) {
4141
id = id.slice('/@id/'.length)
4242
}
43-
if (id && id.startsWith('/@fs/')) {
44-
// Absolute path
45-
id = id.slice('/@fs'.length)
46-
// On Windows, this may be `/C:/my/path` at this point, in which case we want to remove the `/`
47-
if (id.match(/^\/\w:/)) {
48-
id = id.slice(1)
49-
}
50-
} else if (id.startsWith('/') && !(/\/app\/entry(|.mjs)$/.test(id))) {
43+
if (id && !id.startsWith('/@fs/') && id.startsWith('/')) {
5144
// Relative to the root directory
5245
const resolvedPath = resolve(opts.viteServer.config.root, '.' + id)
5346
if (existsSync(resolvedPath)) {
5447
id = resolvedPath
5548
}
5649
}
5750

51+
// On Windows, we prefix absolute paths with `/@fs/` to skip node resolution algorithm
52+
id = id.replace(/^\/?(?=\w:)/, '/@fs/')
53+
5854
// Vite will add ?v=123 to bypass browser cache
5955
// Remove for externals
6056
const withoutVersionQuery = id.replace(/\?v=\w+$/, '')
@@ -240,7 +236,7 @@ export async function initViteDevBundler (ctx: ViteBuildContext, onBuild: () =>
240236
// Build and watch
241237
const _doBuild = async () => {
242238
const start = Date.now()
243-
const { code, ids } = await bundleRequest(options, resolve(ctx.nuxt.options.appDir, 'entry'))
239+
const { code, ids } = await bundleRequest(options, ctx.entry)
244240
await fse.writeFile(resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs'), code, 'utf-8')
245241
// Have CSS in the manifest to prevent FOUC on dev SSR
246242
await writeManifest(ctx, ids.filter(isCSS).map(i => i.slice(1)))

packages/vite/src/manifest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function writeManifest (ctx: ViteBuildContext, extraEntries: string
1111

1212
const entries = [
1313
'@vite/client',
14-
'entry.mjs',
14+
ctx.entry,
1515
...extraEntries
1616
]
1717

packages/vite/src/vite-node.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ViteNodeServer } from 'vite-node/server'
44
import fse from 'fs-extra'
55
import { resolve } from 'pathe'
66
import { addServerMiddleware } from '@nuxt/kit'
7-
import type { ModuleNode, Plugin as VitePlugin, ViteDevServer } from 'vite'
7+
import type { ModuleNode, Plugin as VitePlugin } from 'vite'
88
import { resolve as resolveModule } from 'mlly'
99
import { distDir } from './dirs'
1010
import type { ViteBuildContext } from './vite'
@@ -47,13 +47,13 @@ export function registerViteNodeMiddleware (ctx: ViteBuildContext) {
4747
})
4848
}
4949

50-
function getManifest (server: ViteDevServer) {
51-
const ids = Array.from(server.moduleGraph.urlToModuleMap.keys())
50+
function getManifest (ctx: ViteBuildContext) {
51+
const ids = Array.from(ctx.ssrServer.moduleGraph.urlToModuleMap.keys())
5252
.filter(i => isCSS(i))
5353

5454
const entries = [
5555
'@vite/client',
56-
'entry.mjs',
56+
ctx.entry,
5757
...ids.map(i => i.slice(1))
5858
]
5959

@@ -70,7 +70,7 @@ function createViteNodeMiddleware (ctx: ViteBuildContext, invalidates: Set<strin
7070
const app = createApp()
7171

7272
app.use('/manifest', defineEventHandler(() => {
73-
const manifest = getManifest(ctx.ssrServer)
73+
const manifest = getManifest(ctx)
7474
return manifest
7575
}))
7676

packages/vite/src/vite.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vite from 'vite'
2-
import { resolve } from 'pathe'
2+
import { join, resolve } from 'pathe'
33
import type { Nuxt } from '@nuxt/schema'
44
import type { InlineConfig, SSROptions } from 'vite'
55
import { logger, isIgnored } from '@nuxt/kit'
@@ -21,13 +21,16 @@ export interface ViteOptions extends InlineConfig {
2121
export interface ViteBuildContext {
2222
nuxt: Nuxt
2323
config: ViteOptions
24+
entry: string
2425
clientServer?: vite.ViteDevServer
2526
ssrServer?: vite.ViteDevServer
2627
}
2728

2829
export async function bundle (nuxt: Nuxt) {
30+
const entry = resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? 'entry.async' : 'entry')
2931
const ctx: ViteBuildContext = {
3032
nuxt,
33+
entry,
3134
config: vite.mergeConfig(
3235
{
3336
resolve: {
@@ -38,16 +41,13 @@ export async function bundle (nuxt: Nuxt) {
3841
// will be filled in client/server configs
3942
'#build/plugins': '',
4043
'#build': nuxt.options.buildDir,
41-
'/entry.mjs': resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? 'entry.async' : 'entry'),
4244
'web-streams-polyfill/ponyfill/es2018': 'unenv/runtime/mock/empty',
4345
// Cannot destructure property 'AbortController' of ..
4446
'abort-controller': 'unenv/runtime/mock/empty'
4547
}
4648
},
4749
optimizeDeps: {
48-
entries: [
49-
resolve(nuxt.options.appDir, 'entry.ts')
50-
],
50+
entries: [entry],
5151
include: ['vue']
5252
},
5353
css: resolveCSSOptions(nuxt),
@@ -104,7 +104,7 @@ export async function bundle (nuxt: Nuxt) {
104104
})
105105

106106
const start = Date.now()
107-
warmupViteServer(server, ['/entry.mjs'])
107+
warmupViteServer(server, [join('/@fs/', ctx.entry)])
108108
.then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`))
109109
.catch(logger.error)
110110
})

0 commit comments

Comments
 (0)