Skip to content

feat: add setupMiddlewares option #4068

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

Merged
merged 9 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/setup-middlewares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# setupMiddlewares

Provides the ability to execute a custom function and apply custom middleware(s).

**webpack.config.js**

```js
module.exports = {
// ...
devServer: {
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}

devServer.app.get("/setup-middleware/some/path", (_, response) => {
response.send("setup-middlewares option GET");
});

middlewares.push({
name: "hello-world-test-one",
// `path` is optional
path: "/foo/bar",
middleware: (req, res) => {
res.send("Foo Bar!");
},
});

middlewares.push((req, res) => {
res.send("Hello World!");
});

return middlewares;
},
},
};
```

To run this example use the following command:

```console
npx webpack serve --open
```

## What Should Happen

1. The script should open `http://localhost:8080/` in your default browser.
2. You should see the text on the page itself change to read `Success!`.
3. Go to `http://localhost:8080/setup-middleware/some/path`, you should see the text on the page itself change to read `setup-middlewares option GET`.
6 changes: 6 additions & 0 deletions examples/setup-middlewares/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

const target = document.querySelector("#target");

target.classList.add("pass");
target.innerHTML = "Success!";
36 changes: 36 additions & 0 deletions examples/setup-middlewares/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

// our setup function adds behind-the-scenes bits to the config that all of our
// examples need
const { setup } = require("../util");

module.exports = setup({
context: __dirname,
entry: "./app.js",
devServer: {
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}

devServer.app.get("/setup-middleware/some/path", (_, response) => {
response.send("setup-middlewares option GET");
});

middlewares.push({
name: "hello-world-test-one",
// `path` is optional
path: "/foo/bar",
middleware: (req, res) => {
res.send("Foo Bar!");
},
});

middlewares.push((req, res) => {
res.send("Hello World!");
});

return middlewares;
},
},
});
Loading