skills/moonbit/references/advanced-moonbit-build.md
Target specific backends/modes in moon.pkg.json:
{
"targets": {
"wasm_only.mbt": ["wasm"],
"js_only.mbt": ["js"],
"debug_only.mbt": ["debug"],
"wasm_or_js.mbt": ["wasm", "js"], // for wasm or js backend
"not_js.mbt": ["not", "js"], // for nonjs backend
"complex.mbt": ["or", ["and", "wasm", "release"], ["and", "js", "debug"]] // more complex conditions
}
}
Available conditions:
"wasm", "wasm-gc", "js", "native""debug", "release""and", "or", "not"{
"link": true, // Enable linking for this package
// OR for advanced cases:
"link": {
"wasm": {
"exports": ["hello", "foo:bar"], // Export functions
"heap-start-address": 1024, // Memory layout
"import-memory": {
// Import external memory
"module": "env",
"name": "memory"
},
"export-memory-name": "memory" // Export memory with name
},
"wasm-gc": {
"exports": ["hello"],
"use-js-builtin-string": true, // JS String Builtin support
"imported-string-constants": "_" // String namespace
},
"js": {
"exports": ["hello"],
"format": "esm" // "esm", "cjs", or "iife"
},
"native": {
"cc": "gcc", // C compiler
"cc-flags": "-O2 -DMOONBIT", // Compile flags
"cc-link-flags": "-s" // Link flags
}
}
}
Disable specific warnings in moon.mod.json or moon.pkg.json:
{
"warn-list": "-2-29" // Disable unused variable (2) & unused package (29)
}
Common warning numbers:
1 - Unused function2 - Unused variable11 - Partial pattern matching12 - Unreachable code29 - Unused packageUse moonc build-package -warn-help to see all available warnings.
Embed external files as MoonBit code:
{
"pre-build": [
{
"input": "data.txt",
"output": "embedded.mbt",
"command": ":embed -i $input -o $output --name data --text"
},
... // more embed commands
]
}
Generated code example:
///|
let data : String =
#|hello,
#|world
#|