From a20e4ff486774c5ee9572551816fa692fe494e61 Mon Sep 17 00:00:00 2001 From: John Jenkins Date: Thu, 30 Apr 2026 19:59:22 +0100 Subject: [PATCH] fix: properly deal with relative output paths --- src/load-config-meta.ts | 10 +++++++--- src/test/load-config-meta.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/load-config-meta.ts b/src/load-config-meta.ts index 26d66ef..4cc6213 100644 --- a/src/load-config-meta.ts +++ b/src/load-config-meta.ts @@ -2,7 +2,7 @@ import { loadConfig, OutputTargetWww } from '@stencil/core/compiler'; import { findUp } from 'find-up'; import { existsSync } from 'fs'; -import { join, relative } from 'path'; +import { isAbsolute, join, relative } from 'path'; /** * Common shape for output targets with dir and buildDir properties. @@ -54,7 +54,9 @@ export const loadConfigMeta = async (cwd?: string) => { if (wwwTarget) { // Get path from dev-server root to www - const relativePath = relative(devServer.root!, wwwTarget.dir!); + // If dir is relative, it's already relative to project root (same as devServer.root) + const wwwDir = wwwTarget.dir!; + const relativePath = isAbsolute(wwwDir) ? relative(devServer.root!, wwwDir) : wwwDir; // Use buildDir from config (defaults to 'build' for www target) const buildDir = (wwwTarget as unknown as { buildDir?: string }).buildDir ?? 'build'; @@ -65,7 +67,9 @@ export const loadConfigMeta = async (cwd?: string) => { const target = distTarget ?? loaderBundleTarget!; // Get path from dev-server root to target dir - const relativePath = relative(devServer.root!, target.dir!); + // If dir is relative, it's already relative to project root (same as devServer.root) + const targetDir = target.dir!; + const relativePath = isAbsolute(targetDir) ? relative(devServer.root!, targetDir) : targetDir; // dist/loader-bundle use empty string as default buildDir // Path structure: dir/buildDir/namespace/namespace (extra namespace folder) diff --git a/src/test/load-config-meta.spec.ts b/src/test/load-config-meta.spec.ts index d0affcc..f4cba4d 100644 --- a/src/test/load-config-meta.spec.ts +++ b/src/test/load-config-meta.spec.ts @@ -213,6 +213,26 @@ describe('loadConfigMeta', () => { }); }); + it('should handle loader-bundle target with relative dir path', async () => { + stencilConfig.outputTargets = [ + { + type: 'loader-bundle', + dir: 'dist/loader-bundle', // relative path, not absolute + }, + ]; + existsSyncMock.mockReturnValueOnce(true); + findUpMock.mockResolvedValueOnce('/mock-path/stencil.config.ts'); + + const configMeta = await loadConfigMeta(); + + expect(configMeta).toEqual({ + baseURL: 'http://localhost:4444', + stencilEntryPath: './dist/loader-bundle/mock-namespace/mock-namespace', + stencilNamespace: 'mock-namespace', + webServerUrl: 'http://localhost:4444/status', + }); + }); + it('should log a warning if no Stencil config path was found', async () => { const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});