-
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.
* task: adding initial server api * fix lint and cases * refactor live reload to be internal plugin * server plugin docs * updates post ResourceInterface changes from release branch Co-authored-by: Owen Buckley <[email protected]>
- Loading branch information
1 parent
c5c6cc8
commit ec29eca
Showing
10 changed files
with
173 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class ServerInterface { | ||
constructor(compilation, options = {}) { | ||
this.compilation = compilation; | ||
this.options = options; | ||
} | ||
|
||
async start() { | ||
return Promise.resolve(true); | ||
} | ||
|
||
async stop() { | ||
return Promise.resolve(true); | ||
} | ||
} | ||
|
||
module.exports = { | ||
ServerInterface | ||
}; |
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,58 @@ | ||
const livereload = require('livereload'); | ||
const path = require('path'); | ||
const { ResourceInterface } = require('../../lib/resource-interface'); | ||
const { ServerInterface } = require('../../lib/server-interface'); | ||
|
||
class LiveReloadServer extends ServerInterface { | ||
constructor(compilation, options = {}) { | ||
super(compilation, options); | ||
|
||
this.liveReloadServer = livereload.createServer({ | ||
exts: ['html', 'css', 'js', 'md'], | ||
applyCSSLive: false // https://github.com/napcs/node-livereload/issues/33#issuecomment-693707006 | ||
}); | ||
} | ||
|
||
async start() { | ||
const { userWorkspace } = this.compilation.context; | ||
|
||
this.liveReloadServer.watch(userWorkspace, () => { | ||
console.info(`Now watching directory "${userWorkspace}" for changes.`); | ||
return Promise.resolve(true); | ||
}); | ||
} | ||
} | ||
|
||
class LiveReloadResource extends ResourceInterface { | ||
|
||
async shouldIntercept(url) { | ||
return Promise.resolve(path.extname(url) === '' && process.env.__GWD_COMMAND__ === 'develop'); // eslint-disable-line no-underscore-dangle | ||
} | ||
|
||
async intercept(url, body) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const contents = body.replace('</head>', ` | ||
<script src="http://localhost:35729/livereload.js?snipver=1"></script> | ||
</head> | ||
`); | ||
|
||
resolve(contents); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = (options = {}) => { | ||
return [{ | ||
type: 'server', | ||
name: 'plugin-live-reload:server', | ||
provider: (compilation) => new LiveReloadServer(compilation, options) | ||
}, { | ||
type: 'resource', | ||
name: 'plugin-live-reload:resource', | ||
provider: (compilation) => new LiveReloadResource(compilation, options) | ||
}]; | ||
}; |
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) | ||
} | ||
}; | ||
``` |