diff --git a/packages/core/src/helpers/namespace.ts b/packages/core/src/helpers/namespace.ts index 4839d36ef..99c42b809 100644 --- a/packages/core/src/helpers/namespace.ts +++ b/packages/core/src/helpers/namespace.ts @@ -42,7 +42,7 @@ export interface CreateNamespaceOptions { strict: boolean, namespace: string, filePath: string, - usedBy?: string + usedByMap: Map ) => string; hashSeparator?: string; strict?: boolean; @@ -75,8 +75,9 @@ export function defaultNoMatchHandler( strict: boolean, ns: string, stylesheetPath: string, - usedBy?: string + usedByMap: Map ): string { + const usedBy = usedByMap.get(ns); throw new Error( `Could not create namespace for:\n${stylesheetPath}\nthe last valid namespace tried was ${JSON.stringify( ns @@ -146,16 +147,11 @@ export function createNamespaceStrategy(options: CreateNamespaceOptions) { if (usedBy === stylesheetPath) { return finalNamespace; } else if (strict) { - return handleNoMatch(strict, finalNamespace, stylesheetPath, usedBy); + return handleNoMatch(strict, finalNamespace, stylesheetPath, usedNamespaces); } i++; } - return handleNoMatch( - strict, - finalNamespace, - stylesheetPath, - usedNamespaces.get(finalNamespace) - ); + return handleNoMatch(strict, finalNamespace, stylesheetPath, usedNamespaces); }; } diff --git a/packages/core/test/helpers/namespace.spec.ts b/packages/core/test/helpers/namespace.spec.ts index 923156ee7..be593a728 100644 --- a/packages/core/test/helpers/namespace.spec.ts +++ b/packages/core/test/helpers/namespace.spec.ts @@ -119,7 +119,12 @@ describe('createNamespaceStrategy', () => { it('should throw when no unique namespace can be generated and hash slice size is larger then hash length', () => { function getErrorMessage() { try { - defaultNoMatchHandler(false, 'x-1', '/package/x2.st.css', '/package/x1.st.css'); + defaultNoMatchHandler( + false, + 'x-1', + '/package/x2.st.css', + new Map([['x-1', '/package/x1.st.css']]) + ); } catch (e) { return (e as Error).message; } @@ -140,7 +145,12 @@ describe('createNamespaceStrategy', () => { it('should throw when no unique namespace can be generated in strict mode', () => { function getErrorMessage() { try { - defaultNoMatchHandler(true, 'x', '/package/x1.st.css', '/package/x.st.css'); + defaultNoMatchHandler( + true, + 'x', + '/package/x1.st.css', + new Map([['x', '/package/x.st.css']]) + ); } catch (e) { return (e as Error).message; } diff --git a/packages/node/src/resolve-namespace.ts b/packages/node/src/resolve-namespace.ts index 5a4c661d9..dd5be5052 100644 --- a/packages/node/src/resolve-namespace.ts +++ b/packages/node/src/resolve-namespace.ts @@ -4,6 +4,7 @@ import { defaultNoMatchHandler, } from '@stylable/core'; import { dirname, relative } from 'path'; +import { existsSync } from 'node:fs'; import { findPackageJson } from './find-package-json'; export function resolveNamespaceFactory( @@ -16,7 +17,10 @@ export function resolveNamespaceFactory( export const packageJsonLookupCache = new Map(); -export function createNamespaceStrategyNode(options: Partial = {}) { +export function createNamespaceStrategyNode( + options: Partial = {}, + devMode?: boolean +) { return createNamespaceStrategy({ normalizePath(packageRoot: string, stylesheetPath: string) { return relative(packageRoot, stylesheetPath).replace(/\\/g, '/'); @@ -36,8 +40,17 @@ export function createNamespaceStrategyNode(options: Partial