# Playlet Brighterscript Plugins - [Json5/Yaml support](#json5yaml-support) - [Manifest editing](#manifest-editing) - [Validation](#validation) - [Includes](#includes) - [@oninit](#oninit) - [Bindings](#bindings) - [Async Task Generator](#async-task-generator) - [Tracking transpilied files](#tracking-transpilied-files) - [Logger](#logger) - [Type Gen](#type-gen) - [Web Server](#web-server) - [Locale validation](#locale-validation) - [Protobuf Generator](#protobuf-generator) Playlet implements a few [Brighterscript Plugins](https://github.com/rokucommunity/brighterscript/blob/master/docs/plugins.md). The plugins inject themselves in the compilation process, allowing the modification of bs scripts, xml components, and even assets or the app manifest. Let's start with a simple one: ## Json5/Yaml support **[Source](/tools/bs-plugins/json-yaml-plugin.ts)** This plugin allows us to include `jsonc`, `json5` and `yaml` files into the app as static assets. They are an alternative to plain `json`, which for instance, does not support comments. Additionally, this plugin minifies the `json` files. ### Why Brightscript can only parse json natively using [ParseJson](https://developer.roku.com/en-ca/docs/references/brightscript/language/global-utility-functions.md#parsejsonjsonstring-as-string-flags---as-string-as-object). But it is nice to format config files as we please, include comments, and use the [Json5](https://json5.org/) format in general. Additionally, we get smaller files than the original assets. ### How At the end of the build, the plugin scans for certain files (`jsonc`, `json`, `json5` and `yaml`), and converts them to plain json, while keeping the original file name. That way they are all parsable by the [ParseJson](https://developer.roku.com/en-ca/docs/references/brightscript/language/global-utility-functions.md#parsejsonjsonstring-as-string-flags---as-string-as-object) function, even though their original format is not compatible. ## Manifest editing **[Source](/tools/bs-plugins/manifest-edit-plugin.ts)** This plugin allows us to make modifications to the app manifest dynamically, mostly based on compiler flags. ### Why A few reasons. For example: - If `--debug` flag is passed to the compiler, we want the `DEBUG` [compilation constant](https://developer.roku.com/en-ca/docs/references/brightscript/language/conditional-compilation.md) to be true - If `--test-mode` flag is passed, Turn `playlet-lib` into a standalone app instead of a component library, so we can run it as a standalone app for testing - Allows us to `DEBUG_HOST_IP_ADDRESS` with the local ip address of the debugging machine, so that `playlet-app` is able to find the `playlet-lib.zip` file that VSCode is serving during a debug session. ### How We simply read the content of the original manifest, and modify it before the compilation starts. When the build is finished, we restore the source file of the manifest. ## Validation **[Source](/tools/bs-plugins/validation-plugin.ts)** This plugin allows us to add custom diagnostic errors messages to source code, to prevent using certain patterns of functions. ### Why Let's see an example. consider this code: ```brighterscript node.ObserveField("someField", "OnSomeFieldChanged") ``` This code makes it possible to make a typo in "OnSomeFieldChanged", and this callback ends up never firing. Let's suppose we want to eliminate this pattern and always use: ```brighterscript node.ObserveField("someField", FuncName(OnSomeFieldChanged)) ``` Where `FuncName` will determine the function name at runtime. This way, we will get a linter error if there's a typo, and adds a bit of safety. ### How It's usually possible to create all types of validation plugins using the [brigherscript plugin APIs](https://github.com/rokucommunity/brighterscript/blob/master/docs/plugins.md). But this particular plugin is generic enough to allow us to define rules based on Regex. To define the rule in the previous example, we simply add this to the `bsconfig.json` file: ```json "validation": [ { "code": "NO_OBSERVE_STRING_CALLBACK", "regex": "\\.observeField(scoped)?\\s*\\(\\s*\"\\w+\"\\s*,\\s*\"\\w+\"\\s*\\)", "regexFlags": "ig", "message": "observeField(\"field\", \"callback\") is not allowed. Use observeField(\"field\", FuncName(callback)) instead." } ], ``` The plugin will scan all the code for usage matching `"regex"` with the `"regexFlags"`. If found, it will error with the `"message"` and the error code `"code"`. The code allows us to ignore this diagnostics message, using the following: ```brighterscript ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK node.ObserveField("someField", "OnSomeFieldChanged") ``` ## Includes **[Source](/tools/bs-plugins/component-includes-plugin.ts)** This plugin allows us "include" different pieces of a component, enabling [Composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance) ### Why Consider a "behaviour" that we want in multiple components. For example: ```xml