JavaScript Script Blocks
Script Blocks provide an easy, inline way to use JavaScript to generate, or insert values into, your commands.
Inline Scripts
Used to evaluate some JavaScript and inline the result into a command.
<%...%>
Scope
Variables, and functions that are available for use in inline script blocks.
-
contextThe current Compiler Context.
-
embed(block: BoundBlock)Takes a
BoundBlockinstance (such as a Templateblockargument) and embeds a reference to it. -
isMacrotrueif the current line is a macro line (starts with$).
Argument Scope
Argument scope is the special scope that is used when an Inline Scripts is used as an argument for MC-Build Compile-time features, such as REPEAT, or IF expressions.
It has access to everything the normal inline script scope has, except for the embed function.
Examples
Using an Inline Script
say <% 1 + 1 %> <%10*10%>
REPEAT (1, 10) as i {
say <% i %>
}
say 2 100
say 1
say 2
say 3
say 4
say 5
say 6
say 7
say 8
say 9
say 10
Multi-line Scripts
Used to evaluate more complex JavaScript. Instead of injecting the return result of the script into a command, Multi-line Scripts include a few functions that can be used to generate commands, and more.
<%%
...
%%>
Scope
Variables, and functions that are available for use in multi-line script blocks.
-
emit(str: String)Emits a string into the output.
-
emit.mcb(str: String)Emits a string into the output after running it through the MC-Build compiler. In a top-level script block, the string can contain top-level code, such as function definitions.
-
emit.block(commands: Array[String], data?: String)Takes a list of commands, generates a function, and emits a call to that function.
datais added after the function call, for example macro arguments. Returns the generated function's name. Not available in top-level script blocks. -
contextThe current Compiler Context.
-
embed(block: BoundBlock)Takes a
BoundBlockinstance and returns a function command that references it, which can then beemited. -
require(path: String)Requires a file relative to the current file. Same as the
requirefunction in Node.js. -
isMacrotrueif the script block is in a macro context.
Examples
Using a Multi-line Script
function say_hello {
<%%
emit("say Hello World")
for (let i = 0; i < 3; i++) {
emit(`say ${i}`)
}
%%>
}
say Hello World
say 0
say 1
say 2
Generating Functions with Multi-line Scripts
At the top level of a file, emit.mcb() can generate functions. Use \n for line breaks in the emitted code.
<%%
for (let i = 0; i < 3; i++) {
emit.mcb(`function say_${i} {\nsay ${i}\n}`)
}
%%>
say 0
say 1
say 2
Global Scope
Variables, and functions that are available for use in all script blocks.
-
REPEATthe method that produces an iterator used for evaluating
REPEATexpressions. -
storeAn object that can be used to store misc values that persist between script blocks, but not between builds.
-
globalAn object that can be used to store misc values that persist between script blocks, and between rebuilds in
mcb watchmode. -
configThe object exported by your
mcb.config.js(ormcb.config.json) file. Add your own keys to it to use them in scripts, for exampleIF config.debug { ... }.