Building plugins
Aurelia makes it easy to create your own plugins. Learn how you can create individual plugins, register them and work with tasks to run code at certain parts of the lifecycle process.
One of the most important needs of users is to design custom plugins. In the following, we want to get acquainted with how to design a plugin in the form of a mono-repository structure with configuration.
What is a mono-repository?
A monorepo (mono repository) is a single repository that stores all of your code and assets for every project. Using a monorepo is important for many reasons. It creates a single source of truth. It makes it easier to share code. It even makes it easier to refactor code.
How NPM v7 helps us?
With workspaces
. Workspaces are a set of features in the npm CLI that offer support for managing multiple packages within a single top-level, root package. NPM v7 has shipped with Node.js v15.
What is the scenario?
To move forward with a practical example. We want to implement Bootstrap components in a custom mono-repository with a configuration to make it customizable.
What is the library structure?
We want to separate our plugin into three packages.
bootstrap-v5-core
We will add the Bootstrap 5 configurations to this package.
bootstrap-v5
Our Bootstrap 5 components will define in this package. bootstrap-v5
depends on bootstrap-v5-core
packages.
demo
We will use our plugin in this package as a demo. demo
depends on bootstrap-v5-core
and bootstrap-v5
.
How to configure NPM v7 workspaces?
To configure your monorepo, you should do as following:
Make sure you have installed NPM v7+
Go to a folder that you want to make the project, for example my-plugin
Create a packages
folder and package.json
inside it.
The mono repository's name is @my-plugin
. We defined our workspaces (projects) under packages
folder.
Open your packages
folder and install the projects inside it.
After creating, delete all files inside src
folders of bootstrap-v5-core
and bootstrap-v5
but resource.d.ts
. We will add our files there.
How to manage dependencies?
As described in the structure section defined packages depend on each other. So, we link them together and add the other prerequisites for each. At the same time it is good to name them a bit better.
bootstrap-v5-core
Go to its package.json
and change the name to
As our core package, it has no dependency.
bootstrap-v5
Go to its package.json
and change the name to
Then, add the following dependencies:
demo
Go to its package.json
and change the name to
Then, add the following dependencies:
Note: All created packages have 0.1.0
version so pay attention if the version changes, update it correctly.
Run the command below to install packages inside the my-plugin
folder.
How to define a plugin configuration?
Go to the src
folder of bootstrap-v5-core
package and create each of the below files there.
Size
As I mentioned before, I want to write a configurable Bootstrap plugin so create src/Size.ts
file.
I made a Size
enum to handle all Bootstrap sizes. I want to make an option for those who use the plugin to define a global size for all Bootstrap components at first. Next, we manage our components according to size value.
Bootstrap 5 Options
Create src/BootstrapV5Options.ts
file.
You need to define your configurations via an interface with its default values as a constant.
DI
To register it via DI, you need to add codes below too:
configure
helps us to set the initial options or loading special files based on a specific option to DI system. To load specific files, you need to do this via AppTask
.
The AppTask
allows you to position when/where certain initialization should happen and also optionally block app rendering accordingly.
If you no need this feature replace it with:
Registration.instance
helps us to register our default option into the container.
To use your option later inside your components, you should introduce it via DI.createInterface
. The trick here is to create a resource name the same as what you want to inject. This is the reason I name it as IBootstrapV5Options
constant.
Finally, you need to make sure that the user can determine the settings. This is the task of BootstrapV5Configuration
.
register
This method helps the user to use your plugin with default settings but customize
is the method that allows the user to introduce their custom settings.
Exports
Create src/index.ts
file.
Create new index.ts
file inside bootstrap-v5-core
package too.
How to implement the custom plugin?
Go to the src
folder of bootstrap-v5
package, create a button
folder then create each of the below files there.
View
Create bs-button.html
file.
ViewModel
Create bs-button.ts
file.
As you can see we are able to access to plugin options easy via ctor
(DI) and react appropriately to its values.
In this example, I get the size from the user and apply it to the button component. If the user does not define a value, the default value will be used.
Exports
Create files below correctly:
Create src/button/index.ts
file.
Create src/index.ts
file.
Create new index.ts
file inside bootstrap-v5
package.
How to use it?
Open demo
package and go to the src
and update main.ts
.
Importing is available for whole components
Or just a component
To register your components you should add them to register
method.
We support configuration so we should introduce it to register
method too.
Now, You are able to use your bs-button
inside src/my-app.html
.
To run the demo
easily, go to the my-plugin
root folder and add the following script section to the package.json
.
Then, call the command
Last updated