Skip to main content
The App API provides functions to register a mini-program application with the MorJS runtime. It wraps the native App() constructor to add lifecycle hooks, event emitting, and cross-platform adapter support.

Functions

createApp

Registers a mini-program application with the MorJS runtime. Initialises lifecycle hooks, runs registered plugins, and calls the platform’s native App() constructor.
function createApp<T extends IData, G extends IData>(
  options: MorAppOptions<T, G>,
  solution?: MorSolution | MorSolution[],
  extend?: {
    globalApp?: (options: IData) => any
    onHooksCreated?: ($hooks: MorHooks) => any
  }
): any
options
MorAppOptions<T, G>
required
The application configuration object. Supports both Alipay (tinyapp.IAppOptionsMethods) and WeChat (WechatMiniprogram.App.Option) option shapes, plus MorJS extensions.
solution
MorSolution | MorSolution[]
One or more MorJS runtime solutions (plugin bundles) to initialise before the app is registered.
extend
object
createApp can only be called once per mini-program. Calling it a second time without extend.globalApp logs an error and returns immediately.

Example

import { createApp } from '@morjs/core'

createApp({
  globalData: {
    userInfo: null
  },
  onLaunch(options) {
    console.log('App launched', options)
  },
  onShow() {
    console.log('App shown')
  },
  onHide() {
    console.log('App hidden')
  },
  onError(msg) {
    console.error('App error', msg)
  }
})

aApp

Alias for createApp. Use when your source DSL is Alipay mini-program.
export const aApp = createApp
import { aApp } from '@morjs/core'

aApp({
  onLaunch(options) {
    console.log('Alipay app launched', options)
  }
})

wApp

Alias for createApp. Use when your source DSL is WeChat mini-program.
export const wApp = createApp
import { wApp } from '@morjs/core'

wApp({
  onLaunch(options) {
    console.log('WeChat app launched', options)
  }
})

registerAppAdapters

Registers one or more cross-platform app adapters. Adapters are called during createApp and can mutate the app options before they are passed to the native App() constructor.
function registerAppAdapters(
  adapters?: MorAppAdapter | MorAppAdapter[]
): void
adapters
MorAppAdapter | MorAppAdapter[]
A single adapter object or an array of adapter objects to register.
import { registerAppAdapters } from '@morjs/core'

registerAppAdapters({
  initApp(appOptions) {
    // mutate appOptions before App() is called
    appOptions.globalData = appOptions.globalData ?? {}
  }
})

Types

MorAppInstance

Properties injected onto every app instance created by createApp.
$morHooks
MorHooks
required
MorJS runtime hooks. Use these to tap into App / Page / Component lifecycles from plugins.
$event
Emitter<any>
required
Global event emitter shared across the entire mini-program. Used internally to broadcast app-level events to pages and components.
$context
IAppContext
required
App context containing the query parameters received in onLaunch.
$morPluginsNames
string[]
required
Names of all MorJS plugins that have been loaded and initialised for the current app.

GetAppFunction

Type signature for the platform getApp function, extended to return MorAppInstance.
type GetAppFunction = <T = IData>(
  opts?: { allowDefault?: boolean }
) => MorAppInstance & T
opts.allowDefault
boolean
When true, returns a default implementation if App has not yet been defined. Useful in standalone sub-packages.

MorAppAdapter

Interface for app-level cross-platform adapters.
interface MorAppAdapter {
  initApp?: (appOptions: Record<string, any>) => void
}
initApp
(appOptions: Record<string, any>) => void
Called once during createApp, before the native App() constructor runs. Receives the fully-constructed app options object.

Lifecycle hooks

The following lifecycle methods are intercepted by MorJS and wrapped to fire the corresponding MorHooks entries:
MethodMorHook
onLaunchappOnLaunch
onShowappOnShow
onHideappOnHide
onErrorappOnError
onPageNotFoundappOnPageNotFound
onUnhandledRejectionappOnUnhandledRejection
The appOnConstruct hook (or appOnInit for backwards compatibility) fires synchronously at the start of createApp, before any lifecycle wiring occurs.

Build docs developers (and LLMs) love