-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-dev-server.config.cjs
54 lines (46 loc) · 1.33 KB
/
web-dev-server.config.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* eslint-env node */
const fs = require("fs");
const { esbuildPlugin } = require("@web/dev-server-esbuild");
const preventFouc = `
<style>
body:not(.resolved) {
opacity: 0;
}
body {
transition: opacity 0.2s;
}
</style>
<script type="module">
// It's important to use type module for the script so the timing is correct
document.body.classList.add('resolved');
</script>
`;
module.exports = {
plugins: [
{
name: "dev-page-listing",
transform(context) {
if (context.response.is("html")) {
let body = context.body;
// Fouc prevention
body = body.replace(/<\/body>/u, `${preventFouc}\n</body>`);
// Index page listing
if (["/dev/index.html", "/dev", "/dev/"].includes(context.path)) {
const listing = `
<ul id="listing">
${fs
.readdirSync("./dev")
.filter((file) => file !== "index.html")
.filter((file) => file.endsWith(".html"))
.map((file) => `<li><a href="/dev/${file}">${file}</a></li>`)
.join("")}
</ul>`;
body = body.replace(/<ul id="listing">.*<\/ul>/u, listing);
}
return { body };
}
},
},
esbuildPlugin({ ts: true }),
],
};