-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: plugin support #5650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
feat: plugin support #5650
Changes from all commits
d6bba3b
73d587b
d8cd138
e0d3e9e
4799c42
3bc0fd8
31535d1
9890a76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -311,6 +311,8 @@ const DEFAULT_ALLOWED_PROTOCOLS = /^(file|.+-extension):/i; | |
| * @property {typeof useFn} use | ||
| */ | ||
|
|
||
| const pluginName = "webpack-dev-server"; | ||
|
|
||
| /** | ||
| * @template {BasicApplication} [A=ExpressApplication] | ||
| * @template {BasicServer} [S=HTTPServer] | ||
|
|
@@ -326,11 +328,14 @@ class Server { | |
| baseDataPath: "options", | ||
| }); | ||
|
|
||
| this.compiler = compiler; | ||
| /** | ||
| * @type {ReturnType<Compiler["getInfrastructureLogger"]>} | ||
| */ | ||
| this.logger = this.compiler.getInfrastructureLogger("webpack-dev-server"); | ||
| if (compiler) { | ||
| this.compiler = compiler; | ||
|
|
||
| /** | ||
| * @type {ReturnType<Compiler["getInfrastructureLogger"]>} | ||
| */ | ||
| this.logger = this.compiler.getInfrastructureLogger(pluginName); | ||
| } | ||
| this.options = options; | ||
| /** | ||
| * @type {FSWatcher[]} | ||
|
|
@@ -357,6 +362,11 @@ class Server { | |
| */ | ||
|
|
||
| this.currentHash = undefined; | ||
| /** | ||
| * @private | ||
| * @type {boolean} | ||
| */ | ||
| this.isPlugin = false; | ||
| } | ||
|
|
||
| static get schema() { | ||
|
|
@@ -1587,6 +1597,9 @@ class Server { | |
| * @returns {void} | ||
| */ | ||
| setupProgressPlugin() { | ||
| // In the case where there is no compiler and it’s not being used as a plugin. | ||
| if (this.compiler === undefined) return; | ||
|
|
||
| const { ProgressPlugin } = | ||
| /** @type {MultiCompiler} */ | ||
| (this.compiler).compilers | ||
|
|
@@ -1631,6 +1644,7 @@ class Server { | |
| * @returns {Promise<void>} | ||
| */ | ||
| async initialize() { | ||
| if (this.compiler === undefined) return; | ||
| this.setupHooks(); | ||
|
|
||
| await this.setupApp(); | ||
|
|
@@ -1706,7 +1720,7 @@ class Server { | |
| needForceShutdown = true; | ||
|
|
||
| this.stopCallback(() => { | ||
| if (typeof this.compiler.close === "function") { | ||
| if (typeof this.compiler?.close === "function") { | ||
| this.compiler.close(() => { | ||
| // eslint-disable-next-line n/no-process-exit | ||
| process.exit(); | ||
|
|
@@ -1781,11 +1795,14 @@ class Server { | |
| * @returns {void} | ||
| */ | ||
| setupHooks() { | ||
| if (this.compiler === undefined) return; | ||
|
|
||
| this.compiler.hooks.invalid.tap("webpack-dev-server", () => { | ||
| if (this.webSocketServer) { | ||
| this.sendMessage(this.webSocketServer.clients, "invalid"); | ||
| } | ||
| }); | ||
|
|
||
| this.compiler.hooks.done.tap( | ||
| "webpack-dev-server", | ||
| /** | ||
|
|
@@ -1840,6 +1857,7 @@ class Server { | |
| * @returns {void} | ||
| */ | ||
| setupMiddlewares() { | ||
| if (this.compiler === undefined) return; | ||
| /** | ||
| * @type {Array<Middleware>} | ||
| */ | ||
|
|
@@ -2346,8 +2364,10 @@ class Server { | |
| // middleware for serving webpack bundle | ||
| /** @type {import("webpack-dev-middleware").API<Request, Response>} */ | ||
| this.middleware = webpackDevMiddleware( | ||
| // @ts-expect-error | ||
| this.compiler, | ||
| this.options.devMiddleware, | ||
| this.isPlugin, | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -3414,6 +3434,30 @@ class Server { | |
| .then(() => callback(), callback) | ||
| .catch(callback); | ||
| } | ||
|
|
||
| /** | ||
| * @param {Compiler} compiler compiler | ||
| * @returns {void} | ||
| */ | ||
| apply(compiler) { | ||
| this.compiler = compiler; | ||
| this.isPlugin = true; | ||
| this.logger = this.compiler.getInfrastructureLogger(pluginName); | ||
|
|
||
| let started = false; | ||
|
|
||
| this.compiler.hooks.watchRun.tapPromise(pluginName, async () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use another hook here, we should:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m going to use the afterEmit hook
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bjohansebas let's also add a test case with |
||
| if (!started) { | ||
| started = true; | ||
| await this.start(); | ||
| } | ||
| }); | ||
|
|
||
| this.compiler.hooks.shutdown.tapPromise(pluginName, async () => { | ||
| started = false; | ||
| await this.stop(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| module.exports = Server; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,94 @@ const path = require("node:path"); | |
| const webpack = require("webpack"); | ||
| const Server = require("../../lib/Server"); | ||
| const config = require("../fixtures/client-config/webpack.config"); | ||
| const compile = require("../helpers/compile"); | ||
| const runBrowser = require("../helpers/run-browser"); | ||
| const sessionSubscribe = require("../helpers/session-subscribe"); | ||
| const port = require("../ports-map").api; | ||
|
|
||
| describe("API", () => { | ||
| it("should work with plugin API", async () => { | ||
| const compiler = webpack(config); | ||
| const server = new Server({ port }); | ||
|
|
||
| server.apply(compiler); | ||
|
|
||
| await compile(compiler, port); | ||
|
|
||
| const { page, browser } = await runBrowser(); | ||
|
|
||
| const pageErrors = []; | ||
| const consoleMessages = []; | ||
|
|
||
| page | ||
| .on("console", (message) => { | ||
| consoleMessages.push(message); | ||
| }) | ||
| .on("pageerror", (error) => { | ||
| pageErrors.push(error); | ||
| }); | ||
|
|
||
| await page.goto(`http://127.0.0.1:${port}/`, { | ||
| waitUntil: "networkidle0", | ||
| }); | ||
|
|
||
| expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( | ||
| "console messages", | ||
| ); | ||
| expect(pageErrors).toMatchSnapshot("page errors"); | ||
|
|
||
| await browser.close(); | ||
| await new Promise((resolve) => { | ||
| compiler.close(resolve); | ||
| }); | ||
| }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add full plugin tests, where dev server plugin is a part of |
||
|
|
||
| it("should not start the server multiple times on recompilation", async () => { | ||
| const compiler = webpack(config); | ||
| const server = new Server({ port }); | ||
| const startSpy = jest.spyOn(server, "start"); | ||
|
|
||
| server.apply(compiler); | ||
|
|
||
| const { watching } = await compile(compiler, port); | ||
|
|
||
| // Trigger a recompilation by invalidating | ||
| await new Promise((resolve) => { | ||
| watching.invalidate(() => { | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| // Wait for the recompilation to finish | ||
| await new Promise((resolve) => { | ||
| setTimeout(resolve, 2000); | ||
| }); | ||
|
|
||
| expect(startSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| startSpy.mockRestore(); | ||
| await new Promise((resolve) => { | ||
| compiler.close(resolve); | ||
| }); | ||
| }); | ||
|
|
||
| it("should stop the server cleanly via compiler.close()", async () => { | ||
| const compiler = webpack(config); | ||
| const server = new Server({ port }); | ||
| const stopSpy = jest.spyOn(server, "stop"); | ||
|
|
||
| server.apply(compiler); | ||
|
|
||
| await compile(compiler, port); | ||
|
|
||
| await new Promise((resolve) => { | ||
| compiler.close(resolve); | ||
| }); | ||
|
|
||
| expect(stopSpy).toHaveBeenCalledTimes(1); | ||
| stopSpy.mockRestore(); | ||
| }); | ||
|
|
||
| describe("WEBPACK_SERVE environment variable", () => { | ||
| const OLD_ENV = process.env; | ||
| let server; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we should always have
compilerhere, so we can use/* {Compiler} */ (this.compiler)to say typescript we have it is