Skip to main content
The Plugin API provides utilities for building mini-program plugin packages that communicate with a host mini-program, and helper functions for converting page configurations into component configurations.
This page documents the runtime plugin API (@morjs/core). For the build-time framework plugin API used with Takin, see the Takin package reference.

Functions

createPlugin

Creates a MorJS mini-program plugin object. The returned object handles two-way communication between the plugin mini-program and its host.
function createPlugin(pluginOptions: {
  getApp: GlobalGetApp
  [k: string]: any
}): CreateMorPluginResult
pluginOptions.getApp
GlobalGetApp
required
The platform getApp function available inside the plugin. MorJS uses it to access the plugin’s own app instance and inject host events.
pluginOptions
Record<string, any>
Any additional properties are spread onto the returned plugin object.
import { createPlugin } from '@morjs/core'

const plugin = createPlugin({
  getApp,
  // expose a method to the host mini-program
  greet(name: string) {
    return `Hello, ${name}`
  }
})

export default plugin

aPlugin

Alias for createPlugin. Use when your plugin source DSL is Alipay.
function aPlugin(
  ...args: Parameters<typeof createPlugin>
): ReturnType<typeof createPlugin>

wPlugin

Alias for createPlugin. Use when your plugin source DSL is WeChat.
function wPlugin(
  ...args: Parameters<typeof createPlugin>
): ReturnType<typeof createPlugin>

PageToComponent

Converts a page options object into a component options object. Useful for rendering a page inside another page or component.
function PageToComponent<D extends IData, T extends IData>(
  pageOptions: MorPageOptions<D, T>,
  sourceType: SOURCE_TYPE,
  features?: MorComponentEnhanceFeatures
): IData
pageOptions
MorPageOptions<D, T>
required
The page configuration to convert.
sourceType
SOURCE_TYPE
required
DSL source type (SOURCE_TYPE.ALIPAY or SOURCE_TYPE.WECHAT).
features
MorComponentEnhanceFeatures
Component enhancement feature flags. invokeComponentHooks defaults to false when using PageToComponent.
return
IData
Enhanced component options ready to pass to Component().
The following lifecycle mappings are applied automatically:
Page lifetimeComponent lifetime (Alipay)Component lifetime (WeChat)
onLoadonInitattached
onReadydidMountready
onUnloaddidUnmountdetached
onShowpageLifetimes.showpageLifetimes.show
onHidepageLifetimes.hidepageLifetimes.hide
onResizepageLifetimes.resizepageLifetimes.resize

aPageToComponent

Converts an Alipay-style page into component options.
function aPageToComponent<D extends IData, T extends IData>(
  options: MorPageOptions<D, T>,
  features?: MorComponentEnhanceFeatures
): any
import { aPageToComponent } from '@morjs/core'

// Use a page as a component in an Alipay mini-program
const componentOptions = aPageToComponent({
  data: { title: 'Embedded Page' },
  onLoad() {
    console.log('loaded as component')
  }
})

wPageToComponent

Converts a WeChat-style page into component options.
function wPageToComponent<D extends IData, T extends IData>(
  options: MorPageOptions<D, T>,
  features?: MorComponentEnhanceFeatures
): any

init

Initialises the MorJS runtime by loading solutions and returning the resulting hooks and plugin names. Called internally by createApp but can also be called manually in environments without a native App() constructor.
function init(
  solution?: MorSolution | MorSolution[]
): { $hooks: MorHooks; pluginsNames: string[] }
solution
MorSolution | MorSolution[]
One or more runtime solutions to load.
$hooks
MorHooks
The initialised hooks instance used by App, Page, and Component.
pluginsNames
string[]
Names of all plugins that were loaded.

Types

CreateMorPluginResult

interface CreateMorPluginResult {
  $pluginEvent?: Emitter<any>
  $hostEvent?: Emitter<any>
  $isMorPlugin: true
  getApp: GlobalGetApp
  internalInit(options?: { $event?: Emitter<any> }): void
  morInit<T extends Record<string, any>>(extend: T): void
  [k: string]: any
}
$pluginEvent
Emitter<any>
The plugin’s own event emitter (the $event from the plugin’s app instance), set at creation time if the plugin app already has $event.
$hostEvent
Emitter<any>
The host mini-program’s event emitter. Set when internalInit is called by the host.
$isMorPlugin
true
required
Marker flag that identifies this object as a MorJS plugin.
getApp
GlobalGetApp
required
Reference to the platform getApp function scoped to the plugin.
internalInit
(options?: { $event?: Emitter<any> }) => void
Called by the host mini-program to inject its $event emitter into the plugin. Sets $hostEvent on both the plugin object and the plugin’s app instance.
morInit
<T extends Record<string, any>>(extend: T) => void
Called by the host to inject capabilities into the plugin. Properties in extend are merged onto app.$host.

Build docs developers (and LLMs) love