Skip to main content

Overview

@morjs/plugin-compiler is the core build plugin for MorJS. It registers the compile CLI command, builds a Webpack configuration from user config, transforms source files through platform-specific parser plugins, and bundles the output for the requested target platform.
PropertyValue
Package@morjs/plugin-compiler
Version1.0.113
Commandmor compile

Installation

npm install --save-dev @morjs/plugin-compiler

Usage

Register the plugin on a Takin instance:
import takin from '@morjs/takin'
import MorCompilerPlugin from '@morjs/plugin-compiler'

const cli = takin('mor')
cli.use([new MorCompilerPlugin()])

await cli.run(
  { name: 'compile', options: { target: 'wechat' } },
  [{ sourceType: 'alipay', target: 'wechat', srcPath: 'src' }]
)
Or run from the command line via @morjs/cli:
mor compile --target wechat
mor compile --target alipay --watch

MorCompilerPlugin

The default export. Implements the Takin Plugin interface.
export default class MorCompilerPlugin implements Plugin {
  name = 'MorCompilerPlugin'
  apply(runner: Runner): void
}
When apply is called, MorCompilerPlugin instantiates an internal MorCompile object that:
  1. Registers the compile CLI command with all supported target options.
  2. Applies parser plugins for config, script, style, template, and SJS files.
  3. Applies optimisation and injection plugins (alias, define, runtime inject, split chunks, etc.).
  4. Applies all compiler plugins returned by getComposedCompilerPlugins().
  5. Hooks into the userConfigValidated stage to apply default config and build the Webpack configuration.

Exported utilities

applyDefaults

Applies MorJS-managed default values to a CompilerUserConfig object. Called automatically during userConfigValidated.
function applyDefaults(config: Config, userConfig: CompilerUserConfig): void

COMPILE_LOADERS

An object containing all webpack loader configurations used by the compiler. Exposed so that third-party plugins can reference or extend them.
export { COMPILE_LOADERS }
Access it from a runner via the shared method getCompilerLoaders:
runner.hooks.beforeRun.tapPromise('MyPlugin', async (runner) => {
  const loaders = runner.methods.invoke('getCompilerLoaders')
})

preprocess

A code preprocessor function that evaluates conditional compilation directives (#ifdef, #ifndef, #endif) in source files.
function preprocess(source: string, conditions: Record<string, boolean>): string
source
string
required
The source code string to preprocess.
conditions
Record<string, boolean>
required
A map of condition names to boolean values used to evaluate directives.

getAllCompilerTargets

Returns an array of all registered target platform identifiers (e.g. 'wechat', 'alipay', 'baidu', 'bytedance', 'qq', 'kuaishou').
function getAllCompilerTargets(): string[]

getComposedCompilerPlugins

Returns the merged set of all registered compiler plugins, keyed by plugin name and target description.
function getComposedCompilerPlugins(): {
  Plugin: Record<string, new () => Plugin>
  targetDescription: Record<string, string>
}
Access it from a runner via the shared method getComposedCompilerPlugins:
const plugins = runner.methods.invoke('getComposedCompilerPlugins')

CompilerUserConfig

The user-facing configuration type for the compile command. Key fields:
sourceType
'alipay' | 'wechat'
required
DSL source type of the project being compiled.
target
string
required
Compilation target platform. Must be one of the values returned by getAllCompilerTargets().
srcPath
string
Source directory. Defaults to 'src'.
outputPath
string
Output directory. Defaults to dist/<target>.
watch
boolean
Enable file-watch mode. Equivalent to --watch on the CLI.
compileMode
string
Compilation mode: 'bundle' (default) or 'transform'.
plugins
Plugin[]
Additional Takin plugins to load for this compilation run.

Runner methods registered

Method nameReturnsDescription
getCompilerLoaderstypeof COMPILE_LOADERSAll webpack loader configs
getComposedCompilerPluginsComposedCompilerPluginsAll registered compiler plugins

Build docs developers (and LLMs) love