From 1795ae0ddb05c0776d9dd1224ea632f1be882ef7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 6 Oct 2024 19:47:21 +0200 Subject: [PATCH] fix: resolve fs paths relative to build directory --- packages/nuxt/src/core/plugins/virtual.ts | 26 +++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/nuxt/src/core/plugins/virtual.ts b/packages/nuxt/src/core/plugins/virtual.ts index b47c535dc2..28f88ea1aa 100644 --- a/packages/nuxt/src/core/plugins/virtual.ts +++ b/packages/nuxt/src/core/plugins/virtual.ts @@ -1,5 +1,5 @@ -import { resolveAlias, useNuxt } from '@nuxt/kit' -import { dirname, isAbsolute, join, resolve } from 'pathe' +import { findPath, resolveAlias, useNuxt } from '@nuxt/kit' +import { dirname, isAbsolute, resolve } from 'pathe' import { createUnplugin } from 'unplugin' const PREFIX = '\0virtual:nuxt:' @@ -27,7 +27,7 @@ export const VirtualFSPlugin = (nuxt = useNuxt(), options: VirtualFSPluginOption return { name: 'nuxt:virtual', enforce: 'pre', - resolveId (id, importer) { + async resolveId (id, importer) { const _id = id id = resolveAlias(id, alias) @@ -41,14 +41,18 @@ export const VirtualFSPlugin = (nuxt = useNuxt(), options: VirtualFSPluginOption return PREFIX + resolvedId } - if (importer && !isAbsolute(id)) { - if (importer.startsWith(PREFIX)) { - importer = importer.slice(PREFIX.length) - } - const resolved = resolveWithExt(join(dirname(importer), id)) + if (importer && /^\.{1,2}\//.test(id)) { + const path = resolve(dirname(withoutPrefix(importer)), id) + const resolved = resolveWithExt(path) if (resolved) { return PREFIX + resolved } + if (importer.startsWith(PREFIX)) { + const fsPath = await findPath(path, { fallbackToOriginal: false }) + if (fsPath) { + return fsPath + } + } } }, @@ -58,9 +62,13 @@ export const VirtualFSPlugin = (nuxt = useNuxt(), options: VirtualFSPluginOption load (id) { return { - code: nuxt.vfs[id.slice(PREFIX.length)] || '', + code: nuxt.vfs[withoutPrefix(id)] || '', map: null, } }, } }) + +function withoutPrefix (id: string) { + return id.startsWith(PREFIX) ? id.slice(PREFIX.length) : id +}