Web-gambit: Modular micro front-end solution!
Web-gambit - lightweight (5Kb) modular micro front-end library for creation progressive web-application on typescript. This library provide a powerful solution based on Clean architecture
principles.
Web-gambit - easy way for creating composition of micro fronted modules from remote sources! REP
, CCP
and CRP
- its simple.
This solution is compatible with all popular frameworks: Vue, React, Angular, Svelte, Ember, etc.
Manual
1. Frontend micro modules (FMM)
1.1. Create Application Frontend Micro Module (AFMM) for instance module
AFMM - Independent reusable micro module. It can contain: Services, Interactors or Encapsulated application. AFMM can sync states without IFMM module only through a Proxy. After hard-reload page, Modules state will be reset to initial state.
import {Module} from 'web-gambit'
type ModuleState = {index: number}
const ModuleNamespace = {moduleName: 'numeric', moduleState: {index: 12345}}
Module<ModuleState>(ModuleNamespace, () => console.log(`AFMM ${ModuleNamespace.moduleName} is loaded`))
1.2. Import remote UMD Frontend Micro Module (UFMM)
UFMM - Its regular UMD modules compiled via webpack, parcel, etc.
import {Module, Emit, importModule, executeUmdModule} from 'web-gambit'
type CreatedDate = {getDate(): Date}
type UMD<ModuleType=any> = {exports: {default: ModuleType}}
const InstanceState = {createdDate: Date | null}
const InstanceNamespace = {moduleName: 'MainApp', moduleState: {createdDate: null}}
function fetchUmdModule<ModuleType=any>(): Promise<ModuleType> {
return importModule('/assets/modules/date/js/date.js').then(umdModule => {
const {exports} = executeUmdModule(umdModule) as UMD<ModuleType>
return exports.default
})
}
Module<InstanceState>(InstanceNamespace, () => {
fetchUmdModule<CreatedDate>().then(dateModule => {
Emit('createdDate', dateModule.getDate())
}).catch(console.log)
})
1.3. FMM Data transfer.
Web-gambit provides methods for transferring and synchronizing data between modules, by default.
- Watch - Create a listener for the selected command
- Emit - Executing a data transfer command
import {Module, Emit} from 'web-gambit'
type InstanceState = {text: string}
const InstanceNamespace = {moduleName: 'MainApp', moduleState: {text: 'Hello World!'}}
Module<InstanceNamespace>(InstanceNamespace, () => {
Watch({
name: 'watchUniqueName',
command: 'text',
action: (text: string) => console.log(text)
})
setInterval(() => Emit('text', Math.random().toString()), 300)
})
2. Modular micro frontend UI
You can create a modular micro-interface application through any popular library or framework. But ... Web Gambit includes its own functional UI (React like) solution for fast client-side UI development.
2.1 Create UI via render function
For creation UI component with h
and render
functions
import {createJsx as h, CreateApp, Module, CreateRouter, IRoute} from 'web-gambit'
import MainPage from './pages/main/MainPage'
type IContent = {text: string}
const ModuleNamespace = {moduleName: 'contentView', moduleState: {text: 'Hello World!'}}
const routes: IRoute[] = [{path: '/', name: 'Main', component: MainPage}]
function SomeComponent() {
return h('div', null, [
h('h1', null, [ModuleNamespace.moduleState.text]),
CreateRouter(routes)
])
}
Module<IContent>(ModuleNamespace, () => CreateApp(SomeComponent, document.getElementById('app')))