24

Writing a plugin for the Faqtor task runner

 4 years ago
source link: https://www.tuicool.com/articles/rIJRVzB
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Writing a plugin for Faqtor is a simple and straightforward process. Schematically, it looks like this ( TypeScript code):

import * as faqtor from “faqtor”;
export const createFactor = (<em>....arguments</em>): faqtor.IFactor => {
const run = async (): Promise<Error> => {
....do some work here
}
return factor.func(run);
}

Let us examine this with the example of the faqtor-of-rollup plugin, intended, as the name implies, to use Rollup bundler in our projects. Here is all the plugin code in TypeScript:

RRZ3Uv2.png!webMjaUFzQ.png!web

In first 2 lines, we import the necessary libraries: the faqtor itself and the rollup library which will do the main work.

Next, you need to write the main export function of the plugin, which will return the necessary factor . The fourth line of the code contains the header of this function, to which we pass RollupOptions as an argument.

Lines 5 through 16 contain the code of the closure that calls the necessary bundler functions. In particular, in line 8, the function rollup.rollup is called, which returns the bundle we need. In line 11 we write the finished bundle into options.output :

QvMzym2.png!webUbm2eib.png!web

Finally, the closure function is ready, now you need to make a factor out of it and return it, which is what happens in line 18.

AFVnqym.png!web

Thus, the process of writing a plugin is extremely simple. Just use the plugin template shown at the beginning of the article, and everything will work as it should.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK