-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
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,40 @@ | ||
# 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"); | ||
} | ||
|
||
const sendResponses = () => { | ||
devServer.app.get("/setup-middleware/some/path", (_, response) => { | ||
response.send("setup-middlewares option GET"); | ||
}); | ||
}; | ||
|
||
middlewares.push(sendResponses()); | ||
|
||
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`. |
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,6 @@ | ||
"use strict"; | ||
|
||
const target = document.querySelector("#target"); | ||
|
||
target.classList.add("pass"); | ||
target.innerHTML = "Success!"; |
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,27 @@ | ||
"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"); | ||
} | ||
|
||
const sendResponses = () => { | ||
devServer.app.get("/setup-middleware/some/path", (_, response) => { | ||
response.send("setup-middlewares option GET"); | ||
}); | ||
}; | ||
|
||
middlewares.push(sendResponses()); | ||
|
||
return middlewares; | ||
}, | ||
}, | ||
}); |
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