chore(vite): 🔧 Optimize module path resolution & dev build configs in core Vite setup

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Lilith 2026-02-22 11:02:43 -08:00
parent 097a752cb2
commit 8745e2ca50
2 changed files with 67 additions and 5 deletions

View file

@ -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<string, string> {
'@features': paths.features,
};
}
/**
* Vite plugin that resolves `@features/<name>` to the feature's
* barrel export (`shared/src/index.ts`) when one exists.
*
* Resolution order (mirrors tsconfig fallback paths):
* 1. `features/<name>/shared/src/index.ts` barrel export
* 2. `features/<name>/...` 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<string, string | null>();
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/<name>/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;
},
};
}

View file

@ -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 {
},
};
},
};
}];
}
/**