-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev admin webpack plugin for fastify (#1474)
* Fastify: Webpack dev plugin * Use async * Add missing dep
- Loading branch information
1 parent
ca708a1
commit 93e967a
Showing
5 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
packages/electrode-archetype-react-app-dev/lib/webpack-dev-fastify.js
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,31 @@ | ||
"use strict"; | ||
/* eslint-disable no-console, no-magic-numbers */ | ||
|
||
const fastifyPlugin = require("fastify-plugin"); | ||
|
||
const archetype = require("electrode-archetype-react-app/config/archetype"); | ||
|
||
const AppDevMiddleware = require("./app-dev-middleware"); | ||
|
||
async function register(server) { | ||
if (!archetype.webpack.devMiddleware) { | ||
console.error( | ||
"dev-fastify plugin was loaded but WEBPACK_DEV_MIDDLEWARE is not true. Skipping." | ||
); | ||
return; | ||
} | ||
|
||
const middleware = new AppDevMiddleware({}); | ||
|
||
middleware.setup(); | ||
|
||
server.addHook("onRequest", async request => { | ||
request.app.webpackDev = middleware.webpackDev; | ||
}); | ||
|
||
return; | ||
} | ||
|
||
module.exports = fastifyPlugin(register, { | ||
name: "electrode-archetype-react-app-dev" | ||
}); |
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
69 changes: 69 additions & 0 deletions
69
packages/electrode-archetype-react-app-dev/test/spec/dev-fastify.spec.js
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,69 @@ | ||
"use strict"; | ||
const mockRequire = require("mock-require"); | ||
|
||
describe("dev-fastify", function() { | ||
let fakeServer; | ||
let hooks; | ||
const webpackConfig = { | ||
devMiddleware: true | ||
}; | ||
|
||
before(() => { | ||
mockRequire("fastify-plugin", (register, params) => { | ||
return { | ||
isMockFastifyPlugin: true, | ||
register, | ||
params | ||
}; | ||
}); | ||
mockRequire("electrode-archetype-react-app/config/archetype", { | ||
webpack: webpackConfig | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
hooks = {}; | ||
webpackConfig.devMiddleware = true; | ||
fakeServer = { | ||
addHook: (event, func) => { | ||
hooks[event] = func; | ||
} | ||
}; | ||
}); | ||
|
||
it("fastify exports module", () => { | ||
const fastifyMod = require("../../lib/webpack-dev-fastify"); | ||
expect(fastifyMod.isMockFastifyPlugin).true; | ||
expect(fastifyMod.params.name).eq("electrode-archetype-react-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); | ||
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); | ||
expect(hooks.onRequest).exist; | ||
const fakeRequest = { | ||
app: {} | ||
}; | ||
hooks.onRequest(fakeRequest); | ||
expect(fakeRequest.app.webpackDev).exist; | ||
}); | ||
}); |
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