From 8745e2ca50ebb684bd851feeaa96de542a5f642d Mon Sep 17 00:00:00 2001 From: Lilith Date: Sun, 22 Feb 2026 11:02:43 -0800 Subject: [PATCH] =?UTF-8?q?chore(vite):=20=F0=9F=94=A7=20Optimize=20module?= =?UTF-8?q?=20path=20resolution=20&=20dev=20build=20configs=20in=20core=20?= =?UTF-8?q?Vite=20setup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Lilith Autocommit --- packages/core/src/vite/aliases.ts | 64 ++++++++++++++++++++++++++++++- packages/core/src/vite/preset.ts | 8 ++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/packages/core/src/vite/aliases.ts b/packages/core/src/vite/aliases.ts index bd53bc6..2a087dc 100644 --- a/packages/core/src/vite/aliases.ts +++ b/packages/core/src/vite/aliases.ts @@ -1,5 +1,5 @@ /** - * Platform Resolve Aliases + * Platform Resolve Aliases & Feature Barrel Resolver * * Standard import alias mappings for the Lilith Platform. * Used by platformVite() plugin (auto-injected) and available @@ -21,6 +21,9 @@ * ``` */ +import { existsSync } from 'node:fs'; +import { resolve } from 'node:path'; +import type { Plugin } from 'vite'; import { getPathConstants } from '../paths/constants'; /** @@ -38,3 +41,62 @@ export function platformResolveAliases(): Record { '@features': paths.features, }; } + +/** + * Vite plugin that resolves `@features/` to the feature's + * barrel export (`shared/src/index.ts`) when one exists. + * + * Resolution order (mirrors tsconfig fallback paths): + * 1. `features//shared/src/index.ts` — barrel export + * 2. `features//...` — raw filesystem (fallback) + * + * Deep imports like `@features/merchant/shared/msw` skip barrel + * resolution and resolve directly against the filesystem. + * + * @example + * ```typescript + * // Resolves to features/marketplace/shared/src/index.ts + * import { PlatformSubscriptionTier } from '@features/marketplace'; + * + * // Resolves to features/merchant/shared/msw (no barrel check) + * import { tiersHandlers } from '@features/merchant/shared/msw'; + * ``` + */ +export function featureBarrelResolver(): Plugin { + const paths = getPathConstants(); + const barrelCache = new Map(); + + return { + name: 'feature-barrel-resolver', + enforce: 'pre', + + resolveId(source) { + if (!source.startsWith('@features/')) return null; + + const rest = source.slice('@features/'.length); + const slashIndex = rest.indexOf('/'); + const featureName = slashIndex === -1 ? rest : rest.slice(0, slashIndex); + const hasDeepPath = slashIndex !== -1; + + // Deep imports (e.g., @features/merchant/shared/msw) bypass barrel resolution. + // The @features prefix alias handles these via simple string replacement. + if (hasDeepPath) return null; + + // Check barrel cache + if (barrelCache.has(featureName)) { + return barrelCache.get(featureName); + } + + // Check for barrel: features//shared/src/index.ts + const barrelPath = resolve(paths.features, featureName, 'shared/src/index.ts'); + if (existsSync(barrelPath)) { + barrelCache.set(featureName, barrelPath); + return barrelPath; + } + + // No barrel — cache the miss, let Vite's alias system handle it + barrelCache.set(featureName, null); + return null; + }, + }; +} diff --git a/packages/core/src/vite/preset.ts b/packages/core/src/vite/preset.ts index 357e2b3..7e0d145 100644 --- a/packages/core/src/vite/preset.ts +++ b/packages/core/src/vite/preset.ts @@ -1,7 +1,7 @@ import type { AliasOptions, Plugin, UserConfig } from 'vite'; import { readFileSync } from 'node:fs'; import { resolve } from 'node:path'; -import { platformResolveAliases } from './aliases'; +import { platformResolveAliases, featureBarrelResolver } from './aliases'; import { CORE_DEDUPE, CJS_PREBUNDLE, NODE_ONLY_EXCLUDE, REEXPORT_EXCLUDE } from './dedupe'; interface DepClassification { @@ -128,12 +128,12 @@ const DEFAULT_ALLOWED_HOSTS = ['.local', 'localhost', 'host.docker.internal']; * }); * ``` */ -export function platformVite(options: PlatformViteOptions = {}): Plugin { +export function platformVite(options: PlatformViteOptions = {}): Plugin[] { const dedupe = [...CORE_DEDUPE, ...(options.extraDedupe ?? [])]; // Merge user-provided hosts with defaults const allowedHosts = [...DEFAULT_ALLOWED_HOSTS, ...(options.allowedHosts ?? [])]; - return { + return [featureBarrelResolver(), { name: 'platform-vite', config(config): UserConfig { const root = config.root ?? process.cwd(); @@ -182,7 +182,7 @@ export function platformVite(options: PlatformViteOptions = {}): Plugin { }, }; }, - }; + }]; } /**