diff --git a/greenwood.config.js b/greenwood.config.js index 8dcde9029..beb49ee7c 100644 --- a/greenwood.config.js +++ b/greenwood.config.js @@ -1,7 +1,6 @@ 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'; @@ -25,8 +24,7 @@ module.exports = { pluginGoogleAnalytics({ analyticsId: 'UA-147204327-1' }), - pluginPolyfills(), - liveReloadServer() + pluginPolyfills() ], markdown: { plugins: [ diff --git a/packages/cli/src/commands/develop.js b/packages/cli/src/commands/develop.js index aa374a92c..b879b8229 100644 --- a/packages/cli/src/commands/develop.js +++ b/packages/cli/src/commands/develop.js @@ -1,5 +1,5 @@ const generateCompilation = require('../lifecycles/compile'); -// const livereload = require('livereload'); +const pluginLiveReloadServer = require('../plugins/server/plugin-livereload')()[0]; const { ServerInterface } = require('../lib/server-interface'); const { devServer } = require('../lifecycles/serve'); @@ -15,7 +15,7 @@ module.exports = runDevServer = async () => { console.info(`Started local development server at localhost:${port}`); // custom user server plugins - const servers = [...compilation.config.plugins.filter((plugin) => { + const servers = [...compilation.config.plugins.concat([pluginLiveReloadServer]).filter((plugin) => { return plugin.type === 'server'; }).map((plugin) => { const provider = plugin.provider(compilation); @@ -30,15 +30,6 @@ module.exports = runDevServer = async () => { 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/lifecycles/serve.js b/packages/cli/src/lifecycles/serve.js index 700bcbd27..64a0d6def 100644 --- a/packages/cli/src/lifecycles/serve.js +++ b/packages/cli/src/lifecycles/serve.js @@ -9,8 +9,9 @@ const pluginResourceStandardHtml = require('../plugins/resource/plugin-standard- const pluginResourceStandardImage = require('../plugins/resource/plugin-standard-image'); const pluginResourceStandardJavaScript = require('../plugins/resource/plugin-standard-javascript'); const pluginResourceStandardJson = require('../plugins/resource/plugin-standard-json'); -const { ResourceInterface } = require('../lib/resource-interface'); +const pluginLiveReloadResource = require('../plugins/server/plugin-livereload')()[1]; const pluginUserWorkspace = require('../plugins/resource/plugin-user-workspace'); +const { ResourceInterface } = require('../lib/resource-interface'); function getDevServer(compilation) { const app = new Koa(); @@ -88,7 +89,10 @@ function getDevServer(compilation) { // allow intercepting of urls app.use(async (ctx) => { - const modifiedResources = resources.concat(pluginNodeModules.provider(compilation)); + const modifiedResources = resources.concat( + pluginNodeModules.provider(compilation), + pluginLiveReloadResource.provider(compilationCopy) + ); const reducedResponse = await modifiedResources.reduce(async (responsePromise, resource) => { const response = await responsePromise; diff --git a/packages/cli/src/plugins/resource/plugin-standard-html.js b/packages/cli/src/plugins/resource/plugin-standard-html.js index 93d0d7121..2b8b2925f 100644 --- a/packages/cli/src/plugins/resource/plugin-standard-html.js +++ b/packages/cli/src/plugins/resource/plugin-standard-html.js @@ -89,16 +89,6 @@ const getAppTemplate = (contents, userWorkspace) => { }; const getUserScripts = (contents) => { - // TODO use an HTML parser? https://www.npmjs.com/package/node-html-parser - if (process.env.__GWD_COMMAND__ === 'develop') { // eslint-disable-line no-underscore-dangle - // TODO setup and teardown should be done together - // console.debug('running in develop mode, attach live reload script'); - contents = contents.replace('', ` - - - `); - } - if (process.env.__GWD_COMMAND__ === 'build') { // eslint-disable-line no-underscore-dangle // TODO setup and teardown should be done together // console.debug('running in build mode, polyfill WebComponents for puppeteer'); diff --git a/packages/cli/src/plugins/server/plugin-livereload.js b/packages/cli/src/plugins/server/plugin-livereload.js index 6fb61c3fb..102708f74 100644 --- a/packages/cli/src/plugins/server/plugin-livereload.js +++ b/packages/cli/src/plugins/server/plugin-livereload.js @@ -1,5 +1,7 @@ -const { ServerInterface } = require('../../lib/server-interface'); 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 = {}) { @@ -22,10 +24,40 @@ class LiveReloadServer extends ServerInterface { } } +class LiveReloadResource extends ResourceInterface { + + shouldIntercept(url) { + return path.extname(url) === ''; + } + + async intercept(body) { + return new Promise((resolve, reject) => { + try { + let contents = body; + + if (process.env.__GWD_COMMAND__ === 'develop') { // eslint-disable-line no-underscore-dangle + contents = contents.replace('', ` + + + `); + } + + resolve(contents); + } catch (e) { + reject(e); + } + }); + } +} + module.exports = (options = {}) => { - return { + return [{ type: 'server', - name: 'plugin-live-reload', + name: 'plugin-live-reload:server', provider: (compilation) => new LiveReloadServer(compilation, options) - }; + }, { + type: 'resource', + name: 'plugin-live-reload:resource', + provider: (compilation) => new LiveReloadResource(compilation, options) + }]; }; \ No newline at end of file