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

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

Merged
merged 4 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions packages/vite/src/dev-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,17 @@ 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)) {
id = resolvedPath
}
}

// On Windows, we prefix absolute paths with `/@fs/` to skip node resolution algorithm
id = id.replace(/^\/?(?=\w:)/, '/@fs/')
pi0 marked this conversation as resolved.
Show resolved Hide resolved

// Vite will add ?v=123 to bypass browser cache
// Remove for externals
const withoutVersionQuery = id.replace(/\?v=\w+$/, '')
Expand Down Expand Up @@ -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)))
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function writeManifest (ctx: ViteBuildContext, extraEntries: string

const entries = [
'@vite/client',
'entry.mjs',
ctx.entry,
...extraEntries
]

Expand Down
10 changes: 5 additions & 5 deletions packages/vite/src/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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))
]

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

Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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: {
Expand All @@ -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),
Expand Down Expand Up @@ -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)
})
Expand Down