diff --git a/greenwood.config.js b/greenwood.config.js index beb49ee7c..8dcde9029 100644 --- a/greenwood.config.js +++ b/greenwood.config.js @@ -1,6 +1,7 @@ const path = require('path'); const pluginGoogleAnalytics = require('./packages/plugin-google-analytics/src/index'); const pluginPolyfills = require('./packages/plugin-polyfills/src/index'); +const liveReloadServer = require('./packages/cli/src/plugins/server/plugin-livereload'); const META_DESCRIPTION = 'A modern and performant static site generator supporting Web Component based development'; const FAVICON_HREF = '/assets/favicon.ico'; @@ -24,7 +25,8 @@ module.exports = { pluginGoogleAnalytics({ analyticsId: 'UA-147204327-1' }), - pluginPolyfills() + pluginPolyfills(), + liveReloadServer() ], markdown: { plugins: [ diff --git a/packages/cli/src/commands/develop.js b/packages/cli/src/commands/develop.js index 4f8006f28..aa374a92c 100644 --- a/packages/cli/src/commands/develop.js +++ b/packages/cli/src/commands/develop.js @@ -1,5 +1,6 @@ const generateCompilation = require('../lifecycles/compile'); -const livereload = require('livereload'); +// const livereload = require('livereload'); +const { ServerInterface } = require('../lib/server-interface'); const { devServer } = require('../lifecycles/serve'); module.exports = runDevServer = async () => { @@ -9,18 +10,35 @@ module.exports = runDevServer = async () => { try { const compilation = await generateCompilation(); const { port } = compilation.config.devServer; - const { userWorkspace } = compilation.context; devServer(compilation).listen(port, () => { + console.info(`Started local development server at localhost:${port}`); - const liveReloadServer = livereload.createServer({ - exts: ['html', 'css', 'js', 'md'], - applyCSSLive: false // https://github.com/napcs/node-livereload/issues/33#issuecomment-693707006 - }); + // custom user server plugins + const servers = [...compilation.config.plugins.filter((plugin) => { + return plugin.type === 'server'; + }).map((plugin) => { + const provider = plugin.provider(compilation); + + if (!(provider instanceof ServerInterface)) { + console.warn(`WARNING: ${plugin.name}'s provider is not an instance of ServerInterface.`); + } + + return provider; + })]; - liveReloadServer.watch(userWorkspace, () => { - console.info(`Now watching directory "${userWorkspace}" for changes.`); + servers.forEach((server) => { + server.start(); }); + + // const liveReloadServer = livereload.createServer({ + // exts: ['html', 'css', 'js', 'md'], + // applyCSSLive: false // https://github.com/napcs/node-livereload/issues/33#issuecomment-693707006 + // }); + + // liveReloadServer.watch(userWorkspace, () => { + // console.info(`Now watching directory "${userWorkspace}" for changes.`); + // }); }); } catch (err) { reject(err); diff --git a/packages/cli/src/lib/server-interface.js b/packages/cli/src/lib/server-interface.js new file mode 100644 index 000000000..36019d567 --- /dev/null +++ b/packages/cli/src/lib/server-interface.js @@ -0,0 +1,15 @@ +class ServerInterface { + constructor(compilation, options = {}) { + this.compilation = compilation; + this.options = options; + } + async start() { + return Promise.resolve(false); + } + async stop() { + return Promise.resolve(false); + } +} +module.exports = { + ServerInterface +}; \ No newline at end of file diff --git a/packages/cli/src/lifecycles/config.js b/packages/cli/src/lifecycles/config.js index 2d830ce07..e81c3e517 100644 --- a/packages/cli/src/lifecycles/config.js +++ b/packages/cli/src/lifecycles/config.js @@ -69,7 +69,7 @@ module.exports = readAndMergeConfig = async() => { // } if (plugins && plugins.length > 0) { - const types = ['resource']; + const types = ['resource', 'server']; plugins.forEach(plugin => { if (!plugin.type || types.indexOf(plugin.type) < 0) { diff --git a/packages/cli/src/plugins/server/plugin-livereload.js b/packages/cli/src/plugins/server/plugin-livereload.js new file mode 100644 index 000000000..6fb61c3fb --- /dev/null +++ b/packages/cli/src/plugins/server/plugin-livereload.js @@ -0,0 +1,31 @@ +const { ServerInterface } = require('../../lib/server-interface'); +const livereload = require('livereload'); + +class LiveReloadServer extends ServerInterface { + constructor(compilation, options = {}) { + super(compilation, options); + } + + async start() { + this.liveReloadServer = livereload.createServer({ + exts: ['html', 'css', 'js', 'md'], + applyCSSLive: false // https://github.com/napcs/node-livereload/issues/33#issuecomment-693707006 + }); + this.liveReloadServer.watch(this.compilation.context.userWorkspace, () => { + console.info(`Now watching directory "${this.compilation.context.userWorkspace}" for changes.`); + }); + return Promise.resolve(true); + } + + async stop() { + return Promise.resolve(false); + } +} + +module.exports = (options = {}) => { + return { + type: 'server', + name: 'plugin-live-reload', + provider: (compilation) => new LiveReloadServer(compilation, options) + }; +}; \ No newline at end of file