-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5debf45
commit 9b32276
Showing
5 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
label: 'Server' | ||
menu: side | ||
title: 'Server' | ||
index: 3 | ||
--- | ||
|
||
## Server | ||
|
||
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: | ||
- Start a live reload server (like Greenwood does by default) | ||
- Starting a GraphQL server | ||
- Reverse proxy to help route external requests | ||
|
||
### API (Server Interface) | ||
Although JavaScript is loosely typed, a [server "interface"](https://github.com/ProjectEvergreen/greenwood/tree/master/packages/cli/src/lib/server-interface.js) 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 server | ||
- `stop` - function to run to stop / teaddown your server | ||
|
||
|
||
They can be used in a _greenwood.config.js_ just like any other plugin type. | ||
```javascript | ||
const pluginMyServerFoo = require('./plugin-my-server'); | ||
|
||
module.exports = { | ||
|
||
... | ||
|
||
plugins: [ | ||
pluginMyServer() | ||
] | ||
|
||
} | ||
``` | ||
|
||
## Example | ||
The below is an excerpt of [Greenwood's internal LiveReload server](https://github.com/ProjectEvergreen/greenwood/tree/master/packages/cli/src/plugins/server/plugin-livereload.js) plugin. | ||
|
||
```javascript | ||
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); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = (options = {}) => { | ||
return { | ||
type: 'server', | ||
name: 'plugin-livereload', | ||
provider: (compilation) => new LiveReloadServer(compilation, options) | ||
} | ||
}; | ||
``` |