fix: do not pass separate alias

This commit is contained in:
Daniel Roe 2024-10-09 10:50:03 +02:00
parent 59d8427048
commit ddc71aea48
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
3 changed files with 7 additions and 17 deletions

View File

@ -243,7 +243,7 @@ async function initNuxt (nuxt: Nuxt) {
// Support Nuxt VFS // Support Nuxt VFS
addBuildPlugin(VirtualFSPlugin(nuxt, { mode: 'server' }), { client: false }) addBuildPlugin(VirtualFSPlugin(nuxt, { mode: 'server' }), { client: false })
addBuildPlugin(VirtualFSPlugin(nuxt, { mode: 'client', alias: { '#internal/nitro': '#build/nitro.client.mjs' } }), { server: false }) addBuildPlugin(VirtualFSPlugin(nuxt, { mode: 'client' }), { server: false })
// Add plugin normalization plugin // Add plugin normalization plugin
addBuildPlugin(RemovePluginMetadataPlugin(nuxt)) addBuildPlugin(RemovePluginMetadataPlugin(nuxt))

View File

@ -1,4 +1,4 @@
import { findPath, resolveAlias, useNuxt } from '@nuxt/kit' import { resolveAlias, useNuxt } from '@nuxt/kit'
import { dirname, isAbsolute, resolve } from 'pathe' import { dirname, isAbsolute, resolve } from 'pathe'
import { createUnplugin } from 'unplugin' import { createUnplugin } from 'unplugin'
@ -6,12 +6,10 @@ const PREFIX = '\0virtual:nuxt:'
interface VirtualFSPluginOptions { interface VirtualFSPluginOptions {
mode: 'client' | 'server' mode: 'client' | 'server'
alias?: Record<string, string>
} }
export const VirtualFSPlugin = (nuxt = useNuxt(), options: VirtualFSPluginOptions) => createUnplugin(() => { export const VirtualFSPlugin = (nuxt = useNuxt(), options: VirtualFSPluginOptions) => createUnplugin(() => {
const extensions = ['', ...nuxt.options.extensions] const extensions = ['', ...nuxt.options.extensions]
const alias = { ...nuxt.options.alias, ...options.alias }
const resolveWithExt = (id: string) => { const resolveWithExt = (id: string) => {
for (const suffix of ['', '.' + options.mode]) { for (const suffix of ['', '.' + options.mode]) {
@ -26,10 +24,8 @@ export const VirtualFSPlugin = (nuxt = useNuxt(), options: VirtualFSPluginOption
return { return {
name: 'nuxt:virtual', name: 'nuxt:virtual',
enforce: 'pre', resolveId (id, importer) {
async resolveId (id, importer) { id = resolveAlias(id, nuxt.options.alias)
const _id = id
id = resolveAlias(id, alias)
if (process.platform === 'win32' && isAbsolute(id)) { if (process.platform === 'win32' && isAbsolute(id)) {
// Add back C: prefix on Windows // Add back C: prefix on Windows
@ -41,23 +37,17 @@ export const VirtualFSPlugin = (nuxt = useNuxt(), options: VirtualFSPluginOption
return PREFIX + resolvedId return PREFIX + resolvedId
} }
if (importer && /^\.{1,2}\//.test(id)) { if (importer && /^\.{1,2}[\\/]/.test(id)) {
const path = resolve(dirname(withoutPrefix(importer)), id) const path = resolve(dirname(withoutPrefix(importer)), id)
const resolved = resolveWithExt(path) const resolved = resolveWithExt(path)
if (resolved) { if (resolved) {
return PREFIX + resolved return PREFIX + resolved
} }
if (importer.startsWith(PREFIX)) {
const fsPath = await findPath(path, { fallbackToOriginal: false })
if (fsPath) {
return fsPath
}
}
} }
}, },
loadInclude (id) { loadInclude (id) {
return id.startsWith(PREFIX) return id.startsWith(PREFIX) && withoutPrefix(id) in nuxt.vfs
}, },
load (id) { load (id) {

View File

@ -109,7 +109,7 @@ export async function buildClient (ctx: ViteBuildContext) {
alias: { alias: {
...nodeCompat.alias, ...nodeCompat.alias,
...ctx.config.resolve?.alias, ...ctx.config.resolve?.alias,
'#internal/nitro': '#build/nitro.client.mjs', '#internal/nitro': join(ctx.nuxt.options.buildDir, 'nitro.client.mjs'),
}, },
dedupe: [ dedupe: [
'vue', 'vue',