How openedx/frontend-platform works
Project Type and Purpose
• This is an open-source JavaScript/React framework package (@edx/frontend-platform) designed to provide foundational services—analytics, logging, authentication, and internationalization—for Open edX micro-frontend applications.
• It’s not a standalone web app but a library/framework that consumer micro-frontends import and initialize via its API (initialize(), getConfig(), mergeConfig(), etc.).
File Structure Analysis
• Top-level:
• src/ – core platform implementation
• example/ – demo micro-frontend application using the platform
• docs/ – architecture decisions, how-tos, templates, and published docs
• __mocks__/, jest.config.js – testing support
• babel.config.js, webpack.dev.config.js – build tool configurations
• .github/workflows/ – CI/CD pipelines (lint, test, release, publish)
• Under src/:
• analytics/, auth/, i18n/, logging/ – each has an interface module and default implementations
• initialize.js – orchestrates the application startup lifecycle (phases: config load, pub/sub init, logging, auth, analytics, i18n, ready)
• config.js, constants.js, pubSub.js, utils.js – miscellaneous utilities used by all services
• react/ – React-specific components and hooks for integrating platform services into React apps
• scripts/ – client-side scripts (e.g., GoogleAnalyticsLoader)
• testing/ – helpers for mocking and initializing a mock app in tests
• Build and deployment:
• example runs on webpack-dev-server (public/index.html)
• package.json scripts manage build, test, and publish to npm
README Insights
• Describes the “Service architecture”: strict modular separation of interfaces vs. implementations, dependency injection pattern (each service receives its dependencies via constructor).
• Describes “Application initialization”: the initialize() function with ordered lifecycle hooks that applications can tap into.
• Highlights that default implementations (New Relic, Segment, Axios/JWT, React Intl) co-exist but remain decoupled from interfaces.
Components
a. Main Components
1. Platform Core
• Initialization Engine (initialize.js)
• Config Manager (config.js, env.config.js)
• Pub/Sub Bus (pubSub.js)
• Utilities (utils.js, constants.js)
2. Service Interfaces
• AnalyticsService Interface
• LoggingService Interface
• AuthService Interface
• I18nService Interface
3. Default Service Implementations
• SegmentAnalyticsService (analytics/)
• NewRelicLoggingService (logging/)
• AxiosJwtAuthService / AxiosCsrfTokenService (auth/)
• ReactIntl I18n Implementation (i18n/)
4. Consumer Application
• Example micro-frontend (example/)
• React components/hooks under src/react/
5. External Systems
• Segment API
• New Relic API
• Auth backend (via Axios)
• Translation source (e.g., Transifex)
• npm registry (for package publish)
• GitHub/GitHub Actions (CI/CD)
b. Relationships & Data Flow
• Initialization Engine invokes lifecycle phases in order; at each phase, it:
1. Loads environment config → Config Manager
2. Initializes Pub/Sub → Pub/Sub Bus
3. Instantiates LoggingService (injects config & pub/sub) → Logging API
4. Instantiates AuthService (injects LoggingService & config) → Auth API
5. Instantiates AnalyticsService (injects LoggingService & pub/sub) → Analytics API
6. Instantiates I18nService (injects config & messages) → I18n API
7. Signals “application ready” → Consumer App subscribes to pub/sub for any post-init hooks
• Consumer App imports service APIs (via the interface layer) and calls methods (e.g., auth.login(), analytics.track(), logging.error(), intl.formatMessage()).
• Default implementations make HTTP calls or library calls to external systems (Segment, New Relic, backend, React Intl).
c. Architectural Patterns & Principles
• Dependency Injection: service implementations receive only the interfaces they depend on.
• Convention over Configuration: sensible defaults drive initialization unless overridden.
• Modular Separation: interfaces are separate from implementations; new implementations can be swapped in without changing consumers.
• Lifecycle Hooks: ordered phases allow custom code to run at precise times.
d. Key Technologies & Libraries
• React (hooks, context)
• Axios (HTTP client for auth)
• React Intl (i18n)
• Segment & New Relic SDKs
• Webpack & Babel (build)
• Jest (testing)
• GitHub Actions (CI/CD)
• npm (package management)