Skip to content

Commit 79e4bd7

Browse files
authored
docs: add magicHtml examples (#3752)
1 parent 0ba652a commit 79e4bd7

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

examples/magic-html/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Magic HTML
2+
3+
Enables/Disables magic HTML routes (enabled by default).
4+
5+
## true
6+
7+
Setting it to `true` will enable magic HTML routes ( routes corresponding to your webpack output, in this example `/main` for `main.js`):
8+
9+
**webpack.config.js**
10+
11+
```js
12+
module.exports = {
13+
// ...
14+
devServer: {
15+
magicHtml: true,
16+
},
17+
};
18+
```
19+
20+
Usage via CLI:
21+
22+
```console
23+
npx webpack serve --magic-html --open
24+
```
25+
26+
### What Should Happen
27+
28+
1. The script should open `http://localhost:8080/` in your default browser.
29+
2. Go to `http://localhost:8080/main`, you should see the text on the page itself change to read `You are viewing the magic HTML route!`.
30+
31+
## false
32+
33+
Setting it to `false` will disable magic HTML route (`/main`):
34+
35+
**webpack.config.js**
36+
37+
```js
38+
module.exports = {
39+
// ...
40+
devServer: {
41+
magicHtml: false,
42+
},
43+
};
44+
```
45+
46+
Usage via CLI:
47+
48+
```console
49+
npx webpack serve --no-magic-html --open
50+
```
51+
52+
### What Should Happen
53+
54+
1. The script should open `http://localhost:8080/` in your default browser.
55+
2. Go to `http://localhost:8080/main`, you should see the text on the page itself change to read `Cannot GET /main` as it is not accessible.

examples/magic-html/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
if (window.location.href.endsWith("main")) {
4+
document.querySelector("body").innerHTML =
5+
"<div>You are viewing the magic HTML route!</div>";
6+
} else {
7+
const target = document.querySelector("#target");
8+
9+
target.classList.add("pass");
10+
target.innerHTML = "Success!";
11+
}

examples/magic-html/webpack.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
// our setup function adds behind-the-scenes bits to the config that all of our
4+
// examples need
5+
const { setup } = require("../util");
6+
7+
module.exports = setup({
8+
context: __dirname,
9+
entry: "./app.js",
10+
devServer: {
11+
magicHtml: true,
12+
},
13+
});

0 commit comments

Comments
 (0)