Server plugins allow developers to start and stop custom servers as part of the serve lifecycle of Greenwood. These lifecycles provide the ability to do things like:
Although JavaScript is loosely typed, a server "interface" has been provided by Greenwood that you can use to start building your own server plugins. Effectively you just have to provide two methods
start
- function to run to start your serverstop
- function to run to stop / teardown your serverThey can be used in a greenwood.config.js just like any other plugin type.
import { myServerPlugin } from './my-server-plugin.js';
export default {
...
plugins: [
myServerPlugin()
]
}
The below is an excerpt of Greenwood's internal LiveReload server plugin.
class LiveReloadServer extends ServerInterface {
constructor(compilation, options = {}) {
super(compilation, options);
this.liveReloadServer = livereload.createServer({ /* options */});
}
async start() {
const { userWorkspace } = this.compilation.context;
return this.liveReloadServer.watch(userWorkspace, () => {
console.info(`Now watching directory "${userWorkspace}" for changes.`);
return Promise.resolve(true);
});
}
}
export function myServerPlugin(options = {}) {
return {
type: 'server',
name: 'plugin-livereload',
provider: (compilation) => new LiveReloadServer(compilation, options)
}
};