Skip to content

Commit

Permalink
fix: use fastify-server for serving dev-server (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored May 13, 2020
1 parent 4a57109 commit fcceeb0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 73 deletions.
14 changes: 3 additions & 11 deletions packages/xarc-app-dev/lib/dev-admin/dev-fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

/* eslint-disable no-console, no-magic-numbers */

const fastifyPlugin = require("fastify-plugin");
const archetype = require("@xarc/app/config/archetype");
const Middleware = require("./middleware");
const Url = require("url");
const mime = require("mime");
const fs = require("fs");

async function register(fastify) {
if (!archetype.webpack.devMiddleware) {
console.error("dev-hapi plugin was loaded but WEBPACK_DEV_MIDDLEWARE is not true. Skipping.");
return;
}

const middleware = new Middleware({
baseUrl: () => {
return Url.format({
Expand All @@ -32,10 +25,11 @@ async function register(fastify) {
await middleware.process(request.raw, reply, {
skip: () => {},
replyHtml: html => {
console.log("replying", html);
reply
.code(200)
.header("Content-Type", "text/html")
.send(html);
.send(`<!DOCTYPE html>${html}`);
},
replyNotFound: () => {
reply.callNotFound();
Expand Down Expand Up @@ -70,6 +64,4 @@ async function register(fastify) {
fastify.use(middleware.hotMiddleware);
}

module.exports = fastifyPlugin(register, {
name: "electrode-dev-fastify"
});
module.exports = register;
35 changes: 27 additions & 8 deletions packages/xarc-app-dev/lib/dev-admin/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const ck = require("chalker");
const archetype = require("@xarc/app/config/archetype");
const optionalRequire = require("optional-require")(require);
const fastifyServer = optionalRequire("@xarc/fastify-server");
const electrodeServer = optionalRequire("electrode-server");
const Hapi = optionalRequire("@hapi/hapi");
const Koa = optionalRequire("koa");
Expand All @@ -18,16 +19,31 @@ if (process.env.WEBPACK_DEV === undefined) {
process.env.WEBPACK_DEV = true;
}

if (electrodeServer) {
if (fastifyServer) {
fastifyServer({
electrode: {
logLevel: "warn",
pinoOptions: false
},
connection: {
host: archetype.webpack.devHostname,
port: archetype.webpack.devPort
},
plugins: {
webpackDevFastify: {
module: "./dev-fastify",
requireFromPath: __dirname
}
}
});
} else if (electrodeServer) {
electrodeServer({
electrode: {
logLevel: "warn"
},
connections: {
default: {
host: archetype.webpack.devHostname,
port: archetype.webpack.devPort
}
connection: {
host: archetype.webpack.devHostname,
port: archetype.webpack.devPort
},
plugins: {
webpackDevHapi: {
Expand All @@ -50,7 +66,7 @@ if (electrodeServer) {
);
})
.catch(err => {
console.error(ck`<red>koa webpack dev server failed</>${err}`);
console.error(ck`<red>Hapi webpack dev server failed</>${err}`);
});
} else if (Koa) {
const app = new Koa();
Expand Down Expand Up @@ -81,7 +97,10 @@ if (electrodeServer) {
} else {
console.error(
ck(`<red>
ERROR: can't find one of electrode-server, express, koa to run dev-server.
ERROR: can't find a HTTP server to run dev-server.
Please install at least one of these dependencies:
@xarc/fastify-server@1+, electrode-server@3+, @hapi/hapi@18+, express@4+, or koa
</red>`)
);
}
6 changes: 3 additions & 3 deletions packages/xarc-app-dev/lib/dev-admin/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,13 @@ ${listDirectoryHtml(this.listAssetPath, outputPath)}
const bundle = require.resolve(`${modName}/dist/${dllParts[1]}`);
return Promise.resolve(cycle.replyFile(bundle));
} else if (req.url === this.devBaseUrl || req.url === this.devBaseUrlSlash) {
res.send(`<html><head><meta charset="utf-8"/></head><body>
const html = `<html><head><meta charset="utf-8"/></head><body>
<h1>Electrode Development Dashboard</h1>
<h3><a href="${this.cwdBaseUrl}">View current working directory files</a></h3>
<h3><a href="${this.cwdContextBaseUrl}">View webpack dev memfs files</a></h3>
<h3><a href="${this.logUrl}">View your app server console logs</a></h3>
</body></html>`);
return Promise.resolve();
</body></html>`;
return Promise.resolve(cycle.replyHtml(html));
}

return Promise.resolve(this.canContinue);
Expand Down
6 changes: 1 addition & 5 deletions packages/xarc-app-dev/lib/webpack-dev-fastify.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
/* eslint-disable no-console, no-magic-numbers */

const fastifyPlugin = require("fastify-plugin");

const archetype = require("@xarc/app/config/archetype");

const AppDevMiddleware = require("./app-dev-middleware");
Expand All @@ -26,6 +24,4 @@ async function register(server) {
return;
}

module.exports = fastifyPlugin(register, {
name: "@xarc/app-dev"
});
module.exports = register;
1 change: 0 additions & 1 deletion packages/xarc-app-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"core-js": "^3",
"electrode-hapi-compat": "^1.2.0",
"electrode-node-resolver": "^2.0.0",
"fastify-plugin": "^1.6.0",
"filter-scan-dir": "^1.0.9",
"fs-extra": "^0.30.0",
"identity-obj-proxy": "^3.0.0",
Expand Down
15 changes: 1 addition & 14 deletions packages/xarc-app-dev/test/spec/dev-admin/dev-fastify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe("dev-admin-fastify", function() {
expect(reply.called);
expect(reply.statusCode).eq(200);
expect(reply.savedHeaders).deep.eq([{ name: "Content-Type", value: "text/html" }]);
expect(reply.savedPayloads).deep.eq(["Some html content"]);
expect(reply.savedPayloads).deep.eq(["<!DOCTYPE html>Some html content"]);
});

it("process calls replyNotFound", async () => {
Expand Down Expand Up @@ -204,17 +204,4 @@ describe("dev-admin-fastify", function() {
expect(reply.statusCode).eq(404);
expect(reply.savedPayloads.length).eq(0);
});

it("middleware skip does not load middleware", () => {
mockRequire("@xarc/app/config/archetype", {
webpack: { devMiddleware: false }
});

const register = require("../../../lib/dev-admin/dev-fastify");
register(mockFastify);

expect(MiddlewareClass.setupCount).eq(0);
expect(mockFastify.use.callCount).eq(0);
expect(mockFastify.hooks.listenerCount("onRequest")).eq(0);
});
});
34 changes: 3 additions & 31 deletions packages/xarc-app-dev/test/spec/dev-fastify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ describe("dev-fastify", function() {
};

before(() => {
mockRequire("fastify-plugin", (register, params) => {
return {
isMockFastifyPlugin: true,
register,
params
};
});
mockRequire("@xarc/app/config/archetype", {
webpack: webpackConfig
});
Expand All @@ -31,38 +24,17 @@ describe("dev-fastify", function() {
};
});

it("fastify exports module", () => {
const fastifyMod = require("../../lib/webpack-dev-fastify");
expect(fastifyMod.isMockFastifyPlugin).true;
expect(fastifyMod.params.name).eq("@xarc/app-dev");
expect(fastifyMod.register).exist;
});

it("if WEBPACK_DEV_MIDDLEWARE is not true, skip with console log", () => {
webpackConfig.devMiddleware = false;
const fastifyMod = require("../../lib/webpack-dev-fastify");
expect(fastifyMod.isMockFastifyPlugin).true;
expect(fastifyMod.register).exist;
fastifyMod.register(fakeServer);
expect(hooks.onRequest).not.exist;
});
it("on request hook is attached", () => {
const fastifyMod = require("../../lib/webpack-dev-fastify");
expect(fastifyMod.isMockFastifyPlugin).true;
expect(fastifyMod.register).exist;
fastifyMod.register(fakeServer);
fastifyMod(fakeServer);
expect(hooks.onRequest).exist;
});

it("calling request hook sets webpackDev", () => {
const fastifyMod = require("../../lib/webpack-dev-fastify");
expect(fastifyMod.isMockFastifyPlugin).true;
expect(fastifyMod.register).exist;
fastifyMod.register(fakeServer);
fastifyMod(fakeServer);
expect(hooks.onRequest).exist;
const fakeRequest = {
app: {}
};
const fakeRequest = { app: {} };
hooks.onRequest(fakeRequest);
expect(fakeRequest.app.webpackDev).exist;
});
Expand Down

0 comments on commit fcceeb0

Please sign in to comment.