Skip to content

Commit

Permalink
task: adding initial server api
Browse files Browse the repository at this point in the history
  • Loading branch information
hutchgrant committed Feb 3, 2021
1 parent 6d0d64b commit e3d7af2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
4 changes: 3 additions & 1 deletion greenwood.config.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,7 +25,8 @@ module.exports = {
pluginGoogleAnalytics({
analyticsId: 'UA-147204327-1'
}),
pluginPolyfills()
pluginPolyfills(),
liveReloadServer()
],
markdown: {
plugins: [
Expand Down
34 changes: 26 additions & 8 deletions packages/cli/src/commands/develop.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/src/lib/server-interface.js
Original file line number Diff line number Diff line change
@@ -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
};
2 changes: 1 addition & 1 deletion packages/cli/src/lifecycles/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions packages/cli/src/plugins/server/plugin-livereload.js
Original file line number Diff line number Diff line change
@@ -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)
};
};

0 comments on commit e3d7af2

Please sign in to comment.