From dabb24bb2d6308e32eb6aeaebf785b2b9194a2d7 Mon Sep 17 00:00:00 2001 From: Windson97 Date: Wed, 3 Apr 2024 15:25:36 +0800 Subject: [PATCH] fix: using file inclusion, if included file contains relative path, the relative path will not correct --- src/node/utils/processIncludes.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/node/utils/processIncludes.ts b/src/node/utils/processIncludes.ts index 02177697..6ea03d97 100644 --- a/src/node/utils/processIncludes.ts +++ b/src/node/utils/processIncludes.ts @@ -3,6 +3,33 @@ import matter from 'gray-matter' import path from 'path' import { slash } from '../shared' +export function processIncludesRelativePath( + srcDir: string, + includePath: string, + src: string +): string { + const relativeRE = /\((\.{1,2}.*)\)/g + return src.replace(relativeRE, (m, p1) => { + try { + const resolvedSrcDir = path.resolve(srcDir) + // use platform-specific file separator to get include file's dir + const includeFileDir = includePath.substring( + 0, + includePath.lastIndexOf(path.sep) + 1 + ) + // get relative path to project root + const p1Path = path + .resolve(includeFileDir, p1) + .substring(resolvedSrcDir.length) + // replace win32's separator to / + const p1PathInVite = p1Path.replace(path.win32.sep, '/') + return '(' + p1PathInVite + ')' + } catch { + return m + } + }) +} + export function processIncludes( srcDir: string, src: string, @@ -34,6 +61,7 @@ export function processIncludes( } else { content = matter(content).content } + content = processIncludesRelativePath(srcDir, includePath, content) includes.push(slash(includePath)) // recursively process includes in the content return processIncludes(srcDir, content, includePath, includes)