Feature Store Fakes: Fakes for feature flagging/feature stores (e.g., LaunchDarkly local mode, Unleash, Faker-based context).
The goal is a "template" – likely a repository structure or a configuration setup that can be reused across multiple projects to standardize local development.
Architecture/Stack Selection: Needs to choose specific technologies for each part (or offer options).
SPA Server: Vite is the current standard for speed and simplicity.
Domain Mocks: MSW (Mock Service Worker) is the industry standard for API mocking without modifying app code. Mountebank is an alternative for Docker-based mocking.
Feature Store: A simple config file, a local server, or a library that mimics the SDK.
Directory Structure: How the code will be organized.
Integration: How these pieces talk to each other.
Developer Experience (DX): Scripts, hot reloading, UIs.
Dockerization (Optional but recommended): To ensure consistency.
Implementation Steps: A step-by-step guide to building it.
Drafting the Content:
Introduction: Define the objective (isolation, speed, realism).
Tech Stack Recommendation:
Core: Vite (or Next/Nuxt if SSR is needed, but "SPA" implies client-side heavy). Let's stick to Vite for a generic SPA.
Network: MSW (Mock Service Worker). It intercepts network requests at the service worker level.
Feature Flags:fake-flag (custom wrapper) or mimicking the provider's local SDK (e.g., LD client with offline mode).
Folder Structure: Needs to be clean.
/src (App)
/dev (The environment specific stuff)
/dev/mocks (MSW handlers)
/dev/flags (Flag definitions)
docker-compose.yml (if using containers)
Detailed Design:
1. SPA Dev Server: Standard Vite config. Proxy rules needed if MSW runs in a separate container (though MSW usually runs in-browser for SPAs). Correction: MSW works best in the browser for SPAs. If we want a "Domain Mock Server" (like a separate Docker container), we should use something like JSON Server or Mountebank. Let's assume MSW for API logic, but maybe a separate Node container for complex "Domain Mocks" if they require state persistence or external protocols (gRPC). Let's stick to MSW for the "Template" as it's cleaner for frontend devs, but mention Docker for backend dependencies (DBs, queues).
2. Domain Mocks: MSW. Defining "handlers" that look like API endpoints.
3. Feature Store Fakes: A React Context or a plain JS object injected into the app during dev. It needs a UI to toggle flags. Idea: A small companion app or a simple UI overlay in the SPA to toggle flags.
Orchestration: How to start it all? npm run dev vs docker-compose up.
Workflow:
Developer runs npm start.
App loads.
MSW intercepts API calls.
Feature Flag provider reads from local JSON file.
UI Overlay allows toggling flags.
Refining the "Feature Store Fake":
Real feature stores (LaunchDarkly, Split.io) have SDKs. The "Fake" needs to implement the same interface.
Strategy: Create an adapter pattern.
src/lib/feature-flag-client -> Real SDK.
src/lib/feature-flag-fake -> Local object.
Injection based on ENV variable.
Refining the "Domain Mocks":
Scenario: A user wants to test the "Error state" of an API.
Implementation: The mock handlers need to be dynamic. MSW allows this. Maybe expose a small control panel (like a local API endpoint) to switch mock scenarios (e.g., POST /mock-settings { scenario: '500-error' }).
Structure of the Output:
Executive Summary: What is this?
Proposed Stack: Specific tools.
Architecture Diagram (Mental): How data flows.
Directory Structure: File layout.
Implementation Details:
Frontend Server (Vite)
Network Layer (MSW)
Feature Flagging (The Fake)
State Management (The Mock Scenario)
UX/DX Features: Control panels, hot reloading.
Getting Started: The command line experience.
Self-Correction during drafting:
Should I use Docker? For a pure SPA, Docker is often overkill for the dev server, but crucial for dependencies (like a local postgres or a separate mock service). However, the prompt asks for "Domain mocks" and "Feature store fakes". These can often live inside the Node/Vite process or in the browser. Decision: Keep it simple. MSW runs in the browser (or node for SSR). Feature flags are injected. This avoids the heavy overhead of Docker for frontend dev, unless requested. I will mention Docker as an optional "sidecar" container if the mocks need to be complex standalone services.
Let's flesh out the "Feature Store Fake" more. It's often the hardest part. Developers often hardcode booleans. A "Template" needs a structured way to manage flags. A flags.json file watched by Vite? Yes. Or a UI component in the app. I'll suggest a DevTools UI overlay.
Final Polish:
Make sure to highlight why this is good: decoupling from backend instability, testing edge cases easily.
Plan Elements:
Core Engine: Vite (fast, plugins).
API/Mocks: Mock Service Worker (MSW).
Flags: A local implementation of the Flag Interface + a UI "Dev Bar" to toggle them.
Scenarios: A way to switch between "Happy Path", "Empty State", "500 Error".
Let's write it out.
This plan outlines a standardized Development Environment Template designed to decouple frontend development from backend instability and feature rollout constraints.
We will call this template spa-bunker. It uses a Node.js/Vite ecosystem for speed, MSW (Mock Service Worker) for network-level mocking, and a custom Feature Flag Fake implementation.
1. High-Level Architecture
The environment operates as a "Simulated Production" layer.
The Shell: Vite (or Webpack) serves the SPA with Hot Module Replacement (HMR).
The Network Interceptor: MSW intercepts HTTP requests at the browser level, removing the need to change API URLs in code.
The State Fabric: A local Feature Store injects configuration into the app, controllable via a local UI or JSON files.
2. Recommended Tech Stack
Component
Technology
Why?
Dev Server
Vite
Extremely fast HMR, native ESM support, and easy proxy configuration.
Domain Mocks
Mock Service Worker (MSW)
Intercepts requests at the network level. Works with fetch, axios, or React Query without code changes.
Feature Store
TypeScript Module + JSON
Simple, type-safe, and reloadable. Can be visualized via a custom DevTool component.
Data Generation
Faker.js
Generates realistic mock data (names, addresses, UUIDs).
Orchestration
NPM Workspaces / Turborepo
Keeps the mock server logic separate from the app code but runnable in parallel.
3. Directory Structure
Separate the "infrastructure" code from the "application" code to prevent mocking logic from leaking into production builds.
Instead of maintaining a separate Express server (which introduces latency and port management), use MSW. It runs in a Service Worker inside the browser.
Scenario: simulating a "User Logged In" state.
File: dev-tools/mocks/handlers/auth.ts
typescript
import { http, HttpResponse } from 'msw';
import { faker } from '@faker-js/faker';
export const authHandlers = [
http.get('/api/user/profile', () => {
return HttpResponse.json({
id: faker.string.uuid(),
email: 'dev@test.com',
role: 'admin',
// Simulate a specific backend bug state easily:
// status: 'pending_verification'
});
}),
];
B. Feature Store Fakes (The Config Layer)
Create a mock implementation of your Feature Flag provider (e.g., LaunchDarkly, Statsig) that reads from a local JSON file.
1. The Configuration (dev-tools/feature-store/flags.json)